-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMO's.sublime-snippet
77 lines (70 loc) · 1.24 KB
/
MO's.sublime-snippet
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
<snippet>
<content><![CDATA[
class MOs
{
public:
int P = 1500;
vector<array<int, 3>>query;
vector<int>A;
static bool compare(const array<int, 3>& R, const array<int, 3>& S, int P)
{
if (R[0] / P != S[0] / P)
return R[0] < S[0];
else
return S[1] < R[1];
}
void init()
{
int P = this->P; // Assign P from the class
sort(query.begin(), query.end(), [P](const array<int, 3>& R, const array<int, 3>& S) {
return compare(R, S, P);
});
debug(query);
}
MOs(vector<array<int, 3>>&query, vector<int>&A)
{
this->query = query;
this->A = A;
int N = A.size();
P = sqrt(N);
init();
}
vector<int> process()
{
vector<int>ans(query.size());
int l=0;
int r=0;
int cnt=0;
for(auto it:query)
{
// right to right
while(r<=it[1])
{
r++;
}
//left to left
while(l>it[0])
{
l--;
}
//right to left
while(r>it[1]+1)
{
r--;
}
// left to right
while(l<=it[0])
{
l++;
}
ans[it[2]]=cnt;
}
return ans;
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>MO's Algorithm</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>