#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e6 + 5;
int par[maxn], sz[maxn];
void init(int n) {
for(int i=0; i<n; i++) par[i] = i, sz[i] = 1;
}
int get(int x) {
if(x == par[x]) return x;
return par[x] = get(par[x]);
}
bool uni(int a, int b) {
a = get(a);
b = get(b);
if(a == b) return 1;
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
return 1;
}
bool sameSet(int u, int v) { return get(u) == get(v); }
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++)
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;
init(n);
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 |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 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 |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 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 |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 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 |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 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 |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2596 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 |
33 ms |
10184 KB |
Output is correct |
2 |
Correct |
28 ms |
10192 KB |
Output is correct |
3 |
Correct |
27 ms |
10184 KB |
Output is correct |
4 |
Correct |
26 ms |
9164 KB |
Output is correct |
5 |
Correct |
27 ms |
9160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
14376 KB |
Output is correct |
2 |
Correct |
42 ms |
12736 KB |
Output is correct |
3 |
Correct |
41 ms |
12224 KB |
Output is correct |
4 |
Correct |
43 ms |
14780 KB |
Output is correct |
5 |
Correct |
41 ms |
12188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
14212 KB |
Output is correct |
2 |
Correct |
45 ms |
11020 KB |
Output is correct |
3 |
Correct |
42 ms |
11960 KB |
Output is correct |
4 |
Correct |
45 ms |
14496 KB |
Output is correct |
5 |
Correct |
44 ms |
13508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
33 ms |
10184 KB |
Output is correct |
12 |
Correct |
28 ms |
10192 KB |
Output is correct |
13 |
Correct |
27 ms |
10184 KB |
Output is correct |
14 |
Correct |
26 ms |
9164 KB |
Output is correct |
15 |
Correct |
27 ms |
9160 KB |
Output is correct |
16 |
Correct |
26 ms |
8144 KB |
Output is correct |
17 |
Correct |
28 ms |
10524 KB |
Output is correct |
18 |
Correct |
29 ms |
11216 KB |
Output is correct |
19 |
Correct |
26 ms |
8144 KB |
Output is correct |
20 |
Correct |
27 ms |
10436 KB |
Output is correct |
21 |
Correct |
26 ms |
8912 KB |
Output is correct |
22 |
Correct |
26 ms |
8140 KB |
Output is correct |
23 |
Correct |
28 ms |
10444 KB |
Output is correct |
24 |
Correct |
29 ms |
7888 KB |
Output is correct |
25 |
Correct |
27 ms |
10156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 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 |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2596 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 |
33 ms |
10184 KB |
Output is correct |
27 |
Correct |
28 ms |
10192 KB |
Output is correct |
28 |
Correct |
27 ms |
10184 KB |
Output is correct |
29 |
Correct |
26 ms |
9164 KB |
Output is correct |
30 |
Correct |
27 ms |
9160 KB |
Output is correct |
31 |
Correct |
47 ms |
14376 KB |
Output is correct |
32 |
Correct |
42 ms |
12736 KB |
Output is correct |
33 |
Correct |
41 ms |
12224 KB |
Output is correct |
34 |
Correct |
43 ms |
14780 KB |
Output is correct |
35 |
Correct |
41 ms |
12188 KB |
Output is correct |
36 |
Correct |
47 ms |
14212 KB |
Output is correct |
37 |
Correct |
45 ms |
11020 KB |
Output is correct |
38 |
Correct |
42 ms |
11960 KB |
Output is correct |
39 |
Correct |
45 ms |
14496 KB |
Output is correct |
40 |
Correct |
44 ms |
13508 KB |
Output is correct |
41 |
Correct |
26 ms |
8144 KB |
Output is correct |
42 |
Correct |
28 ms |
10524 KB |
Output is correct |
43 |
Correct |
29 ms |
11216 KB |
Output is correct |
44 |
Correct |
26 ms |
8144 KB |
Output is correct |
45 |
Correct |
27 ms |
10436 KB |
Output is correct |
46 |
Correct |
26 ms |
8912 KB |
Output is correct |
47 |
Correct |
26 ms |
8140 KB |
Output is correct |
48 |
Correct |
28 ms |
10444 KB |
Output is correct |
49 |
Correct |
29 ms |
7888 KB |
Output is correct |
50 |
Correct |
27 ms |
10156 KB |
Output is correct |
51 |
Correct |
54 ms |
14008 KB |
Output is correct |
52 |
Correct |
52 ms |
13412 KB |
Output is correct |
53 |
Correct |
54 ms |
13776 KB |
Output is correct |
54 |
Correct |
49 ms |
14404 KB |
Output is correct |
55 |
Correct |
50 ms |
14532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 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 |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2596 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 |
33 ms |
10184 KB |
Output is correct |
27 |
Correct |
28 ms |
10192 KB |
Output is correct |
28 |
Correct |
27 ms |
10184 KB |
Output is correct |
29 |
Correct |
26 ms |
9164 KB |
Output is correct |
30 |
Correct |
27 ms |
9160 KB |
Output is correct |
31 |
Correct |
47 ms |
14376 KB |
Output is correct |
32 |
Correct |
42 ms |
12736 KB |
Output is correct |
33 |
Correct |
41 ms |
12224 KB |
Output is correct |
34 |
Correct |
43 ms |
14780 KB |
Output is correct |
35 |
Correct |
41 ms |
12188 KB |
Output is correct |
36 |
Correct |
47 ms |
14212 KB |
Output is correct |
37 |
Correct |
45 ms |
11020 KB |
Output is correct |
38 |
Correct |
42 ms |
11960 KB |
Output is correct |
39 |
Correct |
45 ms |
14496 KB |
Output is correct |
40 |
Correct |
44 ms |
13508 KB |
Output is correct |
41 |
Correct |
26 ms |
8144 KB |
Output is correct |
42 |
Correct |
28 ms |
10524 KB |
Output is correct |
43 |
Correct |
29 ms |
11216 KB |
Output is correct |
44 |
Correct |
26 ms |
8144 KB |
Output is correct |
45 |
Correct |
27 ms |
10436 KB |
Output is correct |
46 |
Correct |
26 ms |
8912 KB |
Output is correct |
47 |
Correct |
26 ms |
8140 KB |
Output is correct |
48 |
Correct |
28 ms |
10444 KB |
Output is correct |
49 |
Correct |
29 ms |
7888 KB |
Output is correct |
50 |
Correct |
27 ms |
10156 KB |
Output is correct |
51 |
Correct |
54 ms |
14008 KB |
Output is correct |
52 |
Correct |
52 ms |
13412 KB |
Output is correct |
53 |
Correct |
54 ms |
13776 KB |
Output is correct |
54 |
Correct |
49 ms |
14404 KB |
Output is correct |
55 |
Correct |
50 ms |
14532 KB |
Output is correct |
56 |
Correct |
542 ms |
72608 KB |
Output is correct |
57 |
Correct |
540 ms |
73380 KB |
Output is correct |
58 |
Correct |
523 ms |
70572 KB |
Output is correct |
59 |
Correct |
531 ms |
71092 KB |
Output is correct |
60 |
Correct |
572 ms |
75308 KB |
Output is correct |