#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;
}
vector<int> mp[maxn+1];
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({ 1, i-1, i, max(v[i], v[i-1]) });
for(int i=0; i<q; i++) {
int x;
cin >> x;
mn = min(mn, x);
qus.push_back({ 2, i, i, x });
}
sort(qus.begin(), qus.end());
vector<ll> ans(q);
ll res = 0;
for(int i=0; i<n; i++) mp[v[i]].push_back(i);
set<int> s;
for(int &x : v) s.insert(x);
vector<int> comp(s.begin(), s.end());
int ptr = 0;
init(n);
for(Query &qu : qus) {
while(ptr < comp.size() && comp[ptr] <= qu.w) {
for(int &p : mp[comp[ptr]]) res++;
ptr++;
}
if(qu.t == 1) {
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:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | while(ptr < comp.size() && comp[ptr] <= qu.w) {
| ~~~~^~~~~~~~~~~~~
pilot.cpp:89:22: warning: unused variable 'p' [-Wunused-variable]
89 | for(int &p : mp[comp[ptr]]) res++;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27228 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
6 ms |
27224 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27228 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
6 ms |
27224 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27228 KB |
Output is correct |
18 |
Correct |
6 ms |
27332 KB |
Output is correct |
19 |
Correct |
7 ms |
27224 KB |
Output is correct |
20 |
Correct |
6 ms |
27244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27228 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
6 ms |
27224 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27228 KB |
Output is correct |
18 |
Correct |
6 ms |
27332 KB |
Output is correct |
19 |
Correct |
7 ms |
27224 KB |
Output is correct |
20 |
Correct |
6 ms |
27244 KB |
Output is correct |
21 |
Correct |
6 ms |
27224 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
6 ms |
27412 KB |
Output is correct |
24 |
Correct |
7 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
33996 KB |
Output is correct |
2 |
Correct |
73 ms |
36812 KB |
Output is correct |
3 |
Correct |
52 ms |
34196 KB |
Output is correct |
4 |
Correct |
67 ms |
36044 KB |
Output is correct |
5 |
Correct |
50 ms |
33740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
39804 KB |
Output is correct |
2 |
Correct |
63 ms |
40384 KB |
Output is correct |
3 |
Correct |
63 ms |
40132 KB |
Output is correct |
4 |
Correct |
60 ms |
40536 KB |
Output is correct |
5 |
Correct |
60 ms |
39560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
40704 KB |
Output is correct |
2 |
Correct |
69 ms |
40128 KB |
Output is correct |
3 |
Correct |
69 ms |
39876 KB |
Output is correct |
4 |
Correct |
70 ms |
40896 KB |
Output is correct |
5 |
Correct |
76 ms |
40684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
63 ms |
33996 KB |
Output is correct |
12 |
Correct |
73 ms |
36812 KB |
Output is correct |
13 |
Correct |
52 ms |
34196 KB |
Output is correct |
14 |
Correct |
67 ms |
36044 KB |
Output is correct |
15 |
Correct |
50 ms |
33740 KB |
Output is correct |
16 |
Correct |
51 ms |
33684 KB |
Output is correct |
17 |
Correct |
76 ms |
36808 KB |
Output is correct |
18 |
Correct |
77 ms |
36816 KB |
Output is correct |
19 |
Correct |
51 ms |
33736 KB |
Output is correct |
20 |
Correct |
71 ms |
36552 KB |
Output is correct |
21 |
Correct |
51 ms |
33788 KB |
Output is correct |
22 |
Correct |
68 ms |
36296 KB |
Output is correct |
23 |
Correct |
58 ms |
34188 KB |
Output is correct |
24 |
Correct |
65 ms |
36044 KB |
Output is correct |
25 |
Correct |
55 ms |
34004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27228 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
6 ms |
27224 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27228 KB |
Output is correct |
18 |
Correct |
6 ms |
27332 KB |
Output is correct |
19 |
Correct |
7 ms |
27224 KB |
Output is correct |
20 |
Correct |
6 ms |
27244 KB |
Output is correct |
21 |
Correct |
6 ms |
27224 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
6 ms |
27412 KB |
Output is correct |
24 |
Correct |
7 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27480 KB |
Output is correct |
26 |
Correct |
63 ms |
33996 KB |
Output is correct |
27 |
Correct |
73 ms |
36812 KB |
Output is correct |
28 |
Correct |
52 ms |
34196 KB |
Output is correct |
29 |
Correct |
67 ms |
36044 KB |
Output is correct |
30 |
Correct |
50 ms |
33740 KB |
Output is correct |
31 |
Correct |
61 ms |
39804 KB |
Output is correct |
32 |
Correct |
63 ms |
40384 KB |
Output is correct |
33 |
Correct |
63 ms |
40132 KB |
Output is correct |
34 |
Correct |
60 ms |
40536 KB |
Output is correct |
35 |
Correct |
60 ms |
39560 KB |
Output is correct |
36 |
Correct |
77 ms |
40704 KB |
Output is correct |
37 |
Correct |
69 ms |
40128 KB |
Output is correct |
38 |
Correct |
69 ms |
39876 KB |
Output is correct |
39 |
Correct |
70 ms |
40896 KB |
Output is correct |
40 |
Correct |
76 ms |
40684 KB |
Output is correct |
41 |
Correct |
51 ms |
33684 KB |
Output is correct |
42 |
Correct |
76 ms |
36808 KB |
Output is correct |
43 |
Correct |
77 ms |
36816 KB |
Output is correct |
44 |
Correct |
51 ms |
33736 KB |
Output is correct |
45 |
Correct |
71 ms |
36552 KB |
Output is correct |
46 |
Correct |
51 ms |
33788 KB |
Output is correct |
47 |
Correct |
68 ms |
36296 KB |
Output is correct |
48 |
Correct |
58 ms |
34188 KB |
Output is correct |
49 |
Correct |
65 ms |
36044 KB |
Output is correct |
50 |
Correct |
55 ms |
34004 KB |
Output is correct |
51 |
Correct |
82 ms |
37824 KB |
Output is correct |
52 |
Correct |
97 ms |
39876 KB |
Output is correct |
53 |
Correct |
78 ms |
37436 KB |
Output is correct |
54 |
Correct |
99 ms |
39872 KB |
Output is correct |
55 |
Correct |
79 ms |
37440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27228 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
6 ms |
27224 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
7 ms |
27228 KB |
Output is correct |
18 |
Correct |
6 ms |
27332 KB |
Output is correct |
19 |
Correct |
7 ms |
27224 KB |
Output is correct |
20 |
Correct |
6 ms |
27244 KB |
Output is correct |
21 |
Correct |
6 ms |
27224 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
6 ms |
27412 KB |
Output is correct |
24 |
Correct |
7 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27480 KB |
Output is correct |
26 |
Correct |
63 ms |
33996 KB |
Output is correct |
27 |
Correct |
73 ms |
36812 KB |
Output is correct |
28 |
Correct |
52 ms |
34196 KB |
Output is correct |
29 |
Correct |
67 ms |
36044 KB |
Output is correct |
30 |
Correct |
50 ms |
33740 KB |
Output is correct |
31 |
Correct |
61 ms |
39804 KB |
Output is correct |
32 |
Correct |
63 ms |
40384 KB |
Output is correct |
33 |
Correct |
63 ms |
40132 KB |
Output is correct |
34 |
Correct |
60 ms |
40536 KB |
Output is correct |
35 |
Correct |
60 ms |
39560 KB |
Output is correct |
36 |
Correct |
77 ms |
40704 KB |
Output is correct |
37 |
Correct |
69 ms |
40128 KB |
Output is correct |
38 |
Correct |
69 ms |
39876 KB |
Output is correct |
39 |
Correct |
70 ms |
40896 KB |
Output is correct |
40 |
Correct |
76 ms |
40684 KB |
Output is correct |
41 |
Correct |
51 ms |
33684 KB |
Output is correct |
42 |
Correct |
76 ms |
36808 KB |
Output is correct |
43 |
Correct |
77 ms |
36816 KB |
Output is correct |
44 |
Correct |
51 ms |
33736 KB |
Output is correct |
45 |
Correct |
71 ms |
36552 KB |
Output is correct |
46 |
Correct |
51 ms |
33788 KB |
Output is correct |
47 |
Correct |
68 ms |
36296 KB |
Output is correct |
48 |
Correct |
58 ms |
34188 KB |
Output is correct |
49 |
Correct |
65 ms |
36044 KB |
Output is correct |
50 |
Correct |
55 ms |
34004 KB |
Output is correct |
51 |
Correct |
82 ms |
37824 KB |
Output is correct |
52 |
Correct |
97 ms |
39876 KB |
Output is correct |
53 |
Correct |
78 ms |
37436 KB |
Output is correct |
54 |
Correct |
99 ms |
39872 KB |
Output is correct |
55 |
Correct |
79 ms |
37440 KB |
Output is correct |
56 |
Execution timed out |
1088 ms |
123056 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |