-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmos algorithm.cpp
More file actions
74 lines (64 loc) · 1.19 KB
/
mos algorithm.cpp
File metadata and controls
74 lines (64 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
struct quary
{
int l;
int r;
};
bool compare(quary& x, quary& y)
{
if(x.l/3 != y.l/3)
{
return x.l/3<y.l/3;
}
return x.r<y.r;
}
vector<quary>v;
int sum=0;
int Mos_alg(int a[], int q_s, int n)
{
sort(v.begin(),v.end(),compare);
int c_l=0,c_r=-1;
for(int i=0; i<q_s; i++)
{
int l=v[i].l,r=v[i].r;
// cout<<"L "<<l<<" R "<<r<<endl;
while(c_l<l)
{
sum -= a[c_l];
c_l++;
}
while(c_l>l)
{
c_l--;
sum+=a[c_l];
}
while(c_r<r)
{
c_r++;
sum+=a[c_r];
//cout<<"S "<<sum<<" "<<c_r<<endl;
}
while(c_r>r)
{
sum-=a[c_r];
c_r--;
}
//cout<<v[i].l<<" to "<<v[i].r<<" === "<<sum<<endl;
}
}
int main()
{
int n;
int a[10]={1,1,1,1,1,1,1,1,1,1};
n=10;
int q;
scanf("%d",&q);
for(int i=0; i<q; i++)
{
int l,r;
cin>>l>>r;
v.push_back({l,r});
}
Mos_alg(a,q,n);
}