-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathF.cpp
254 lines (239 loc) · 6.41 KB
/
F.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <bits/stdc++.h>
using namespace std;
int INF = 1000000007;
int tree[2097152];
int lazy[2097152];
int tree2[2097152];
int lazy2[2097152];
void push(int v, int tl, int tr) {
if (tl<tr) {
lazy[2*v] += lazy[v];
lazy[2*v+1] += lazy[v];
}
tree[v] += lazy[v];
lazy[v] = 0;
}
void update(int v, int l, int r, int tl, int tr, int addend) {
//push(v,tl,tr);
if (l==r) {
l+=1048576;
tree[l] += addend;
while (l>0) {
l/=2;
tree[l] = max(tree[l*2], tree[l*2+1]);
}
return;
}
if (l>tr || r<tl) {
return;
}
if (l<=tl && tr<=r) {
tree[v] += addend;
return;
}
int m = (tl+tr)/2;
update(2*v,l,r,tl,m,addend);
update(2*v+1,l,r,m+1,tr,addend);
tree[v] = max(tree[2*v], tree[2*v+1]);
}
int query(int v, int l, int r, int tl, int tr) {
int ans=0;
l+=1048576;
r+=1048576;
while (l<r) {
if (l==r) {
break;
}
if (l%2==1) {
ans=max(ans,tree[l]);
l++;
}
if (l==r) {
break;
}
if (r%2==0) {
ans=max(ans,tree[r]);
r--;
}
if (l==r) {
break;
}
l/=2;
r/=2;
if (l==r) {
break;
}
}
ans=max(ans,tree[l]);
return ans;
}
void push2(int v, int tl, int tr) {
if (tl<tr) {
lazy2[2*v] += lazy2[v];
lazy2[2*v+1] += lazy2[v];
}
tree2[v] += lazy2[v]*(tr-tl+1);
lazy2[v] = 0;
}
void update2(int v, int l, int r, int tl, int tr, int addend) {
//push2(v,tl,tr);
if (l==r) {
l+=1048576;
tree2[l] += addend;
while (l>0) {
l/=2;
tree2[l] = (tree2[l*2] + tree2[l*2+1]);
}
return;
}
if (l>tr || r<tl) {
return;
}
if (l<=tl && tr<=r) {
tree2[v] += addend*(tr-tl+1);
return;
}
int m = (tl+tr)/2;
update2(2*v,l,r,tl,m,addend);
update2(2*v+1,l,r,m+1,tr,addend);
tree2[v] = (tree2[2*v]+tree2[2*v+1]);
}
int query2(int v, int l, int r, int tl, int tr) {
int ans=0;
l+=1048576;
r+=1048576;
while (l<r) {
if (l==r) {
break;
}
if (l%2==1) {
ans+=tree2[l];
l++;
}
if (l==r) {
break;
}
if (r%2==0) {
ans+=tree2[r];
r--;
}
if (l==r) {
break;
}
l/=2;
r/=2;
if (l==r) {
break;
}
}
ans+=tree2[l];
return ans;
}
signed main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif //fast IO
int n,q;
cin >> n >> q;
int T;
if (n>100000) {
T=1000;
}
else {
T=2097152;
}
for (int i=0; i<T; i++) {
tree[i] = lazy[i] = tree2[i] = lazy2[i] = 0;
}
vector<int> arr;
vector<vector<int>> inddict(n+1);
for (int i=0; i<n; i++) {
int x;
cin >> x;
arr.push_back(x);
inddict[x].push_back(i);
}
vector<vector<int>> queries;
for (int i=0; i<q; i++) {
int l,r;
cin >> l >> r;
queries.push_back({l,r,i});
}
//for each value, say v is in the interval
//then based on the left endpoint the answer can be calculated
//the answer for a value is the number of consecutive pairs of it
//which are split with a smaller element
//therefore using a max segtree
//for each value
//calculate the maximum splitting number for each pair
//this can be done by querying and then unlocking each number in turn
//now make this a set of events for each l
//such that when l is excluded from the interval
//which right endpoints have their answer reduced by 1
//there are at most O(n) such events because there are O(n)
//pairs of consecutive elements with the same value
//and each one determines at most one event
//and then read off the value for r, when the query is given
//(once the queries are sorted in order of l)
//events[l] denotes a series of positions such that when l value goes above l
//then right value of this element is subtracted by one
vector<int> qans(q,-1);
sort(queries.begin(), queries.end());
vector<vector<vector<int>>> qu(n+1);
for (auto i:queries) {
qu[i[0]].push_back({i[1],i[2]});
}
vector<vector<int>> events(n+1);
vector<int> answers(n+1,0);
for (int val=0; val<=n; val++) {
if (inddict[val].size()==0) {
//if (val>0) answers[val] = answers[val-1];
continue;
}
answers[val]++;
for (int j=0; j<inddict[val].size()-1; j++) {
//cout << val << " " << j << " " << inddict[val][j]<<" " << inddict[val][j+1]<<endl;
int maxbloat = query(1, inddict[val][j], inddict[val][j+1], 0, 1048575);
if (maxbloat==0) {
continue;
}
answers[val]++;
events[maxbloat].push_back(val);
//cout << maxbloat << endl;
}
for (int j=0; j<inddict[val].size(); j++) {
update(1, inddict[val][j], inddict[val][j], 0, 1048575, val);
}
//if (val>0) answers[val]+=answers[val-1];
}
for (int i=0; i<=n; i++) {
update2(1,i,i,0,1048575,answers[i]);
}
for (int i=0; i<=n; i++) {
//cout << "VALUE" <<" "<< i <<" " << query2(1,i,i,0,1048575) << endl;
}
for (int i=0; i<=n; i++) {
//cout << "TESTING " << i << " " << answers[i] << endl;
//for (auto j:events[i]) {
// cout << j <<" ";
//}
//cout << endl;
for (auto Q:qu[i]) {
//cout << i <<" " << Q[1] << endl;
qans[Q[1]] = query2(1,i,Q[0],0,1048575);
}
for (auto j:events[i]) {
update2(1, j, j, 0, 1048575, -1);
}
}
for (int i=0; i<q; i++) {
cout << qans[i] << endl;
}
}