-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMo's Algorithm.cpp
53 lines (49 loc) · 1.27 KB
/
Mo's Algorithm.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> using indexed_set = tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
ll block;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n, i, k, j, m;
cin>>n;
ll a[n];
for(i = 0; i < n; i++)
{
a[i] = rand() % n;
cout<<a[i]<<" ";
}
cout<<"\n";
cin>>m;
block = (ll)sqrt(n);
pair<ll, ll> q[m];
for(i = 0; i < m; i++)
cin>>q[i].first>>q[i].second;
sort(q, q + m, [](pair<ll, ll> a, pair<ll, ll> b)
{
if(a.first / block != b.first / block)
return a.first < b.first;
return a.second < b.second;
});
ll s = 0, l = 0, r = 0;
for(i = 0; i < m; i++)
{
while(l < q[i].first)
s -= a[l++];
while(l > q[i].first)
s += a[--l];
while(r <= q[i].second)
s += a[r++];
while(r > q[i].second + 1)
s -= a[--r];
cout<<q[i].first<<" "<<q[i].second<<" "<<s<<"\n";
}
return 0;
}