#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
vector<pair<long long, long long> > a(N);
long long ans[N];
bool IsThere[N];
long long par[N], Rank[N];
long long curans = 0;
long long Find(long long x) {
if(x != par[x]) {
par[x] = Find(par[x]);
}
return par[x];
}
void Unite(long long a, long long b) {
if(!IsThere[a] || !IsThere[b])
return;
long long roota = Find(a), rootb = Find(b);
curans -= Rank[roota] * (Rank[roota] + 1) / 2;
curans -= Rank[rootb] * (Rank[rootb] + 1) / 2;
if(Rank[roota] > Rank[rootb]) {
Rank[roota] += Rank[rootb];
par[rootb] = roota;
curans += Rank[roota] * (Rank[roota] + 1) / 2;
}
else {
Rank[rootb] += Rank[roota];
par[roota] = rootb;
curans += Rank[rootb] * (Rank[rootb] + 1) / 2;
}
}
void Solve() {
long long n, q;
cin >> n >> q;
for(long long i = 0; i < N; i++) {
IsThere[i] = false;
par[i] = i;
Rank[i] = 1;
}
for(long long i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin() + 1, a.begin() + 1 + n);
long long curpos = 1;
ans[0] = 0;
for(long long i = 1; i < N; i++) {
while(curpos <= n && a[curpos].first == i) {
IsThere[a[curpos].second] = true;
curans++;
Unite(a[curpos].second, a[curpos].second + 1);
Unite(a[curpos].second, a[curpos].second - 1);
curpos++;
}
ans[i] = curans;
}
for(long long i = 1; i <= q; i++) {
long long x;
cin >> x;
cout << ans[x] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
11 |
Correct |
23 ms |
40428 KB |
Output is correct |
12 |
Correct |
25 ms |
40428 KB |
Output is correct |
13 |
Correct |
24 ms |
40428 KB |
Output is correct |
14 |
Correct |
25 ms |
40556 KB |
Output is correct |
15 |
Correct |
24 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
11 |
Correct |
23 ms |
40428 KB |
Output is correct |
12 |
Correct |
25 ms |
40428 KB |
Output is correct |
13 |
Correct |
24 ms |
40428 KB |
Output is correct |
14 |
Correct |
25 ms |
40556 KB |
Output is correct |
15 |
Correct |
24 ms |
40428 KB |
Output is correct |
16 |
Correct |
28 ms |
40428 KB |
Output is correct |
17 |
Correct |
27 ms |
40428 KB |
Output is correct |
18 |
Correct |
29 ms |
40484 KB |
Output is correct |
19 |
Correct |
25 ms |
40428 KB |
Output is correct |
20 |
Correct |
24 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
11 |
Correct |
23 ms |
40428 KB |
Output is correct |
12 |
Correct |
25 ms |
40428 KB |
Output is correct |
13 |
Correct |
24 ms |
40428 KB |
Output is correct |
14 |
Correct |
25 ms |
40556 KB |
Output is correct |
15 |
Correct |
24 ms |
40428 KB |
Output is correct |
16 |
Correct |
28 ms |
40428 KB |
Output is correct |
17 |
Correct |
27 ms |
40428 KB |
Output is correct |
18 |
Correct |
29 ms |
40484 KB |
Output is correct |
19 |
Correct |
25 ms |
40428 KB |
Output is correct |
20 |
Correct |
24 ms |
40428 KB |
Output is correct |
21 |
Correct |
23 ms |
40428 KB |
Output is correct |
22 |
Correct |
25 ms |
40428 KB |
Output is correct |
23 |
Correct |
25 ms |
40428 KB |
Output is correct |
24 |
Correct |
26 ms |
40428 KB |
Output is correct |
25 |
Correct |
28 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
40428 KB |
Output is correct |
2 |
Correct |
50 ms |
40428 KB |
Output is correct |
3 |
Correct |
45 ms |
40428 KB |
Output is correct |
4 |
Correct |
46 ms |
40428 KB |
Output is correct |
5 |
Correct |
46 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
41472 KB |
Output is correct |
2 |
Correct |
61 ms |
41580 KB |
Output is correct |
3 |
Correct |
67 ms |
41452 KB |
Output is correct |
4 |
Correct |
60 ms |
41452 KB |
Output is correct |
5 |
Correct |
59 ms |
41452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
41452 KB |
Output is correct |
2 |
Correct |
67 ms |
41484 KB |
Output is correct |
3 |
Correct |
65 ms |
41452 KB |
Output is correct |
4 |
Correct |
65 ms |
41452 KB |
Output is correct |
5 |
Correct |
69 ms |
41616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
11 |
Correct |
48 ms |
40428 KB |
Output is correct |
12 |
Correct |
50 ms |
40428 KB |
Output is correct |
13 |
Correct |
45 ms |
40428 KB |
Output is correct |
14 |
Correct |
46 ms |
40428 KB |
Output is correct |
15 |
Correct |
46 ms |
40428 KB |
Output is correct |
16 |
Correct |
45 ms |
40428 KB |
Output is correct |
17 |
Correct |
47 ms |
40428 KB |
Output is correct |
18 |
Correct |
48 ms |
40428 KB |
Output is correct |
19 |
Correct |
45 ms |
40428 KB |
Output is correct |
20 |
Correct |
47 ms |
40556 KB |
Output is correct |
21 |
Correct |
46 ms |
40428 KB |
Output is correct |
22 |
Correct |
47 ms |
40428 KB |
Output is correct |
23 |
Correct |
47 ms |
40428 KB |
Output is correct |
24 |
Correct |
47 ms |
40428 KB |
Output is correct |
25 |
Correct |
46 ms |
40556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
11 |
Correct |
23 ms |
40428 KB |
Output is correct |
12 |
Correct |
25 ms |
40428 KB |
Output is correct |
13 |
Correct |
24 ms |
40428 KB |
Output is correct |
14 |
Correct |
25 ms |
40556 KB |
Output is correct |
15 |
Correct |
24 ms |
40428 KB |
Output is correct |
16 |
Correct |
28 ms |
40428 KB |
Output is correct |
17 |
Correct |
27 ms |
40428 KB |
Output is correct |
18 |
Correct |
29 ms |
40484 KB |
Output is correct |
19 |
Correct |
25 ms |
40428 KB |
Output is correct |
20 |
Correct |
24 ms |
40428 KB |
Output is correct |
21 |
Correct |
23 ms |
40428 KB |
Output is correct |
22 |
Correct |
25 ms |
40428 KB |
Output is correct |
23 |
Correct |
25 ms |
40428 KB |
Output is correct |
24 |
Correct |
26 ms |
40428 KB |
Output is correct |
25 |
Correct |
28 ms |
40428 KB |
Output is correct |
26 |
Correct |
48 ms |
40428 KB |
Output is correct |
27 |
Correct |
50 ms |
40428 KB |
Output is correct |
28 |
Correct |
45 ms |
40428 KB |
Output is correct |
29 |
Correct |
46 ms |
40428 KB |
Output is correct |
30 |
Correct |
46 ms |
40428 KB |
Output is correct |
31 |
Correct |
59 ms |
41472 KB |
Output is correct |
32 |
Correct |
61 ms |
41580 KB |
Output is correct |
33 |
Correct |
67 ms |
41452 KB |
Output is correct |
34 |
Correct |
60 ms |
41452 KB |
Output is correct |
35 |
Correct |
59 ms |
41452 KB |
Output is correct |
36 |
Correct |
65 ms |
41452 KB |
Output is correct |
37 |
Correct |
67 ms |
41484 KB |
Output is correct |
38 |
Correct |
65 ms |
41452 KB |
Output is correct |
39 |
Correct |
65 ms |
41452 KB |
Output is correct |
40 |
Correct |
69 ms |
41616 KB |
Output is correct |
41 |
Correct |
45 ms |
40428 KB |
Output is correct |
42 |
Correct |
47 ms |
40428 KB |
Output is correct |
43 |
Correct |
48 ms |
40428 KB |
Output is correct |
44 |
Correct |
45 ms |
40428 KB |
Output is correct |
45 |
Correct |
47 ms |
40556 KB |
Output is correct |
46 |
Correct |
46 ms |
40428 KB |
Output is correct |
47 |
Correct |
47 ms |
40428 KB |
Output is correct |
48 |
Correct |
47 ms |
40428 KB |
Output is correct |
49 |
Correct |
47 ms |
40428 KB |
Output is correct |
50 |
Correct |
46 ms |
40556 KB |
Output is correct |
51 |
Correct |
72 ms |
41324 KB |
Output is correct |
52 |
Correct |
75 ms |
41220 KB |
Output is correct |
53 |
Correct |
73 ms |
41324 KB |
Output is correct |
54 |
Correct |
79 ms |
41196 KB |
Output is correct |
55 |
Correct |
74 ms |
41324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
24 ms |
40576 KB |
Output is correct |
3 |
Correct |
24 ms |
40428 KB |
Output is correct |
4 |
Correct |
24 ms |
40428 KB |
Output is correct |
5 |
Correct |
24 ms |
40428 KB |
Output is correct |
6 |
Correct |
24 ms |
40428 KB |
Output is correct |
7 |
Correct |
25 ms |
40428 KB |
Output is correct |
8 |
Correct |
24 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40448 KB |
Output is correct |
10 |
Correct |
24 ms |
40428 KB |
Output is correct |
11 |
Correct |
23 ms |
40428 KB |
Output is correct |
12 |
Correct |
25 ms |
40428 KB |
Output is correct |
13 |
Correct |
24 ms |
40428 KB |
Output is correct |
14 |
Correct |
25 ms |
40556 KB |
Output is correct |
15 |
Correct |
24 ms |
40428 KB |
Output is correct |
16 |
Correct |
28 ms |
40428 KB |
Output is correct |
17 |
Correct |
27 ms |
40428 KB |
Output is correct |
18 |
Correct |
29 ms |
40484 KB |
Output is correct |
19 |
Correct |
25 ms |
40428 KB |
Output is correct |
20 |
Correct |
24 ms |
40428 KB |
Output is correct |
21 |
Correct |
23 ms |
40428 KB |
Output is correct |
22 |
Correct |
25 ms |
40428 KB |
Output is correct |
23 |
Correct |
25 ms |
40428 KB |
Output is correct |
24 |
Correct |
26 ms |
40428 KB |
Output is correct |
25 |
Correct |
28 ms |
40428 KB |
Output is correct |
26 |
Correct |
48 ms |
40428 KB |
Output is correct |
27 |
Correct |
50 ms |
40428 KB |
Output is correct |
28 |
Correct |
45 ms |
40428 KB |
Output is correct |
29 |
Correct |
46 ms |
40428 KB |
Output is correct |
30 |
Correct |
46 ms |
40428 KB |
Output is correct |
31 |
Correct |
59 ms |
41472 KB |
Output is correct |
32 |
Correct |
61 ms |
41580 KB |
Output is correct |
33 |
Correct |
67 ms |
41452 KB |
Output is correct |
34 |
Correct |
60 ms |
41452 KB |
Output is correct |
35 |
Correct |
59 ms |
41452 KB |
Output is correct |
36 |
Correct |
65 ms |
41452 KB |
Output is correct |
37 |
Correct |
67 ms |
41484 KB |
Output is correct |
38 |
Correct |
65 ms |
41452 KB |
Output is correct |
39 |
Correct |
65 ms |
41452 KB |
Output is correct |
40 |
Correct |
69 ms |
41616 KB |
Output is correct |
41 |
Correct |
45 ms |
40428 KB |
Output is correct |
42 |
Correct |
47 ms |
40428 KB |
Output is correct |
43 |
Correct |
48 ms |
40428 KB |
Output is correct |
44 |
Correct |
45 ms |
40428 KB |
Output is correct |
45 |
Correct |
47 ms |
40556 KB |
Output is correct |
46 |
Correct |
46 ms |
40428 KB |
Output is correct |
47 |
Correct |
47 ms |
40428 KB |
Output is correct |
48 |
Correct |
47 ms |
40428 KB |
Output is correct |
49 |
Correct |
47 ms |
40428 KB |
Output is correct |
50 |
Correct |
46 ms |
40556 KB |
Output is correct |
51 |
Correct |
72 ms |
41324 KB |
Output is correct |
52 |
Correct |
75 ms |
41220 KB |
Output is correct |
53 |
Correct |
73 ms |
41324 KB |
Output is correct |
54 |
Correct |
79 ms |
41196 KB |
Output is correct |
55 |
Correct |
74 ms |
41324 KB |
Output is correct |
56 |
Correct |
645 ms |
52560 KB |
Output is correct |
57 |
Correct |
631 ms |
60396 KB |
Output is correct |
58 |
Correct |
599 ms |
59824 KB |
Output is correct |
59 |
Correct |
605 ms |
60012 KB |
Output is correct |
60 |
Correct |
641 ms |
61548 KB |
Output is correct |