#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[b] = a;
curans += Rank[roota] * (Rank[roota] + 1) / 2;
}
else {
Rank[rootb] += Rank[roota];
par[a] = b;
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] << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
25 ms |
40428 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
25 ms |
40428 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
25 ms |
40428 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
40444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
41452 KB |
Output is correct |
2 |
Correct |
312 ms |
42572 KB |
Output is correct |
3 |
Correct |
293 ms |
42604 KB |
Output is correct |
4 |
Correct |
288 ms |
42604 KB |
Output is correct |
5 |
Correct |
300 ms |
42732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
41452 KB |
Output is correct |
2 |
Correct |
271 ms |
42476 KB |
Output is correct |
3 |
Correct |
276 ms |
42608 KB |
Output is correct |
4 |
Correct |
271 ms |
42476 KB |
Output is correct |
5 |
Correct |
297 ms |
42528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
53 ms |
40444 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
25 ms |
40428 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
40428 KB |
Output is correct |
2 |
Correct |
26 ms |
40428 KB |
Output is correct |
3 |
Correct |
28 ms |
40428 KB |
Output is correct |
4 |
Correct |
25 ms |
40428 KB |
Output is correct |
5 |
Correct |
25 ms |
40428 KB |
Output is correct |
6 |
Correct |
25 ms |
40428 KB |
Output is correct |
7 |
Correct |
26 ms |
40428 KB |
Output is correct |
8 |
Correct |
25 ms |
40428 KB |
Output is correct |
9 |
Correct |
25 ms |
40428 KB |
Output is correct |
10 |
Correct |
26 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
25 ms |
40428 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |