#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
ll curr = 0, sz[1000002], ans[1000002];
int cmp[1000002];
int find(int A) {
while (A != cmp[A]) A = cmp[A], cmp[A] = cmp[cmp[A]];
return A;
}
void onion(int A, int B) {
A = find(A), B = find(B);
curr += sz[A] * sz[B];
sz[A] += sz[B];
cmp[B] = A;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<int, int>> events;
FOR(i, 1, n + 1) {
int h;
cin >> h;
events.push_back({h, -i});
}
FOR(i, 1, q + 1) {
int h;
cin >> h;
events.push_back({h, i});
}
sort(events.begin(), events.end());
for (pair<int, int> i : events) {
if (i.second > 0) ans[i.second] = curr;
else {
curr++;
cmp[-i.second] = -i.second;
sz[-i.second] = 1;
if (cmp[-i.second - 1]) onion(-i.second, -i.second - 1);
if (cmp[-i.second + 1]) onion(-i.second, -i.second + 1);
}
}
FOR(i, 1, q + 1) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
2936 KB |
Output is correct |
2 |
Correct |
29 ms |
3064 KB |
Output is correct |
3 |
Correct |
28 ms |
2928 KB |
Output is correct |
4 |
Correct |
27 ms |
2936 KB |
Output is correct |
5 |
Correct |
26 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
5860 KB |
Output is correct |
2 |
Correct |
53 ms |
5992 KB |
Output is correct |
3 |
Correct |
51 ms |
5992 KB |
Output is correct |
4 |
Correct |
54 ms |
5992 KB |
Output is correct |
5 |
Correct |
53 ms |
5868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
5996 KB |
Output is correct |
2 |
Correct |
53 ms |
5992 KB |
Output is correct |
3 |
Correct |
54 ms |
6120 KB |
Output is correct |
4 |
Correct |
56 ms |
6272 KB |
Output is correct |
5 |
Correct |
57 ms |
6248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
27 ms |
2936 KB |
Output is correct |
12 |
Correct |
29 ms |
3064 KB |
Output is correct |
13 |
Correct |
28 ms |
2928 KB |
Output is correct |
14 |
Correct |
27 ms |
2936 KB |
Output is correct |
15 |
Correct |
26 ms |
2808 KB |
Output is correct |
16 |
Correct |
27 ms |
2808 KB |
Output is correct |
17 |
Correct |
28 ms |
3056 KB |
Output is correct |
18 |
Correct |
30 ms |
3192 KB |
Output is correct |
19 |
Correct |
27 ms |
2808 KB |
Output is correct |
20 |
Correct |
28 ms |
3064 KB |
Output is correct |
21 |
Correct |
27 ms |
2832 KB |
Output is correct |
22 |
Correct |
26 ms |
2928 KB |
Output is correct |
23 |
Correct |
28 ms |
3052 KB |
Output is correct |
24 |
Correct |
26 ms |
3064 KB |
Output is correct |
25 |
Correct |
28 ms |
2928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
27 ms |
2936 KB |
Output is correct |
27 |
Correct |
29 ms |
3064 KB |
Output is correct |
28 |
Correct |
28 ms |
2928 KB |
Output is correct |
29 |
Correct |
27 ms |
2936 KB |
Output is correct |
30 |
Correct |
26 ms |
2808 KB |
Output is correct |
31 |
Correct |
54 ms |
5860 KB |
Output is correct |
32 |
Correct |
53 ms |
5992 KB |
Output is correct |
33 |
Correct |
51 ms |
5992 KB |
Output is correct |
34 |
Correct |
54 ms |
5992 KB |
Output is correct |
35 |
Correct |
53 ms |
5868 KB |
Output is correct |
36 |
Correct |
53 ms |
5996 KB |
Output is correct |
37 |
Correct |
53 ms |
5992 KB |
Output is correct |
38 |
Correct |
54 ms |
6120 KB |
Output is correct |
39 |
Correct |
56 ms |
6272 KB |
Output is correct |
40 |
Correct |
57 ms |
6248 KB |
Output is correct |
41 |
Correct |
27 ms |
2808 KB |
Output is correct |
42 |
Correct |
28 ms |
3056 KB |
Output is correct |
43 |
Correct |
30 ms |
3192 KB |
Output is correct |
44 |
Correct |
27 ms |
2808 KB |
Output is correct |
45 |
Correct |
28 ms |
3064 KB |
Output is correct |
46 |
Correct |
27 ms |
2832 KB |
Output is correct |
47 |
Correct |
26 ms |
2928 KB |
Output is correct |
48 |
Correct |
28 ms |
3052 KB |
Output is correct |
49 |
Correct |
26 ms |
3064 KB |
Output is correct |
50 |
Correct |
28 ms |
2928 KB |
Output is correct |
51 |
Correct |
60 ms |
5864 KB |
Output is correct |
52 |
Correct |
59 ms |
5868 KB |
Output is correct |
53 |
Correct |
60 ms |
5864 KB |
Output is correct |
54 |
Correct |
58 ms |
5736 KB |
Output is correct |
55 |
Correct |
61 ms |
5864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
27 ms |
2936 KB |
Output is correct |
27 |
Correct |
29 ms |
3064 KB |
Output is correct |
28 |
Correct |
28 ms |
2928 KB |
Output is correct |
29 |
Correct |
27 ms |
2936 KB |
Output is correct |
30 |
Correct |
26 ms |
2808 KB |
Output is correct |
31 |
Correct |
54 ms |
5860 KB |
Output is correct |
32 |
Correct |
53 ms |
5992 KB |
Output is correct |
33 |
Correct |
51 ms |
5992 KB |
Output is correct |
34 |
Correct |
54 ms |
5992 KB |
Output is correct |
35 |
Correct |
53 ms |
5868 KB |
Output is correct |
36 |
Correct |
53 ms |
5996 KB |
Output is correct |
37 |
Correct |
53 ms |
5992 KB |
Output is correct |
38 |
Correct |
54 ms |
6120 KB |
Output is correct |
39 |
Correct |
56 ms |
6272 KB |
Output is correct |
40 |
Correct |
57 ms |
6248 KB |
Output is correct |
41 |
Correct |
27 ms |
2808 KB |
Output is correct |
42 |
Correct |
28 ms |
3056 KB |
Output is correct |
43 |
Correct |
30 ms |
3192 KB |
Output is correct |
44 |
Correct |
27 ms |
2808 KB |
Output is correct |
45 |
Correct |
28 ms |
3064 KB |
Output is correct |
46 |
Correct |
27 ms |
2832 KB |
Output is correct |
47 |
Correct |
26 ms |
2928 KB |
Output is correct |
48 |
Correct |
28 ms |
3052 KB |
Output is correct |
49 |
Correct |
26 ms |
3064 KB |
Output is correct |
50 |
Correct |
28 ms |
2928 KB |
Output is correct |
51 |
Correct |
60 ms |
5864 KB |
Output is correct |
52 |
Correct |
59 ms |
5868 KB |
Output is correct |
53 |
Correct |
60 ms |
5864 KB |
Output is correct |
54 |
Correct |
58 ms |
5736 KB |
Output is correct |
55 |
Correct |
61 ms |
5864 KB |
Output is correct |
56 |
Correct |
655 ms |
54980 KB |
Output is correct |
57 |
Correct |
648 ms |
54592 KB |
Output is correct |
58 |
Correct |
587 ms |
51520 KB |
Output is correct |
59 |
Correct |
614 ms |
52804 KB |
Output is correct |
60 |
Correct |
648 ms |
56256 KB |
Output is correct |