#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()
#define lc(i) 2*i
#define rc(i) 2*i+1
#define int long long
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vii = vector<ii>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
const int N = 1e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 5;
struct UFDS {
int n, validFlights;
vector<int> par, s;
vector<bool> active;
void init(int x) {
n = x;
validFlights = 0;
par.resize(n);
iota(all(par), 0);
s.assign(n, 1);
active.assign(n, false);
}
int get(int x) {
if (par[x] == x) return x;
return par[x] = get(par[x]);
}
bool unite(int x, int y) {
x = get(x);
y = get(y);
if (!active[x]) {
active[x] = true;
validFlights += s[x];
}
if (!active[y] || x == y) return false;
if (s[x] < s[y]) swap(x, y);
validFlights -= flights(s[x]) + flights(s[y]);
s[x] += s[y];
par[y] = x;
validFlights += flights(s[x]);
return true;
}
int getSz(int x) {
x = get(x);
return s[x];
}
int flights(int x) {
return x * (x + 1) / 2;
}
};
signed main() {
FAST_IO;
#ifdef arujbansal_local
setIO("input.txt", "output.txt");
#endif
int n, q;
cin >> n >> q;
vii h(n);
for (int i = 0; i < n; i++) {
cin >> h[i].first;
h[i].second = i;
}
sort(all(h));
vii queries(q);
for (int i = 0; i < q; i++) {
cin >> queries[i].first;
queries[i].second = i;
}
sort(all(queries));
UFDS mountains;
mountains.init(n);
int ans[q];
int j = 0;
for (const auto &[plane, idx] : queries) {
while (j < n && h[j].first <= plane) {
int cur = h[j].second;
int prev = cur - 1;
int next = cur + 1;
if (prev >= 0) mountains.unite(cur, prev);
if (next < n) mountains.unite(cur, next);
j++;
}
ans[idx] = mountains.validFlights;
}
for (const auto &x : ans) cout << x << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3436 KB |
Output is correct |
2 |
Correct |
25 ms |
3436 KB |
Output is correct |
3 |
Correct |
24 ms |
3308 KB |
Output is correct |
4 |
Correct |
23 ms |
3180 KB |
Output is correct |
5 |
Correct |
23 ms |
3308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6500 KB |
Output is correct |
2 |
Correct |
46 ms |
7652 KB |
Output is correct |
3 |
Correct |
44 ms |
7656 KB |
Output is correct |
4 |
Correct |
47 ms |
7812 KB |
Output is correct |
5 |
Correct |
44 ms |
7524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
6372 KB |
Output is correct |
2 |
Correct |
46 ms |
7780 KB |
Output is correct |
3 |
Correct |
46 ms |
7780 KB |
Output is correct |
4 |
Correct |
45 ms |
7908 KB |
Output is correct |
5 |
Correct |
53 ms |
8040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
27 ms |
3436 KB |
Output is correct |
12 |
Correct |
25 ms |
3436 KB |
Output is correct |
13 |
Correct |
24 ms |
3308 KB |
Output is correct |
14 |
Correct |
23 ms |
3180 KB |
Output is correct |
15 |
Correct |
23 ms |
3308 KB |
Output is correct |
16 |
Correct |
21 ms |
3308 KB |
Output is correct |
17 |
Correct |
21 ms |
3436 KB |
Output is correct |
18 |
Correct |
21 ms |
3556 KB |
Output is correct |
19 |
Correct |
19 ms |
3180 KB |
Output is correct |
20 |
Correct |
21 ms |
3300 KB |
Output is correct |
21 |
Correct |
24 ms |
3180 KB |
Output is correct |
22 |
Correct |
25 ms |
3308 KB |
Output is correct |
23 |
Correct |
21 ms |
3436 KB |
Output is correct |
24 |
Correct |
19 ms |
3172 KB |
Output is correct |
25 |
Correct |
21 ms |
3308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
27 ms |
3436 KB |
Output is correct |
27 |
Correct |
25 ms |
3436 KB |
Output is correct |
28 |
Correct |
24 ms |
3308 KB |
Output is correct |
29 |
Correct |
23 ms |
3180 KB |
Output is correct |
30 |
Correct |
23 ms |
3308 KB |
Output is correct |
31 |
Correct |
43 ms |
6500 KB |
Output is correct |
32 |
Correct |
46 ms |
7652 KB |
Output is correct |
33 |
Correct |
44 ms |
7656 KB |
Output is correct |
34 |
Correct |
47 ms |
7812 KB |
Output is correct |
35 |
Correct |
44 ms |
7524 KB |
Output is correct |
36 |
Correct |
44 ms |
6372 KB |
Output is correct |
37 |
Correct |
46 ms |
7780 KB |
Output is correct |
38 |
Correct |
46 ms |
7780 KB |
Output is correct |
39 |
Correct |
45 ms |
7908 KB |
Output is correct |
40 |
Correct |
53 ms |
8040 KB |
Output is correct |
41 |
Correct |
21 ms |
3308 KB |
Output is correct |
42 |
Correct |
21 ms |
3436 KB |
Output is correct |
43 |
Correct |
21 ms |
3556 KB |
Output is correct |
44 |
Correct |
19 ms |
3180 KB |
Output is correct |
45 |
Correct |
21 ms |
3300 KB |
Output is correct |
46 |
Correct |
24 ms |
3180 KB |
Output is correct |
47 |
Correct |
25 ms |
3308 KB |
Output is correct |
48 |
Correct |
21 ms |
3436 KB |
Output is correct |
49 |
Correct |
19 ms |
3172 KB |
Output is correct |
50 |
Correct |
21 ms |
3308 KB |
Output is correct |
51 |
Correct |
69 ms |
7652 KB |
Output is correct |
52 |
Correct |
54 ms |
7524 KB |
Output is correct |
53 |
Correct |
55 ms |
7652 KB |
Output is correct |
54 |
Correct |
65 ms |
7672 KB |
Output is correct |
55 |
Correct |
55 ms |
7652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
27 ms |
3436 KB |
Output is correct |
27 |
Correct |
25 ms |
3436 KB |
Output is correct |
28 |
Correct |
24 ms |
3308 KB |
Output is correct |
29 |
Correct |
23 ms |
3180 KB |
Output is correct |
30 |
Correct |
23 ms |
3308 KB |
Output is correct |
31 |
Correct |
43 ms |
6500 KB |
Output is correct |
32 |
Correct |
46 ms |
7652 KB |
Output is correct |
33 |
Correct |
44 ms |
7656 KB |
Output is correct |
34 |
Correct |
47 ms |
7812 KB |
Output is correct |
35 |
Correct |
44 ms |
7524 KB |
Output is correct |
36 |
Correct |
44 ms |
6372 KB |
Output is correct |
37 |
Correct |
46 ms |
7780 KB |
Output is correct |
38 |
Correct |
46 ms |
7780 KB |
Output is correct |
39 |
Correct |
45 ms |
7908 KB |
Output is correct |
40 |
Correct |
53 ms |
8040 KB |
Output is correct |
41 |
Correct |
21 ms |
3308 KB |
Output is correct |
42 |
Correct |
21 ms |
3436 KB |
Output is correct |
43 |
Correct |
21 ms |
3556 KB |
Output is correct |
44 |
Correct |
19 ms |
3180 KB |
Output is correct |
45 |
Correct |
21 ms |
3300 KB |
Output is correct |
46 |
Correct |
24 ms |
3180 KB |
Output is correct |
47 |
Correct |
25 ms |
3308 KB |
Output is correct |
48 |
Correct |
21 ms |
3436 KB |
Output is correct |
49 |
Correct |
19 ms |
3172 KB |
Output is correct |
50 |
Correct |
21 ms |
3308 KB |
Output is correct |
51 |
Correct |
69 ms |
7652 KB |
Output is correct |
52 |
Correct |
54 ms |
7524 KB |
Output is correct |
53 |
Correct |
55 ms |
7652 KB |
Output is correct |
54 |
Correct |
65 ms |
7672 KB |
Output is correct |
55 |
Correct |
55 ms |
7652 KB |
Output is correct |
56 |
Correct |
727 ms |
73816 KB |
Output is correct |
57 |
Correct |
737 ms |
73316 KB |
Output is correct |
58 |
Correct |
690 ms |
69216 KB |
Output is correct |
59 |
Correct |
726 ms |
71044 KB |
Output is correct |
60 |
Correct |
738 ms |
75552 KB |
Output is correct |