-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmo.cpp
More file actions
108 lines (102 loc) · 2.09 KB
/
mo.cpp
File metadata and controls
108 lines (102 loc) · 2.09 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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 500005
#define mod 1000000007
int n, q;
int v[MAXN];
namespace mo
{
struct query
{
int idx, l, r;
};
int block;
vector<query> queries;
vector<int> ans;
// bool cmp(query &x, query &y) essa funcao de ordenacao pode funcionar em caso de TLE
// {
// int ablock = x.l / MAGIC, bblock = y.l / MAGIC;
// if (ablock != bblock)
// return ablock < bblock;
// if (ablock & 1)
// return x.r < y.r;
// return x.r > y.r;
// }
bool cmp(query &x, query &y)
{
if (x.l / block != y.l / block)
return x.l / block < y.l / block;
return x.r < y.r;
}
void run()
{
block = (int)sqrt(n);
sort(queries.begin(), queries.end(), cmp);
ans.resize(queries.size());
int cl = 0, cr = -1, sum = 0;
auto add = [&](int x)
{
sum += x;
};
auto rem = [&](int x)
{
sum -= x;
};
for (int i = 0; i < queries.size(); i++)
{
while (cl > queries[i].l)
{
cl--;
add(v[cl]);
}
while (cr < queries[i].r)
{
cr++;
add(v[cr]);
}
while (cl < queries[i].l)
{
rem(v[cl]);
cl++;
}
while (cr > queries[i].r)
{
rem(v[cr]);
cr--;
}
ans[queries[i].idx] = sum;
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> v[i];
for (int i = 0; i < q; i++)
{
mo::query curr;
cin >> curr.l >> curr.r;
curr.r--;
curr.idx = i;
mo::queries.pb(curr);
}
mo::run();
for (auto const &i : mo::ans)
cout << i << endl;
}
// to test: https://judge.yosupo.jp/problem/static_range_sum