#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10, INF = 1e9+7;
using ll = long long;
using pi = pair<int,int>;
int n,Q,h[N],lef[N],rig[N],pa[N];
ll ans, ansp[N];
bool We[N],Ws[N];
vector<pi> vec,qq;
map<int, int> E,S; //E for end point of subarray, S for start point of subarray
int fi(int x){ return pa[x] = (pa[x] == x) ? x : fi(pa[x]); }
void rem(int id){
int ir = 0, il = 0;
if(Ws[id+1]){
ir = fi(id+1);
}
if(We[id-1]){
il = fi(id-1);
}
if (!ir and !il){ //create new subarray
lef[id] = rig[id] = id;
Ws[id] = We[id] = true;
ans += 1;
} else if(ir and il){ //merge left and right subarray together
int tsr = rig[ir] - lef[ir] + 1;
int tsl = rig[il] - lef[il] + 1;
int tsz = tsr + tsl + 1;
ans -= 1ll*tsr*(tsr+1)/2; //delete previous ans and add new one later
ans -= 1ll*tsl*(tsl+1)/2;
ans += 1ll*tsz*(tsz+1)/2;
rig[il] = rig[ir];
pa[ir] = il;
Ws[id+1] = We[id-1] = false;
} else if(ir){
int tsr = rig[ir] - lef[ir] + 1;
ans -= 1ll*tsr*(tsr+1)/2;
ans += 1ll*(tsr+1)*(tsr+2)/2;
lef[ir]--;
pa[id] = ir;
Ws[id+1] = false;
Ws[id] = true;
} else if(il){
int tsl = rig[il] - lef[il] + 1;
ans -= 1ll*tsl*(tsl+1)/2;
ans += 1ll*(tsl+1)*(tsl+2)/2;
rig[il]++;
pa[id] = il;
We[id-1] = false;
We[id] = true;
}
return;
}
int main(void){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> Q;
for(int i = 1; i <= n; i++){
cin >> h[i];
vec.emplace_back(h[i], i);
pa[i] = i;
}
for(int i = 1; i <= Q; i++){
int x; cin >> x;
qq.emplace_back(x, i);
}
sort(qq.begin(), qq.end());
sort(vec.begin(), vec.end());
int vi = 0; //pointer of vector's index
for(auto [want, iq] : qq){
while(vi < n and vec[vi].first <= want){
rem(vec[vi].second);
vi++;
}
/*cout << "CHK : " << want << "\n";
for(int i = 1; i <= n; i++){
cout << fi(i) << " " << lef[fi(i)] << " " << rig[fi(i)] << "\n";
}*/
ansp[iq] = ans;
}
for(int i = 1; i <= Q; i++){
cout << ansp[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
2996 KB |
Output is correct |
2 |
Correct |
19 ms |
2888 KB |
Output is correct |
3 |
Correct |
18 ms |
2836 KB |
Output is correct |
4 |
Correct |
21 ms |
2792 KB |
Output is correct |
5 |
Correct |
18 ms |
2828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
4456 KB |
Output is correct |
2 |
Correct |
36 ms |
4476 KB |
Output is correct |
3 |
Correct |
37 ms |
4448 KB |
Output is correct |
4 |
Correct |
33 ms |
4488 KB |
Output is correct |
5 |
Correct |
33 ms |
4408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
4404 KB |
Output is correct |
2 |
Correct |
39 ms |
4480 KB |
Output is correct |
3 |
Correct |
40 ms |
4432 KB |
Output is correct |
4 |
Correct |
36 ms |
4440 KB |
Output is correct |
5 |
Correct |
37 ms |
4556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
22 ms |
2996 KB |
Output is correct |
12 |
Correct |
19 ms |
2888 KB |
Output is correct |
13 |
Correct |
18 ms |
2836 KB |
Output is correct |
14 |
Correct |
21 ms |
2792 KB |
Output is correct |
15 |
Correct |
18 ms |
2828 KB |
Output is correct |
16 |
Correct |
17 ms |
2748 KB |
Output is correct |
17 |
Correct |
18 ms |
2888 KB |
Output is correct |
18 |
Correct |
18 ms |
2932 KB |
Output is correct |
19 |
Correct |
20 ms |
2812 KB |
Output is correct |
20 |
Correct |
19 ms |
2860 KB |
Output is correct |
21 |
Correct |
24 ms |
2760 KB |
Output is correct |
22 |
Correct |
16 ms |
2760 KB |
Output is correct |
23 |
Correct |
17 ms |
3016 KB |
Output is correct |
24 |
Correct |
18 ms |
2760 KB |
Output is correct |
25 |
Correct |
17 ms |
2884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
22 ms |
2996 KB |
Output is correct |
27 |
Correct |
19 ms |
2888 KB |
Output is correct |
28 |
Correct |
18 ms |
2836 KB |
Output is correct |
29 |
Correct |
21 ms |
2792 KB |
Output is correct |
30 |
Correct |
18 ms |
2828 KB |
Output is correct |
31 |
Correct |
32 ms |
4456 KB |
Output is correct |
32 |
Correct |
36 ms |
4476 KB |
Output is correct |
33 |
Correct |
37 ms |
4448 KB |
Output is correct |
34 |
Correct |
33 ms |
4488 KB |
Output is correct |
35 |
Correct |
33 ms |
4408 KB |
Output is correct |
36 |
Correct |
33 ms |
4404 KB |
Output is correct |
37 |
Correct |
39 ms |
4480 KB |
Output is correct |
38 |
Correct |
40 ms |
4432 KB |
Output is correct |
39 |
Correct |
36 ms |
4440 KB |
Output is correct |
40 |
Correct |
37 ms |
4556 KB |
Output is correct |
41 |
Correct |
17 ms |
2748 KB |
Output is correct |
42 |
Correct |
18 ms |
2888 KB |
Output is correct |
43 |
Correct |
18 ms |
2932 KB |
Output is correct |
44 |
Correct |
20 ms |
2812 KB |
Output is correct |
45 |
Correct |
19 ms |
2860 KB |
Output is correct |
46 |
Correct |
24 ms |
2760 KB |
Output is correct |
47 |
Correct |
16 ms |
2760 KB |
Output is correct |
48 |
Correct |
17 ms |
3016 KB |
Output is correct |
49 |
Correct |
18 ms |
2760 KB |
Output is correct |
50 |
Correct |
17 ms |
2884 KB |
Output is correct |
51 |
Correct |
43 ms |
5208 KB |
Output is correct |
52 |
Correct |
52 ms |
5048 KB |
Output is correct |
53 |
Correct |
44 ms |
5184 KB |
Output is correct |
54 |
Correct |
40 ms |
4976 KB |
Output is correct |
55 |
Correct |
42 ms |
5192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
22 ms |
2996 KB |
Output is correct |
27 |
Correct |
19 ms |
2888 KB |
Output is correct |
28 |
Correct |
18 ms |
2836 KB |
Output is correct |
29 |
Correct |
21 ms |
2792 KB |
Output is correct |
30 |
Correct |
18 ms |
2828 KB |
Output is correct |
31 |
Correct |
32 ms |
4456 KB |
Output is correct |
32 |
Correct |
36 ms |
4476 KB |
Output is correct |
33 |
Correct |
37 ms |
4448 KB |
Output is correct |
34 |
Correct |
33 ms |
4488 KB |
Output is correct |
35 |
Correct |
33 ms |
4408 KB |
Output is correct |
36 |
Correct |
33 ms |
4404 KB |
Output is correct |
37 |
Correct |
39 ms |
4480 KB |
Output is correct |
38 |
Correct |
40 ms |
4432 KB |
Output is correct |
39 |
Correct |
36 ms |
4440 KB |
Output is correct |
40 |
Correct |
37 ms |
4556 KB |
Output is correct |
41 |
Correct |
17 ms |
2748 KB |
Output is correct |
42 |
Correct |
18 ms |
2888 KB |
Output is correct |
43 |
Correct |
18 ms |
2932 KB |
Output is correct |
44 |
Correct |
20 ms |
2812 KB |
Output is correct |
45 |
Correct |
19 ms |
2860 KB |
Output is correct |
46 |
Correct |
24 ms |
2760 KB |
Output is correct |
47 |
Correct |
16 ms |
2760 KB |
Output is correct |
48 |
Correct |
17 ms |
3016 KB |
Output is correct |
49 |
Correct |
18 ms |
2760 KB |
Output is correct |
50 |
Correct |
17 ms |
2884 KB |
Output is correct |
51 |
Correct |
43 ms |
5208 KB |
Output is correct |
52 |
Correct |
52 ms |
5048 KB |
Output is correct |
53 |
Correct |
44 ms |
5184 KB |
Output is correct |
54 |
Correct |
40 ms |
4976 KB |
Output is correct |
55 |
Correct |
42 ms |
5192 KB |
Output is correct |
56 |
Correct |
530 ms |
47332 KB |
Output is correct |
57 |
Correct |
528 ms |
60188 KB |
Output is correct |
58 |
Correct |
502 ms |
56860 KB |
Output is correct |
59 |
Correct |
509 ms |
58156 KB |
Output is correct |
60 |
Correct |
555 ms |
61848 KB |
Output is correct |