#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e6 + 5;
int par[maxn], sz[maxn];
int get(int x) {
if(x == par[x]) return x;
return par[x] = get(par[x]);
}
void uni(int a, int b) {
a = get(a);
b = get(b);
if(a == b) return ;
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
}
int getSize(int u) { return sz[get(u)]; }
struct Query {
int t, a, b, w;
bool operator<(Query &e) {
if(w == e.w) return (t < e.t);
return w < e.w;
}
};
ll C(ll x) {
return x * (x - 1) / 2;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> v(n);
vector<Query> qus;
for(int &x : v) cin >> x;
for(int i=1; i<n; i++)
qus.push_back({ 2, i-1, i, max(v[i], v[i-1]) });
for(int i=0; i<n; i++) {
par[i] = i, sz[i] = 1;
qus.push_back({ 1, i, i, v[i] });
}
for(int i=0; i<q; i++) {
int x;
cin >> x;
qus.push_back({ 3, i, i, x });
}
sort(qus.begin(), qus.end());
vector<ll> ans(q);
ll res = 0;
for(Query &qu : qus) {
if(qu.t == 1) {
res++;
} else if(qu.t == 2) {
res -= ( C(getSize(qu.a)) + C(getSize(qu.b)) );
uni(qu.a, qu.b);
res += C(getSize(qu.a));
} else {
ans[qu.a] = res;
}
}
for(ll &x : ans) cout << x << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
10148 KB |
Output is correct |
2 |
Correct |
27 ms |
10192 KB |
Output is correct |
3 |
Correct |
27 ms |
10192 KB |
Output is correct |
4 |
Correct |
26 ms |
7860 KB |
Output is correct |
5 |
Correct |
33 ms |
8144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
17104 KB |
Output is correct |
2 |
Correct |
42 ms |
14532 KB |
Output is correct |
3 |
Correct |
42 ms |
15056 KB |
Output is correct |
4 |
Correct |
45 ms |
16912 KB |
Output is correct |
5 |
Correct |
44 ms |
15056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
16844 KB |
Output is correct |
2 |
Correct |
42 ms |
14032 KB |
Output is correct |
3 |
Correct |
58 ms |
15308 KB |
Output is correct |
4 |
Correct |
44 ms |
17100 KB |
Output is correct |
5 |
Correct |
43 ms |
16080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
28 ms |
10148 KB |
Output is correct |
12 |
Correct |
27 ms |
10192 KB |
Output is correct |
13 |
Correct |
27 ms |
10192 KB |
Output is correct |
14 |
Correct |
26 ms |
7860 KB |
Output is correct |
15 |
Correct |
33 ms |
8144 KB |
Output is correct |
16 |
Correct |
26 ms |
7888 KB |
Output is correct |
17 |
Correct |
28 ms |
10192 KB |
Output is correct |
18 |
Correct |
28 ms |
10192 KB |
Output is correct |
19 |
Correct |
26 ms |
7888 KB |
Output is correct |
20 |
Correct |
27 ms |
10704 KB |
Output is correct |
21 |
Correct |
27 ms |
7888 KB |
Output is correct |
22 |
Correct |
26 ms |
8144 KB |
Output is correct |
23 |
Correct |
28 ms |
10400 KB |
Output is correct |
24 |
Correct |
25 ms |
7892 KB |
Output is correct |
25 |
Correct |
28 ms |
10192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
28 ms |
10148 KB |
Output is correct |
27 |
Correct |
27 ms |
10192 KB |
Output is correct |
28 |
Correct |
27 ms |
10192 KB |
Output is correct |
29 |
Correct |
26 ms |
7860 KB |
Output is correct |
30 |
Correct |
33 ms |
8144 KB |
Output is correct |
31 |
Correct |
49 ms |
17104 KB |
Output is correct |
32 |
Correct |
42 ms |
14532 KB |
Output is correct |
33 |
Correct |
42 ms |
15056 KB |
Output is correct |
34 |
Correct |
45 ms |
16912 KB |
Output is correct |
35 |
Correct |
44 ms |
15056 KB |
Output is correct |
36 |
Correct |
58 ms |
16844 KB |
Output is correct |
37 |
Correct |
42 ms |
14032 KB |
Output is correct |
38 |
Correct |
58 ms |
15308 KB |
Output is correct |
39 |
Correct |
44 ms |
17100 KB |
Output is correct |
40 |
Correct |
43 ms |
16080 KB |
Output is correct |
41 |
Correct |
26 ms |
7888 KB |
Output is correct |
42 |
Correct |
28 ms |
10192 KB |
Output is correct |
43 |
Correct |
28 ms |
10192 KB |
Output is correct |
44 |
Correct |
26 ms |
7888 KB |
Output is correct |
45 |
Correct |
27 ms |
10704 KB |
Output is correct |
46 |
Correct |
27 ms |
7888 KB |
Output is correct |
47 |
Correct |
26 ms |
8144 KB |
Output is correct |
48 |
Correct |
28 ms |
10400 KB |
Output is correct |
49 |
Correct |
25 ms |
7892 KB |
Output is correct |
50 |
Correct |
28 ms |
10192 KB |
Output is correct |
51 |
Correct |
50 ms |
16332 KB |
Output is correct |
52 |
Correct |
48 ms |
16332 KB |
Output is correct |
53 |
Correct |
51 ms |
17356 KB |
Output is correct |
54 |
Correct |
49 ms |
17360 KB |
Output is correct |
55 |
Correct |
50 ms |
16332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
28 ms |
10148 KB |
Output is correct |
27 |
Correct |
27 ms |
10192 KB |
Output is correct |
28 |
Correct |
27 ms |
10192 KB |
Output is correct |
29 |
Correct |
26 ms |
7860 KB |
Output is correct |
30 |
Correct |
33 ms |
8144 KB |
Output is correct |
31 |
Correct |
49 ms |
17104 KB |
Output is correct |
32 |
Correct |
42 ms |
14532 KB |
Output is correct |
33 |
Correct |
42 ms |
15056 KB |
Output is correct |
34 |
Correct |
45 ms |
16912 KB |
Output is correct |
35 |
Correct |
44 ms |
15056 KB |
Output is correct |
36 |
Correct |
58 ms |
16844 KB |
Output is correct |
37 |
Correct |
42 ms |
14032 KB |
Output is correct |
38 |
Correct |
58 ms |
15308 KB |
Output is correct |
39 |
Correct |
44 ms |
17100 KB |
Output is correct |
40 |
Correct |
43 ms |
16080 KB |
Output is correct |
41 |
Correct |
26 ms |
7888 KB |
Output is correct |
42 |
Correct |
28 ms |
10192 KB |
Output is correct |
43 |
Correct |
28 ms |
10192 KB |
Output is correct |
44 |
Correct |
26 ms |
7888 KB |
Output is correct |
45 |
Correct |
27 ms |
10704 KB |
Output is correct |
46 |
Correct |
27 ms |
7888 KB |
Output is correct |
47 |
Correct |
26 ms |
8144 KB |
Output is correct |
48 |
Correct |
28 ms |
10400 KB |
Output is correct |
49 |
Correct |
25 ms |
7892 KB |
Output is correct |
50 |
Correct |
28 ms |
10192 KB |
Output is correct |
51 |
Correct |
50 ms |
16332 KB |
Output is correct |
52 |
Correct |
48 ms |
16332 KB |
Output is correct |
53 |
Correct |
51 ms |
17356 KB |
Output is correct |
54 |
Correct |
49 ms |
17360 KB |
Output is correct |
55 |
Correct |
50 ms |
16332 KB |
Output is correct |
56 |
Correct |
570 ms |
77984 KB |
Output is correct |
57 |
Correct |
543 ms |
79284 KB |
Output is correct |
58 |
Correct |
510 ms |
78764 KB |
Output is correct |
59 |
Correct |
519 ms |
77740 KB |
Output is correct |
60 |
Correct |
563 ms |
78772 KB |
Output is correct |