#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;
struct DSU {
int n, comp;
vector<int> par, size;
void config(int _n) {
n = _n + 1;
par.resize(n+1);
size.resize(n+1, 1);
for(int i=1; i<=n; i++) par[i] = i;
}
int get(int u) {
if(u == par[u]) return u;
return par[u] = get(par[u]);
}
bool uni(int u, int v) {
u = get(u), v = get(v);
if(u == v) return false;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
par[v] = u;
return true;
}
int getSize(int u) { return size[get(u)]; }
bool sameSet(int u, int v) { return get(u) == get(v); }
};
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;
DSU dsu; dsu.config(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(dsu.sameSet(qu.a, qu.b)) continue;
res -= C(dsu.getSize(qu.a));
res -= C(dsu.getSize(qu.b));
dsu.uni(qu.a, qu.b);
res += C(dsu.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:94:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | while(ptr < comp.size() && comp[ptr] <= qu.w) {
| ~~~~^~~~~~~~~~~~~
pilot.cpp:95:22: warning: unused variable 'p' [-Wunused-variable]
95 | for(int &p : mp[comp[ptr]]) res++;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23900 KB |
Output is correct |
12 |
Correct |
6 ms |
23944 KB |
Output is correct |
13 |
Correct |
6 ms |
23900 KB |
Output is correct |
14 |
Correct |
6 ms |
23940 KB |
Output is correct |
15 |
Correct |
5 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23900 KB |
Output is correct |
12 |
Correct |
6 ms |
23944 KB |
Output is correct |
13 |
Correct |
6 ms |
23900 KB |
Output is correct |
14 |
Correct |
6 ms |
23940 KB |
Output is correct |
15 |
Correct |
5 ms |
23900 KB |
Output is correct |
16 |
Correct |
5 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23900 KB |
Output is correct |
19 |
Correct |
6 ms |
23900 KB |
Output is correct |
20 |
Correct |
5 ms |
23964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23900 KB |
Output is correct |
12 |
Correct |
6 ms |
23944 KB |
Output is correct |
13 |
Correct |
6 ms |
23900 KB |
Output is correct |
14 |
Correct |
6 ms |
23940 KB |
Output is correct |
15 |
Correct |
5 ms |
23900 KB |
Output is correct |
16 |
Correct |
5 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23900 KB |
Output is correct |
19 |
Correct |
6 ms |
23900 KB |
Output is correct |
20 |
Correct |
5 ms |
23964 KB |
Output is correct |
21 |
Correct |
6 ms |
23900 KB |
Output is correct |
22 |
Correct |
6 ms |
23900 KB |
Output is correct |
23 |
Correct |
6 ms |
23896 KB |
Output is correct |
24 |
Correct |
6 ms |
23876 KB |
Output is correct |
25 |
Correct |
6 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
31944 KB |
Output is correct |
2 |
Correct |
71 ms |
34860 KB |
Output is correct |
3 |
Correct |
48 ms |
31688 KB |
Output is correct |
4 |
Correct |
66 ms |
33996 KB |
Output is correct |
5 |
Correct |
55 ms |
31692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
38852 KB |
Output is correct |
2 |
Correct |
64 ms |
38932 KB |
Output is correct |
3 |
Correct |
69 ms |
39112 KB |
Output is correct |
4 |
Correct |
62 ms |
38848 KB |
Output is correct |
5 |
Correct |
61 ms |
38112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
39476 KB |
Output is correct |
2 |
Correct |
71 ms |
38340 KB |
Output is correct |
3 |
Correct |
77 ms |
39252 KB |
Output is correct |
4 |
Correct |
73 ms |
39364 KB |
Output is correct |
5 |
Correct |
74 ms |
38852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
11 |
Correct |
52 ms |
31944 KB |
Output is correct |
12 |
Correct |
71 ms |
34860 KB |
Output is correct |
13 |
Correct |
48 ms |
31688 KB |
Output is correct |
14 |
Correct |
66 ms |
33996 KB |
Output is correct |
15 |
Correct |
55 ms |
31692 KB |
Output is correct |
16 |
Correct |
49 ms |
31692 KB |
Output is correct |
17 |
Correct |
81 ms |
34912 KB |
Output is correct |
18 |
Correct |
73 ms |
34856 KB |
Output is correct |
19 |
Correct |
48 ms |
31688 KB |
Output is correct |
20 |
Correct |
69 ms |
34760 KB |
Output is correct |
21 |
Correct |
50 ms |
31676 KB |
Output is correct |
22 |
Correct |
67 ms |
34252 KB |
Output is correct |
23 |
Correct |
52 ms |
32204 KB |
Output is correct |
24 |
Correct |
64 ms |
33996 KB |
Output is correct |
25 |
Correct |
50 ms |
32004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23900 KB |
Output is correct |
12 |
Correct |
6 ms |
23944 KB |
Output is correct |
13 |
Correct |
6 ms |
23900 KB |
Output is correct |
14 |
Correct |
6 ms |
23940 KB |
Output is correct |
15 |
Correct |
5 ms |
23900 KB |
Output is correct |
16 |
Correct |
5 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23900 KB |
Output is correct |
19 |
Correct |
6 ms |
23900 KB |
Output is correct |
20 |
Correct |
5 ms |
23964 KB |
Output is correct |
21 |
Correct |
6 ms |
23900 KB |
Output is correct |
22 |
Correct |
6 ms |
23900 KB |
Output is correct |
23 |
Correct |
6 ms |
23896 KB |
Output is correct |
24 |
Correct |
6 ms |
23876 KB |
Output is correct |
25 |
Correct |
6 ms |
24056 KB |
Output is correct |
26 |
Correct |
52 ms |
31944 KB |
Output is correct |
27 |
Correct |
71 ms |
34860 KB |
Output is correct |
28 |
Correct |
48 ms |
31688 KB |
Output is correct |
29 |
Correct |
66 ms |
33996 KB |
Output is correct |
30 |
Correct |
55 ms |
31692 KB |
Output is correct |
31 |
Correct |
63 ms |
38852 KB |
Output is correct |
32 |
Correct |
64 ms |
38932 KB |
Output is correct |
33 |
Correct |
69 ms |
39112 KB |
Output is correct |
34 |
Correct |
62 ms |
38848 KB |
Output is correct |
35 |
Correct |
61 ms |
38112 KB |
Output is correct |
36 |
Correct |
79 ms |
39476 KB |
Output is correct |
37 |
Correct |
71 ms |
38340 KB |
Output is correct |
38 |
Correct |
77 ms |
39252 KB |
Output is correct |
39 |
Correct |
73 ms |
39364 KB |
Output is correct |
40 |
Correct |
74 ms |
38852 KB |
Output is correct |
41 |
Correct |
49 ms |
31692 KB |
Output is correct |
42 |
Correct |
81 ms |
34912 KB |
Output is correct |
43 |
Correct |
73 ms |
34856 KB |
Output is correct |
44 |
Correct |
48 ms |
31688 KB |
Output is correct |
45 |
Correct |
69 ms |
34760 KB |
Output is correct |
46 |
Correct |
50 ms |
31676 KB |
Output is correct |
47 |
Correct |
67 ms |
34252 KB |
Output is correct |
48 |
Correct |
52 ms |
32204 KB |
Output is correct |
49 |
Correct |
64 ms |
33996 KB |
Output is correct |
50 |
Correct |
50 ms |
32004 KB |
Output is correct |
51 |
Correct |
78 ms |
36280 KB |
Output is correct |
52 |
Correct |
95 ms |
38848 KB |
Output is correct |
53 |
Correct |
75 ms |
36332 KB |
Output is correct |
54 |
Correct |
94 ms |
38860 KB |
Output is correct |
55 |
Correct |
78 ms |
35820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23896 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
6 ms |
23900 KB |
Output is correct |
5 |
Correct |
6 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
5 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
5 ms |
23900 KB |
Output is correct |
10 |
Correct |
5 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23900 KB |
Output is correct |
12 |
Correct |
6 ms |
23944 KB |
Output is correct |
13 |
Correct |
6 ms |
23900 KB |
Output is correct |
14 |
Correct |
6 ms |
23940 KB |
Output is correct |
15 |
Correct |
5 ms |
23900 KB |
Output is correct |
16 |
Correct |
5 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23900 KB |
Output is correct |
19 |
Correct |
6 ms |
23900 KB |
Output is correct |
20 |
Correct |
5 ms |
23964 KB |
Output is correct |
21 |
Correct |
6 ms |
23900 KB |
Output is correct |
22 |
Correct |
6 ms |
23900 KB |
Output is correct |
23 |
Correct |
6 ms |
23896 KB |
Output is correct |
24 |
Correct |
6 ms |
23876 KB |
Output is correct |
25 |
Correct |
6 ms |
24056 KB |
Output is correct |
26 |
Correct |
52 ms |
31944 KB |
Output is correct |
27 |
Correct |
71 ms |
34860 KB |
Output is correct |
28 |
Correct |
48 ms |
31688 KB |
Output is correct |
29 |
Correct |
66 ms |
33996 KB |
Output is correct |
30 |
Correct |
55 ms |
31692 KB |
Output is correct |
31 |
Correct |
63 ms |
38852 KB |
Output is correct |
32 |
Correct |
64 ms |
38932 KB |
Output is correct |
33 |
Correct |
69 ms |
39112 KB |
Output is correct |
34 |
Correct |
62 ms |
38848 KB |
Output is correct |
35 |
Correct |
61 ms |
38112 KB |
Output is correct |
36 |
Correct |
79 ms |
39476 KB |
Output is correct |
37 |
Correct |
71 ms |
38340 KB |
Output is correct |
38 |
Correct |
77 ms |
39252 KB |
Output is correct |
39 |
Correct |
73 ms |
39364 KB |
Output is correct |
40 |
Correct |
74 ms |
38852 KB |
Output is correct |
41 |
Correct |
49 ms |
31692 KB |
Output is correct |
42 |
Correct |
81 ms |
34912 KB |
Output is correct |
43 |
Correct |
73 ms |
34856 KB |
Output is correct |
44 |
Correct |
48 ms |
31688 KB |
Output is correct |
45 |
Correct |
69 ms |
34760 KB |
Output is correct |
46 |
Correct |
50 ms |
31676 KB |
Output is correct |
47 |
Correct |
67 ms |
34252 KB |
Output is correct |
48 |
Correct |
52 ms |
32204 KB |
Output is correct |
49 |
Correct |
64 ms |
33996 KB |
Output is correct |
50 |
Correct |
50 ms |
32004 KB |
Output is correct |
51 |
Correct |
78 ms |
36280 KB |
Output is correct |
52 |
Correct |
95 ms |
38848 KB |
Output is correct |
53 |
Correct |
75 ms |
36332 KB |
Output is correct |
54 |
Correct |
94 ms |
38860 KB |
Output is correct |
55 |
Correct |
78 ms |
35820 KB |
Output is correct |
56 |
Execution timed out |
1022 ms |
136192 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |