-
Notifications
You must be signed in to change notification settings - Fork 0
/
mo.cpp
165 lines (146 loc) · 3.48 KB
/
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <bits/stdc++.h>
using namespace std;
struct AVLNode{
AVLNode *left, *right;
short int height;
int size, value, freq;
AVLNode(int value = 0): left(NULL), right(NULL), value(value), height(1), size(1), freq(1){}
inline short int balance(){
return (right ? right->height : 0) - (left ? left->height : 0);
}
inline void update(){
height = 1 + max(left ? left->height : 0, right ? right->height : 0);
size = 1 + (left ? left->size : 0) + (right ? right->size : 0);
}
AVLNode *maxLeftChild(){
AVLNode *ret = this;
while(ret->left) ret = ret->left;
return ret;
}
};
struct AVLTree{
AVLNode *root;
AVLTree(): root(NULL){}
inline int nodeSize(AVLNode *& pos){return pos ? pos->size: 0;}
int size(){return nodeSize(root);}
void leftRotate(AVLNode *& x){
AVLNode *y = x->right, *t = y->left;
y->left = x, x->right = t;
x->update(), y->update();
x = y;
}
void rightRotate(AVLNode *& y){
AVLNode *x = y->left, *t = x->right;
x->right = y, y->left = t;
y->update(), x->update();
y = x;
}
void updateBalance(AVLNode *& pos){
short int bal = pos->balance();
if(bal > 1){
if(pos->right->balance() < 0) rightRotate(pos->right);
leftRotate(pos);
}else if(bal < -1){
if(pos->left->balance() > 0) leftRotate(pos->left);
rightRotate(pos);
}
}
void insert(AVLNode *&pos, int value){
if(pos){
if(value < pos->value) insert(pos->left, value);
else if(value > pos->value) insert(pos->right, value);
else pos->freq++;
pos->update(), updateBalance(pos);
}else{
pos = new AVLNode(value);
}
}
void erase(AVLNode *&pos, int value){
if(!pos) return;
if(value < pos->value) erase(pos->left, value);
else if(value > pos->value) erase(pos->right, value);
else{
if(pos->freq > 1) pos->freq--;
else{
if(!pos->left) pos = pos->right;
else if(!pos->right) pos = pos->left;
else{
pos->value = pos->right->maxLeftChild()->value;
erase(pos->right, pos->value);
}
}
}
if(pos) pos->update(), updateBalance(pos);
}
void insert(int value){insert(root, value);}
void erase(int value){erase(root, value);}
int leq(int x){
int ans = 0;
AVLNode *pos = root;
while(pos){
if(x < pos->value){
pos = pos->left;
}else{
ans += nodeSize(pos->left) + 1;
pos = pos->right;
}
}
return ans;
}
};
struct query{
int l, r, x, y, index, S;
bool operator<(const query & q) const{
int c_o = l / S, c_q = q.l / S;
if(c_o == c_q)
return r < q.r;
return c_o < c_q;
}
};
vector<int> MO(const vector<int> & A, vector<query> & queries){
AVLTree st;
vector<int> ans(queries.size());
sort(queries.begin(), queries.end());
int prevL = 0, prevR = -1;
int i, j;
for(const query & q : queries){
for(i = prevL, j = min(prevR, q.l - 1); i <= j; ++i){
st.erase(A[i]);
}
for(i = prevL - 1; i >= q.l; --i){
st.insert(A[i]);
}
for(i = max(prevR + 1, q.l); i <= q.r; ++i){
st.insert(A[i]);
}
for(i = prevR; i >= q.r + 1; --i){
st.erase(A[i]);
}
prevL = q.l, prevR = q.r;
ans[q.index] = st.leq(q.y) - st.leq(q.x-1);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> A(n);
for(int & ai : A){
cin >> ai;
}
int S = n / sqrt(q);
vector<query> queries(q);
for(int i = 0; i < q; ++i){
cin >> queries[i].l >> queries[i].r >> queries[i].x >> queries[i].y;
--queries[i].l;
--queries[i].r;
queries[i].S = S;
queries[i].index = i;
}
auto ans = MO(A, queries);
for(int a : ans){
cout << a << "\n";
}
return 0;
}