#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
ll tests = 1 , curr = 0;
const int N = 1000010;
int parent[N],Size[N];
int get_parent(int u){
return ((u != parent[u]) ? parent[u] = get_parent(parent[u]) : u);
}
void union__(int u,int v){
if((u = get_parent(u)) == (v = get_parent(v))){
return ;
}
if (Size[u] < Size[v]){
swap(u , v);
}
curr += Size[u] * 1ll * Size[v];
parent[u] = v;
Size[v] += Size[u];
}
void solve(){
int n , q , z;
cin >> n >> q;
vector<pair<int,int>> tot;
for(int i = 1 ;i <= n ;i++){
cin >> z;
tot.push_back({z , -i});
}
for(int i = 1 ;i <= q ;i++){
cin >> z;
tot.push_back({z , i});
}
sort(tot.begin(),tot.end());
ll ans[q];
for(int i = 0 ;i < n + q ;i ++){
ll a = tot[i].first , b = tot[i].second;
if(b > 0){
ans[b] = curr;
}
else{
b *= -1;
curr++;
parent[b] = b;
Size[b] = 1;
if(parent[b + 1]){
union__(b , b + 1);
}
if(parent[b - 1]){
union__(b , b - 1);
}
}
}
for(int i = 1; i<= q; i++){
cout << ans[i] << endl;
}
}
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
// cin>>tests;
for(int i=0;i<tests;i++){
solve();
}
}
Compilation message
pilot.cpp: In function 'void solve()':
pilot.cpp:37:12: warning: unused variable 'a' [-Wunused-variable]
37 | ll a = tot[i].first , b = tot[i].second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1844 KB |
Output is correct |
2 |
Correct |
27 ms |
2000 KB |
Output is correct |
3 |
Correct |
28 ms |
1944 KB |
Output is correct |
4 |
Correct |
27 ms |
1768 KB |
Output is correct |
5 |
Correct |
24 ms |
1808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
4288 KB |
Output is correct |
2 |
Correct |
142 ms |
4316 KB |
Output is correct |
3 |
Correct |
142 ms |
4288 KB |
Output is correct |
4 |
Correct |
147 ms |
4540 KB |
Output is correct |
5 |
Correct |
143 ms |
4540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
4200 KB |
Output is correct |
2 |
Correct |
143 ms |
4328 KB |
Output is correct |
3 |
Correct |
145 ms |
4288 KB |
Output is correct |
4 |
Correct |
138 ms |
4284 KB |
Output is correct |
5 |
Correct |
151 ms |
4544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
25 ms |
1844 KB |
Output is correct |
12 |
Correct |
27 ms |
2000 KB |
Output is correct |
13 |
Correct |
28 ms |
1944 KB |
Output is correct |
14 |
Correct |
27 ms |
1768 KB |
Output is correct |
15 |
Correct |
24 ms |
1808 KB |
Output is correct |
16 |
Correct |
24 ms |
1996 KB |
Output is correct |
17 |
Correct |
27 ms |
1992 KB |
Output is correct |
18 |
Correct |
29 ms |
1992 KB |
Output is correct |
19 |
Correct |
25 ms |
1992 KB |
Output is correct |
20 |
Correct |
27 ms |
1996 KB |
Output is correct |
21 |
Correct |
24 ms |
1992 KB |
Output is correct |
22 |
Correct |
25 ms |
1792 KB |
Output is correct |
23 |
Correct |
26 ms |
1948 KB |
Output is correct |
24 |
Correct |
24 ms |
1996 KB |
Output is correct |
25 |
Correct |
25 ms |
2100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
25 ms |
1844 KB |
Output is correct |
27 |
Correct |
27 ms |
2000 KB |
Output is correct |
28 |
Correct |
28 ms |
1944 KB |
Output is correct |
29 |
Correct |
27 ms |
1768 KB |
Output is correct |
30 |
Correct |
24 ms |
1808 KB |
Output is correct |
31 |
Correct |
138 ms |
4288 KB |
Output is correct |
32 |
Correct |
142 ms |
4316 KB |
Output is correct |
33 |
Correct |
142 ms |
4288 KB |
Output is correct |
34 |
Correct |
147 ms |
4540 KB |
Output is correct |
35 |
Correct |
143 ms |
4540 KB |
Output is correct |
36 |
Correct |
143 ms |
4200 KB |
Output is correct |
37 |
Correct |
143 ms |
4328 KB |
Output is correct |
38 |
Correct |
145 ms |
4288 KB |
Output is correct |
39 |
Correct |
138 ms |
4284 KB |
Output is correct |
40 |
Correct |
151 ms |
4544 KB |
Output is correct |
41 |
Correct |
24 ms |
1996 KB |
Output is correct |
42 |
Correct |
27 ms |
1992 KB |
Output is correct |
43 |
Correct |
29 ms |
1992 KB |
Output is correct |
44 |
Correct |
25 ms |
1992 KB |
Output is correct |
45 |
Correct |
27 ms |
1996 KB |
Output is correct |
46 |
Correct |
24 ms |
1992 KB |
Output is correct |
47 |
Correct |
25 ms |
1792 KB |
Output is correct |
48 |
Correct |
26 ms |
1948 KB |
Output is correct |
49 |
Correct |
24 ms |
1996 KB |
Output is correct |
50 |
Correct |
25 ms |
2100 KB |
Output is correct |
51 |
Correct |
146 ms |
4420 KB |
Output is correct |
52 |
Correct |
150 ms |
4200 KB |
Output is correct |
53 |
Correct |
150 ms |
4332 KB |
Output is correct |
54 |
Correct |
154 ms |
4196 KB |
Output is correct |
55 |
Correct |
157 ms |
4284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
25 ms |
1844 KB |
Output is correct |
27 |
Correct |
27 ms |
2000 KB |
Output is correct |
28 |
Correct |
28 ms |
1944 KB |
Output is correct |
29 |
Correct |
27 ms |
1768 KB |
Output is correct |
30 |
Correct |
24 ms |
1808 KB |
Output is correct |
31 |
Correct |
138 ms |
4288 KB |
Output is correct |
32 |
Correct |
142 ms |
4316 KB |
Output is correct |
33 |
Correct |
142 ms |
4288 KB |
Output is correct |
34 |
Correct |
147 ms |
4540 KB |
Output is correct |
35 |
Correct |
143 ms |
4540 KB |
Output is correct |
36 |
Correct |
143 ms |
4200 KB |
Output is correct |
37 |
Correct |
143 ms |
4328 KB |
Output is correct |
38 |
Correct |
145 ms |
4288 KB |
Output is correct |
39 |
Correct |
138 ms |
4284 KB |
Output is correct |
40 |
Correct |
151 ms |
4544 KB |
Output is correct |
41 |
Correct |
24 ms |
1996 KB |
Output is correct |
42 |
Correct |
27 ms |
1992 KB |
Output is correct |
43 |
Correct |
29 ms |
1992 KB |
Output is correct |
44 |
Correct |
25 ms |
1992 KB |
Output is correct |
45 |
Correct |
27 ms |
1996 KB |
Output is correct |
46 |
Correct |
24 ms |
1992 KB |
Output is correct |
47 |
Correct |
25 ms |
1792 KB |
Output is correct |
48 |
Correct |
26 ms |
1948 KB |
Output is correct |
49 |
Correct |
24 ms |
1996 KB |
Output is correct |
50 |
Correct |
25 ms |
2100 KB |
Output is correct |
51 |
Correct |
146 ms |
4420 KB |
Output is correct |
52 |
Correct |
150 ms |
4200 KB |
Output is correct |
53 |
Correct |
150 ms |
4332 KB |
Output is correct |
54 |
Correct |
154 ms |
4196 KB |
Output is correct |
55 |
Correct |
157 ms |
4284 KB |
Output is correct |
56 |
Execution timed out |
1024 ms |
34072 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |