-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mo.cpp
38 lines (38 loc) · 897 Bytes
/
Mo.cpp
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
//mo's , square root decomposition,
//stregth decomposition
//#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
using namespace std;
int i,l,r,n,m,b,c[200005],f[1000005];
long long cnt,ans[200005];
struct Q{
int l,r,k;
bool operator<(const Q &a)const{
return ((l-1)/b<(a.l-1)/b) || ((l-1)/b==(a.l-1)/b && r<a.r);
}
}a[200005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for(i=1;i<=n;i++) cin >> c[i];
for(i=1;i<=m;i++){
cin >> a[i].l >> a[i].r;
a[i].k=i;
}
b=sqrt(n);
sort(a+1,a+m+1);
for(l=i=1; i<=m; i++){
//write operation at c[l], adjust l and r
// according to current pattern
for(;l<a[i].l;cnt-=1ll*c[l++]);
for(;l>a[i].l;cnt+=1ll*c[--l]);
for(;r<a[i].r;cnt+=1ll*c[++r]);
for(;r>a[i].r;cnt-=1ll*c[r--]);
ans[a[i].k]=cnt;
}
for(i=1;i<=m;i++) cout << ans[i] << "\n";
}