-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmos_algorithm.cpp
More file actions
125 lines (97 loc) · 1.73 KB
/
mos_algorithm.cpp
File metadata and controls
125 lines (97 loc) · 1.73 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dbl;
#define fr(x,a,b) for(ll x=a;x<b;x++)
#define rf(x,a,b) for(ll x=a;x>b;x--)
#define pii pair<ll,ll>
#define PB push_back
#define MP make_pair
#define mod 1000000007
#define gmax LLONG_MAX
#define gmin LLONG_MIN
#define INF 2e9
#define N 200001
#define MAX(a,b,c) max(max(a,b),c)
#define MIN(a,b,c) min(min(a,b),c)
#define SZ(s) s.size()
#define MS(x,v) memset(x,v,sizeof(x))
ll n,m;
ll a[N];
ll ans;
ll block_size;
struct Query{
ll l; // left
ll r; // right
ll ind; // original index
ll res; // result of query
};
struct Query q[N];
bool cmp1(struct Query q1,struct Query q2){
if(q1.l/block_size==q2.l/block_size){
if(q1.l==q2.l) return (q1.r < q2.r);
else return (q1.l < q2.l);
}
else return (q1.l/block_size < q2.l/block_size);
}
bool cmp2(struct Query q1,struct Query q2){
return (q1.ind < q2.ind);
}
void add(ll ind){
ans+=a[ind];
}
void remove(ll ind){
ans-=a[ind];
}
void process(){
ll cl,cr;
cl=1;
cr=0;
ans=0;
ll l,r;
fr(i,0,m){
l=q[i].l;
r=q[i].r;
while(cr<r){
cr++;
add(cr);
}
while(cr>r){
remove(cr);
cr--;
}
while(cl<l){
remove(cl);
cl++;
}
while(cl>l){
cl--;
add(cl);
}
q[i].res=ans;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
fr(i,1,n+1) cin>>a[i];
block_size=sqrt(n);
// assuming 1 based indexing
ll a,b;
fr(i,0,m){
cin>>a>>b;
q[i].l=a;
q[i].r=b;
q[i].ind=i;
}
sort(q,q+m,cmp1);
process();
sort(q,q+m,cmp2);
fr(i,0,m){
cout<<"Sum in range ["<<q[i].l<<","<<q[i].r<<"]"<<" : ";
cout<<q[i].res<<"\n";
}
return 0;
}