#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
using ll = long long;
using pii = pair<int, int>;
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;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q, mn = 1e9;
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;
mn = min(mn, x);
qus.push_back({ 3, i, i, x });
}
sort(qus.begin(), qus.end());
vector<ll> ans(q);
ll res = 0;
int ptr = 0;
init(n);
for(Query &qu : qus) {
if(qu.t == 1) {
res++;
} else if(qu.t == 2) {
if(sameSet(qu.a, qu.b)) continue;
res -= C(getSize(qu.a));
res -= 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;
}
Compilation message
pilot.cpp: In function 'int32_t main()':
pilot.cpp:78:9: warning: unused variable 'ptr' [-Wunused-variable]
78 | int ptr = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
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 |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
32 ms |
10704 KB |
Output is correct |
2 |
Correct |
32 ms |
10180 KB |
Output is correct |
3 |
Correct |
33 ms |
10704 KB |
Output is correct |
4 |
Correct |
29 ms |
8140 KB |
Output is correct |
5 |
Correct |
31 ms |
8656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
13504 KB |
Output is correct |
2 |
Correct |
49 ms |
13156 KB |
Output is correct |
3 |
Correct |
48 ms |
12736 KB |
Output is correct |
4 |
Correct |
50 ms |
15092 KB |
Output is correct |
5 |
Correct |
47 ms |
11960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
13588 KB |
Output is correct |
2 |
Correct |
49 ms |
11196 KB |
Output is correct |
3 |
Correct |
49 ms |
11452 KB |
Output is correct |
4 |
Correct |
48 ms |
13236 KB |
Output is correct |
5 |
Correct |
52 ms |
13756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
32 ms |
10704 KB |
Output is correct |
12 |
Correct |
32 ms |
10180 KB |
Output is correct |
13 |
Correct |
33 ms |
10704 KB |
Output is correct |
14 |
Correct |
29 ms |
8140 KB |
Output is correct |
15 |
Correct |
31 ms |
8656 KB |
Output is correct |
16 |
Correct |
30 ms |
8384 KB |
Output is correct |
17 |
Correct |
31 ms |
10192 KB |
Output is correct |
18 |
Correct |
33 ms |
10376 KB |
Output is correct |
19 |
Correct |
30 ms |
7880 KB |
Output is correct |
20 |
Correct |
31 ms |
10436 KB |
Output is correct |
21 |
Correct |
29 ms |
7880 KB |
Output is correct |
22 |
Correct |
29 ms |
8368 KB |
Output is correct |
23 |
Correct |
35 ms |
10436 KB |
Output is correct |
24 |
Correct |
29 ms |
7888 KB |
Output is correct |
25 |
Correct |
32 ms |
10940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
32 ms |
10704 KB |
Output is correct |
27 |
Correct |
32 ms |
10180 KB |
Output is correct |
28 |
Correct |
33 ms |
10704 KB |
Output is correct |
29 |
Correct |
29 ms |
8140 KB |
Output is correct |
30 |
Correct |
31 ms |
8656 KB |
Output is correct |
31 |
Correct |
48 ms |
13504 KB |
Output is correct |
32 |
Correct |
49 ms |
13156 KB |
Output is correct |
33 |
Correct |
48 ms |
12736 KB |
Output is correct |
34 |
Correct |
50 ms |
15092 KB |
Output is correct |
35 |
Correct |
47 ms |
11960 KB |
Output is correct |
36 |
Correct |
48 ms |
13588 KB |
Output is correct |
37 |
Correct |
49 ms |
11196 KB |
Output is correct |
38 |
Correct |
49 ms |
11452 KB |
Output is correct |
39 |
Correct |
48 ms |
13236 KB |
Output is correct |
40 |
Correct |
52 ms |
13756 KB |
Output is correct |
41 |
Correct |
30 ms |
8384 KB |
Output is correct |
42 |
Correct |
31 ms |
10192 KB |
Output is correct |
43 |
Correct |
33 ms |
10376 KB |
Output is correct |
44 |
Correct |
30 ms |
7880 KB |
Output is correct |
45 |
Correct |
31 ms |
10436 KB |
Output is correct |
46 |
Correct |
29 ms |
7880 KB |
Output is correct |
47 |
Correct |
29 ms |
8368 KB |
Output is correct |
48 |
Correct |
35 ms |
10436 KB |
Output is correct |
49 |
Correct |
29 ms |
7888 KB |
Output is correct |
50 |
Correct |
32 ms |
10940 KB |
Output is correct |
51 |
Correct |
55 ms |
13792 KB |
Output is correct |
52 |
Correct |
54 ms |
14528 KB |
Output is correct |
53 |
Correct |
55 ms |
14004 KB |
Output is correct |
54 |
Correct |
54 ms |
14276 KB |
Output is correct |
55 |
Correct |
57 ms |
13412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
32 ms |
10704 KB |
Output is correct |
27 |
Correct |
32 ms |
10180 KB |
Output is correct |
28 |
Correct |
33 ms |
10704 KB |
Output is correct |
29 |
Correct |
29 ms |
8140 KB |
Output is correct |
30 |
Correct |
31 ms |
8656 KB |
Output is correct |
31 |
Correct |
48 ms |
13504 KB |
Output is correct |
32 |
Correct |
49 ms |
13156 KB |
Output is correct |
33 |
Correct |
48 ms |
12736 KB |
Output is correct |
34 |
Correct |
50 ms |
15092 KB |
Output is correct |
35 |
Correct |
47 ms |
11960 KB |
Output is correct |
36 |
Correct |
48 ms |
13588 KB |
Output is correct |
37 |
Correct |
49 ms |
11196 KB |
Output is correct |
38 |
Correct |
49 ms |
11452 KB |
Output is correct |
39 |
Correct |
48 ms |
13236 KB |
Output is correct |
40 |
Correct |
52 ms |
13756 KB |
Output is correct |
41 |
Correct |
30 ms |
8384 KB |
Output is correct |
42 |
Correct |
31 ms |
10192 KB |
Output is correct |
43 |
Correct |
33 ms |
10376 KB |
Output is correct |
44 |
Correct |
30 ms |
7880 KB |
Output is correct |
45 |
Correct |
31 ms |
10436 KB |
Output is correct |
46 |
Correct |
29 ms |
7880 KB |
Output is correct |
47 |
Correct |
29 ms |
8368 KB |
Output is correct |
48 |
Correct |
35 ms |
10436 KB |
Output is correct |
49 |
Correct |
29 ms |
7888 KB |
Output is correct |
50 |
Correct |
32 ms |
10940 KB |
Output is correct |
51 |
Correct |
55 ms |
13792 KB |
Output is correct |
52 |
Correct |
54 ms |
14528 KB |
Output is correct |
53 |
Correct |
55 ms |
14004 KB |
Output is correct |
54 |
Correct |
54 ms |
14276 KB |
Output is correct |
55 |
Correct |
57 ms |
13412 KB |
Output is correct |
56 |
Correct |
612 ms |
72092 KB |
Output is correct |
57 |
Correct |
619 ms |
86448 KB |
Output is correct |
58 |
Correct |
569 ms |
81864 KB |
Output is correct |
59 |
Correct |
587 ms |
84132 KB |
Output is correct |
60 |
Correct |
628 ms |
88604 KB |
Output is correct |