#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++) {
ans[i] = ans[i - 1];
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 |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
26 ms |
40572 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
26 ms |
40572 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
26 ms |
40572 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
40428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
41452 KB |
Output is correct |
2 |
Correct |
274 ms |
41596 KB |
Output is correct |
3 |
Correct |
279 ms |
41792 KB |
Output is correct |
4 |
Correct |
270 ms |
41452 KB |
Output is correct |
5 |
Correct |
266 ms |
41580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
41452 KB |
Output is correct |
2 |
Correct |
272 ms |
41452 KB |
Output is correct |
3 |
Correct |
276 ms |
41488 KB |
Output is correct |
4 |
Correct |
260 ms |
41452 KB |
Output is correct |
5 |
Correct |
284 ms |
41452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
48 ms |
40428 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
26 ms |
40572 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
40440 KB |
Output is correct |
2 |
Correct |
33 ms |
40428 KB |
Output is correct |
3 |
Correct |
32 ms |
40428 KB |
Output is correct |
4 |
Correct |
31 ms |
40428 KB |
Output is correct |
5 |
Correct |
26 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 |
26 ms |
40428 KB |
Output is correct |
10 |
Correct |
25 ms |
40428 KB |
Output is correct |
11 |
Incorrect |
26 ms |
40572 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |