#include <bits/stdc++.h>
#define int long long
using namespace std;
int res = 0;
struct dsu{
vector<int> D,dep;
void init(int N){
D.resize(N+1);
dep.resize(N+1);
for(int i = 0; i<=N;++i){
D[i] = i; dep[i] = 1;
}
}
int finden(int x){
return D[x] = (D[x] == x ? x : finden(D[x]));
}
void unite(int a, int b){
a = finden(a); b = finden(b);
if(a == b) return;
res += dep[a]*dep[b];
D[a] = b; dep[b] += dep[a];
}
}DSU;
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,Q; cin >> N >> Q;
vector<int> height(N),ans(Q);
for(int i = 0;i<N;++i) cin >> height[i];
DSU.init(N);
vector<pair<int,int>> queries, edges;
for(int i = 0;i<Q;++i){
int x; cin >> x;
queries.push_back({x,i});
}
for(int i = 0;i<N;++i) edges.push_back({height[i],i});
sort(queries.begin(),queries.end()); sort(edges.begin(),edges.end());
int pos = 0;
for(int i = 0;i<Q;++i){
while(pos < N && queries[i].first >= edges[pos].first){
DSU.unite(edges[pos].second,edges[pos].second+1);
pos++;
}
ans[queries[i].second] = res;
}
for(int i = 0;i<Q;++i) cout << ans[i] << " ";
cout << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
4820 KB |
Output is correct |
2 |
Correct |
19 ms |
5588 KB |
Output is correct |
3 |
Correct |
17 ms |
5332 KB |
Output is correct |
4 |
Correct |
19 ms |
5588 KB |
Output is correct |
5 |
Correct |
17 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
7888 KB |
Output is correct |
2 |
Correct |
31 ms |
9024 KB |
Output is correct |
3 |
Correct |
31 ms |
8916 KB |
Output is correct |
4 |
Correct |
32 ms |
8908 KB |
Output is correct |
5 |
Correct |
31 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
7932 KB |
Output is correct |
2 |
Correct |
35 ms |
9160 KB |
Output is correct |
3 |
Correct |
33 ms |
9160 KB |
Output is correct |
4 |
Correct |
31 ms |
9164 KB |
Output is correct |
5 |
Correct |
38 ms |
9224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
17 ms |
4820 KB |
Output is correct |
12 |
Correct |
19 ms |
5588 KB |
Output is correct |
13 |
Correct |
17 ms |
5332 KB |
Output is correct |
14 |
Correct |
19 ms |
5588 KB |
Output is correct |
15 |
Correct |
17 ms |
5332 KB |
Output is correct |
16 |
Correct |
17 ms |
5332 KB |
Output is correct |
17 |
Correct |
16 ms |
5588 KB |
Output is correct |
18 |
Correct |
17 ms |
5588 KB |
Output is correct |
19 |
Correct |
15 ms |
5332 KB |
Output is correct |
20 |
Correct |
16 ms |
5588 KB |
Output is correct |
21 |
Correct |
17 ms |
5332 KB |
Output is correct |
22 |
Correct |
15 ms |
5332 KB |
Output is correct |
23 |
Correct |
17 ms |
5592 KB |
Output is correct |
24 |
Correct |
15 ms |
5336 KB |
Output is correct |
25 |
Correct |
17 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
17 ms |
4820 KB |
Output is correct |
27 |
Correct |
19 ms |
5588 KB |
Output is correct |
28 |
Correct |
17 ms |
5332 KB |
Output is correct |
29 |
Correct |
19 ms |
5588 KB |
Output is correct |
30 |
Correct |
17 ms |
5332 KB |
Output is correct |
31 |
Correct |
31 ms |
7888 KB |
Output is correct |
32 |
Correct |
31 ms |
9024 KB |
Output is correct |
33 |
Correct |
31 ms |
8916 KB |
Output is correct |
34 |
Correct |
32 ms |
8908 KB |
Output is correct |
35 |
Correct |
31 ms |
8916 KB |
Output is correct |
36 |
Correct |
31 ms |
7932 KB |
Output is correct |
37 |
Correct |
35 ms |
9160 KB |
Output is correct |
38 |
Correct |
33 ms |
9160 KB |
Output is correct |
39 |
Correct |
31 ms |
9164 KB |
Output is correct |
40 |
Correct |
38 ms |
9224 KB |
Output is correct |
41 |
Correct |
17 ms |
5332 KB |
Output is correct |
42 |
Correct |
16 ms |
5588 KB |
Output is correct |
43 |
Correct |
17 ms |
5588 KB |
Output is correct |
44 |
Correct |
15 ms |
5332 KB |
Output is correct |
45 |
Correct |
16 ms |
5588 KB |
Output is correct |
46 |
Correct |
17 ms |
5332 KB |
Output is correct |
47 |
Correct |
15 ms |
5332 KB |
Output is correct |
48 |
Correct |
17 ms |
5592 KB |
Output is correct |
49 |
Correct |
15 ms |
5336 KB |
Output is correct |
50 |
Correct |
17 ms |
5332 KB |
Output is correct |
51 |
Correct |
40 ms |
9168 KB |
Output is correct |
52 |
Correct |
38 ms |
9156 KB |
Output is correct |
53 |
Correct |
38 ms |
9172 KB |
Output is correct |
54 |
Correct |
39 ms |
9096 KB |
Output is correct |
55 |
Correct |
43 ms |
9076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
548 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
17 ms |
4820 KB |
Output is correct |
27 |
Correct |
19 ms |
5588 KB |
Output is correct |
28 |
Correct |
17 ms |
5332 KB |
Output is correct |
29 |
Correct |
19 ms |
5588 KB |
Output is correct |
30 |
Correct |
17 ms |
5332 KB |
Output is correct |
31 |
Correct |
31 ms |
7888 KB |
Output is correct |
32 |
Correct |
31 ms |
9024 KB |
Output is correct |
33 |
Correct |
31 ms |
8916 KB |
Output is correct |
34 |
Correct |
32 ms |
8908 KB |
Output is correct |
35 |
Correct |
31 ms |
8916 KB |
Output is correct |
36 |
Correct |
31 ms |
7932 KB |
Output is correct |
37 |
Correct |
35 ms |
9160 KB |
Output is correct |
38 |
Correct |
33 ms |
9160 KB |
Output is correct |
39 |
Correct |
31 ms |
9164 KB |
Output is correct |
40 |
Correct |
38 ms |
9224 KB |
Output is correct |
41 |
Correct |
17 ms |
5332 KB |
Output is correct |
42 |
Correct |
16 ms |
5588 KB |
Output is correct |
43 |
Correct |
17 ms |
5588 KB |
Output is correct |
44 |
Correct |
15 ms |
5332 KB |
Output is correct |
45 |
Correct |
16 ms |
5588 KB |
Output is correct |
46 |
Correct |
17 ms |
5332 KB |
Output is correct |
47 |
Correct |
15 ms |
5332 KB |
Output is correct |
48 |
Correct |
17 ms |
5592 KB |
Output is correct |
49 |
Correct |
15 ms |
5336 KB |
Output is correct |
50 |
Correct |
17 ms |
5332 KB |
Output is correct |
51 |
Correct |
40 ms |
9168 KB |
Output is correct |
52 |
Correct |
38 ms |
9156 KB |
Output is correct |
53 |
Correct |
38 ms |
9172 KB |
Output is correct |
54 |
Correct |
39 ms |
9096 KB |
Output is correct |
55 |
Correct |
43 ms |
9076 KB |
Output is correct |
56 |
Correct |
457 ms |
83204 KB |
Output is correct |
57 |
Correct |
447 ms |
84144 KB |
Output is correct |
58 |
Correct |
409 ms |
82352 KB |
Output is correct |
59 |
Correct |
437 ms |
84748 KB |
Output is correct |
60 |
Correct |
447 ms |
85680 KB |
Output is correct |