#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
int res = 0;
struct dsu{
vector<int> D, sz;
void init(int N){
D.resize(N+1); sz.resize(N+1);
for(int i = 0;i<=N;++i){
D[i] = i;
sz[i] = 1;
}
}
int finden(int x){
return D[x] == x ? x : finden(D[x]);
}
void unite(int a, int b){
a = finden(a); b = finden(b);
if(a == b) return;
res += sz[a]*sz[b];
D[a] = b; sz[b] += sz[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> heights(N),ans(Q); DSU.init(N);
for(int i = 0;i<N;++i) cin >> heights[i];
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({heights[i],i});
sort(queries.begin(),queries.end()); sort(edges.begin(),edges.end());
int j = 0;
for(int i = 0;i<Q;++i){
while(j < N && edges[j].f <= queries[i].f){
DSU.unite(edges[j].s,edges[j].s+1);
j++;
}
ans[queries[i].s] = res;
}
for(int i = 0;i<Q;++i) cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
5284 KB |
Output is correct |
2 |
Correct |
19 ms |
5588 KB |
Output is correct |
3 |
Correct |
20 ms |
5336 KB |
Output is correct |
4 |
Correct |
15 ms |
5336 KB |
Output is correct |
5 |
Correct |
16 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
8984 KB |
Output is correct |
2 |
Correct |
30 ms |
8916 KB |
Output is correct |
3 |
Correct |
29 ms |
8916 KB |
Output is correct |
4 |
Correct |
31 ms |
8948 KB |
Output is correct |
5 |
Correct |
31 ms |
8912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
9172 KB |
Output is correct |
2 |
Correct |
31 ms |
9172 KB |
Output is correct |
3 |
Correct |
30 ms |
9164 KB |
Output is correct |
4 |
Correct |
31 ms |
9172 KB |
Output is correct |
5 |
Correct |
32 ms |
9160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
19 ms |
5284 KB |
Output is correct |
12 |
Correct |
19 ms |
5588 KB |
Output is correct |
13 |
Correct |
20 ms |
5336 KB |
Output is correct |
14 |
Correct |
15 ms |
5336 KB |
Output is correct |
15 |
Correct |
16 ms |
5332 KB |
Output is correct |
16 |
Correct |
15 ms |
5336 KB |
Output is correct |
17 |
Correct |
16 ms |
5588 KB |
Output is correct |
18 |
Correct |
16 ms |
5744 KB |
Output is correct |
19 |
Correct |
16 ms |
5332 KB |
Output is correct |
20 |
Correct |
17 ms |
5464 KB |
Output is correct |
21 |
Correct |
16 ms |
5336 KB |
Output is correct |
22 |
Correct |
16 ms |
5336 KB |
Output is correct |
23 |
Correct |
16 ms |
5588 KB |
Output is correct |
24 |
Correct |
15 ms |
5332 KB |
Output is correct |
25 |
Correct |
16 ms |
5268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
19 ms |
5284 KB |
Output is correct |
27 |
Correct |
19 ms |
5588 KB |
Output is correct |
28 |
Correct |
20 ms |
5336 KB |
Output is correct |
29 |
Correct |
15 ms |
5336 KB |
Output is correct |
30 |
Correct |
16 ms |
5332 KB |
Output is correct |
31 |
Correct |
29 ms |
8984 KB |
Output is correct |
32 |
Correct |
30 ms |
8916 KB |
Output is correct |
33 |
Correct |
29 ms |
8916 KB |
Output is correct |
34 |
Correct |
31 ms |
8948 KB |
Output is correct |
35 |
Correct |
31 ms |
8912 KB |
Output is correct |
36 |
Correct |
30 ms |
9172 KB |
Output is correct |
37 |
Correct |
31 ms |
9172 KB |
Output is correct |
38 |
Correct |
30 ms |
9164 KB |
Output is correct |
39 |
Correct |
31 ms |
9172 KB |
Output is correct |
40 |
Correct |
32 ms |
9160 KB |
Output is correct |
41 |
Correct |
15 ms |
5336 KB |
Output is correct |
42 |
Correct |
16 ms |
5588 KB |
Output is correct |
43 |
Correct |
16 ms |
5744 KB |
Output is correct |
44 |
Correct |
16 ms |
5332 KB |
Output is correct |
45 |
Correct |
17 ms |
5464 KB |
Output is correct |
46 |
Correct |
16 ms |
5336 KB |
Output is correct |
47 |
Correct |
16 ms |
5336 KB |
Output is correct |
48 |
Correct |
16 ms |
5588 KB |
Output is correct |
49 |
Correct |
15 ms |
5332 KB |
Output is correct |
50 |
Correct |
16 ms |
5268 KB |
Output is correct |
51 |
Correct |
38 ms |
9036 KB |
Output is correct |
52 |
Correct |
38 ms |
9168 KB |
Output is correct |
53 |
Correct |
37 ms |
9172 KB |
Output is correct |
54 |
Correct |
36 ms |
9184 KB |
Output is correct |
55 |
Correct |
36 ms |
8992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
19 ms |
5284 KB |
Output is correct |
27 |
Correct |
19 ms |
5588 KB |
Output is correct |
28 |
Correct |
20 ms |
5336 KB |
Output is correct |
29 |
Correct |
15 ms |
5336 KB |
Output is correct |
30 |
Correct |
16 ms |
5332 KB |
Output is correct |
31 |
Correct |
29 ms |
8984 KB |
Output is correct |
32 |
Correct |
30 ms |
8916 KB |
Output is correct |
33 |
Correct |
29 ms |
8916 KB |
Output is correct |
34 |
Correct |
31 ms |
8948 KB |
Output is correct |
35 |
Correct |
31 ms |
8912 KB |
Output is correct |
36 |
Correct |
30 ms |
9172 KB |
Output is correct |
37 |
Correct |
31 ms |
9172 KB |
Output is correct |
38 |
Correct |
30 ms |
9164 KB |
Output is correct |
39 |
Correct |
31 ms |
9172 KB |
Output is correct |
40 |
Correct |
32 ms |
9160 KB |
Output is correct |
41 |
Correct |
15 ms |
5336 KB |
Output is correct |
42 |
Correct |
16 ms |
5588 KB |
Output is correct |
43 |
Correct |
16 ms |
5744 KB |
Output is correct |
44 |
Correct |
16 ms |
5332 KB |
Output is correct |
45 |
Correct |
17 ms |
5464 KB |
Output is correct |
46 |
Correct |
16 ms |
5336 KB |
Output is correct |
47 |
Correct |
16 ms |
5336 KB |
Output is correct |
48 |
Correct |
16 ms |
5588 KB |
Output is correct |
49 |
Correct |
15 ms |
5332 KB |
Output is correct |
50 |
Correct |
16 ms |
5268 KB |
Output is correct |
51 |
Correct |
38 ms |
9036 KB |
Output is correct |
52 |
Correct |
38 ms |
9168 KB |
Output is correct |
53 |
Correct |
37 ms |
9172 KB |
Output is correct |
54 |
Correct |
36 ms |
9184 KB |
Output is correct |
55 |
Correct |
36 ms |
8992 KB |
Output is correct |
56 |
Correct |
418 ms |
84700 KB |
Output is correct |
57 |
Correct |
432 ms |
84652 KB |
Output is correct |
58 |
Correct |
387 ms |
82352 KB |
Output is correct |
59 |
Correct |
393 ms |
82356 KB |
Output is correct |
60 |
Correct |
445 ms |
86552 KB |
Output is correct |