/*
> Statement
You are given a circular array A of length N. You are given Q queries, each giving (l, r, a). You are to run the following code:
for (int i = l; i <= r; i++)
if (a < A[i])
swap(a, A[i]);
For each query, output the final value of a.
> Scoring
05: 1 <= N, Q <= 2e3
15: For all queries, l = r = 1
80: 1 <= N <= 4e5, 1 <= Q <= 2.5e4, 1 <= A_i <= 1e9
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 4e5, B = 1000;
struct T {
int *a, n;
vector<int> u;
priority_queue<int> v;
void build(int *a_, int n_) {
a = a_, n = n_;
pull();
}
void pull() {
v = priority_queue<int>(a, a + n);
}
int force(int l, int r, int x) {
for (int i = l; i <= r; i++)
if (x < a[i])
swap(x, a[i]);
return x;
}
int put(int x) {
int t = v.top();
if (x < t) {
v.pop();
v.push(x);
u.push_back(x);
x = t;
}
return x;
}
void push() {
if (u.empty())
return;
static priority_queue<int, vector<int>, greater<int>> u_;
u_ = priority_queue<int, vector<int>, greater<int>>(u.begin(), u.end());
int t = u_.top();
for (int i = 0; i < n; i++)
if (t < a[i]) {
u_.pop(), u_.push(a[i]);
a[i] = t, t = u_.top();
}
u.clear();
}
} b[(N - 1) / B + 1];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
static int a[N];
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i <= (n - 1) / B; i++)
b[i].build(a + B * i, min(n, B * (i + 1)) - B * i);
while (q--) {
int l, r, x;
cin >> l >> r >> x, l--, r--;
if (l <= r) {
if (l / B == r / B) {
b[l / B].push();
x = b[l / B].force(l - l / B * B, r - l / B * B, x);
b[l / B].pull();
} else {
b[l / B].push();
x = b[l / B].force(l - l / B * B, b[l / B].n - 1, x);
b[l / B].pull();
for (int i = l / B + 1; i < r / B; i++)
x = b[i].put(x);
b[r / B].push();
x = b[r / B].force(0, r - r / B * B, x);
b[r / B].pull();
}
} else {
if (l / B == r / B) {
b[l / B].push();
x = b[l / B].force(l - l / B * B, b[l / B].n - 1, x);
for (int i = l / B + 1; i <= (n - 1) / B; i++)
x = b[i].put(x);
for (int i = 0; i < l / B; i++)
x = b[i].put(x);
x = b[l / B].force(0, r - l / B * B, x);
b[l / B].pull();
} else {
b[l / B].push();
x = b[l / B].force(l - l / B * B, b[l / B].n - 1, x);
b[l / B].pull();
for (int i = l / B + 1; i <= (n - 1) / B; i++)
x = b[i].put(x);
for (int i = 0; i < r / B; i++)
x = b[i].put(x);
b[r / B].push();
x = b[r / B].force(0, r - r / B * B, x);
b[r / B].pull();
}
}
cout << x << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
384 KB |
Output is correct |
2 |
Correct |
25 ms |
340 KB |
Output is correct |
3 |
Correct |
22 ms |
396 KB |
Output is correct |
4 |
Correct |
25 ms |
380 KB |
Output is correct |
5 |
Correct |
23 ms |
380 KB |
Output is correct |
6 |
Correct |
26 ms |
340 KB |
Output is correct |
7 |
Correct |
19 ms |
340 KB |
Output is correct |
8 |
Correct |
19 ms |
340 KB |
Output is correct |
9 |
Correct |
25 ms |
380 KB |
Output is correct |
10 |
Correct |
25 ms |
396 KB |
Output is correct |
11 |
Correct |
22 ms |
340 KB |
Output is correct |
12 |
Correct |
24 ms |
396 KB |
Output is correct |
13 |
Correct |
24 ms |
340 KB |
Output is correct |
14 |
Correct |
19 ms |
400 KB |
Output is correct |
15 |
Correct |
20 ms |
396 KB |
Output is correct |
16 |
Correct |
15 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1133 ms |
50324 KB |
Output is correct |
2 |
Correct |
1171 ms |
50124 KB |
Output is correct |
3 |
Correct |
307 ms |
3620 KB |
Output is correct |
4 |
Correct |
1141 ms |
50112 KB |
Output is correct |
5 |
Correct |
751 ms |
49608 KB |
Output is correct |
6 |
Correct |
1067 ms |
49540 KB |
Output is correct |
7 |
Correct |
987 ms |
49700 KB |
Output is correct |
8 |
Correct |
914 ms |
49624 KB |
Output is correct |
9 |
Correct |
237 ms |
3620 KB |
Output is correct |
10 |
Correct |
696 ms |
47000 KB |
Output is correct |
11 |
Correct |
231 ms |
3628 KB |
Output is correct |
12 |
Correct |
593 ms |
47888 KB |
Output is correct |
13 |
Correct |
1167 ms |
49988 KB |
Output is correct |
14 |
Correct |
1159 ms |
49908 KB |
Output is correct |
15 |
Correct |
283 ms |
3492 KB |
Output is correct |
16 |
Correct |
1081 ms |
50288 KB |
Output is correct |
17 |
Correct |
798 ms |
49688 KB |
Output is correct |
18 |
Correct |
1045 ms |
49804 KB |
Output is correct |
19 |
Correct |
951 ms |
49736 KB |
Output is correct |
20 |
Correct |
928 ms |
49788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
384 KB |
Output is correct |
2 |
Correct |
25 ms |
340 KB |
Output is correct |
3 |
Correct |
22 ms |
396 KB |
Output is correct |
4 |
Correct |
25 ms |
380 KB |
Output is correct |
5 |
Correct |
23 ms |
380 KB |
Output is correct |
6 |
Correct |
26 ms |
340 KB |
Output is correct |
7 |
Correct |
19 ms |
340 KB |
Output is correct |
8 |
Correct |
19 ms |
340 KB |
Output is correct |
9 |
Correct |
25 ms |
380 KB |
Output is correct |
10 |
Correct |
25 ms |
396 KB |
Output is correct |
11 |
Correct |
22 ms |
340 KB |
Output is correct |
12 |
Correct |
24 ms |
396 KB |
Output is correct |
13 |
Correct |
24 ms |
340 KB |
Output is correct |
14 |
Correct |
19 ms |
400 KB |
Output is correct |
15 |
Correct |
20 ms |
396 KB |
Output is correct |
16 |
Correct |
15 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1133 ms |
50324 KB |
Output is correct |
24 |
Correct |
1171 ms |
50124 KB |
Output is correct |
25 |
Correct |
307 ms |
3620 KB |
Output is correct |
26 |
Correct |
1141 ms |
50112 KB |
Output is correct |
27 |
Correct |
751 ms |
49608 KB |
Output is correct |
28 |
Correct |
1067 ms |
49540 KB |
Output is correct |
29 |
Correct |
987 ms |
49700 KB |
Output is correct |
30 |
Correct |
914 ms |
49624 KB |
Output is correct |
31 |
Correct |
237 ms |
3620 KB |
Output is correct |
32 |
Correct |
696 ms |
47000 KB |
Output is correct |
33 |
Correct |
231 ms |
3628 KB |
Output is correct |
34 |
Correct |
593 ms |
47888 KB |
Output is correct |
35 |
Correct |
1167 ms |
49988 KB |
Output is correct |
36 |
Correct |
1159 ms |
49908 KB |
Output is correct |
37 |
Correct |
283 ms |
3492 KB |
Output is correct |
38 |
Correct |
1081 ms |
50288 KB |
Output is correct |
39 |
Correct |
798 ms |
49688 KB |
Output is correct |
40 |
Correct |
1045 ms |
49804 KB |
Output is correct |
41 |
Correct |
951 ms |
49736 KB |
Output is correct |
42 |
Correct |
928 ms |
49788 KB |
Output is correct |
43 |
Correct |
702 ms |
4028 KB |
Output is correct |
44 |
Correct |
712 ms |
4144 KB |
Output is correct |
45 |
Correct |
390 ms |
3540 KB |
Output is correct |
46 |
Correct |
712 ms |
4128 KB |
Output is correct |
47 |
Correct |
695 ms |
4084 KB |
Output is correct |
48 |
Correct |
2057 ms |
4836 KB |
Output is correct |
49 |
Correct |
2169 ms |
4828 KB |
Output is correct |
50 |
Correct |
2155 ms |
4888 KB |
Output is correct |
51 |
Correct |
2138 ms |
4976 KB |
Output is correct |
52 |
Correct |
469 ms |
4588 KB |
Output is correct |
53 |
Correct |
449 ms |
4412 KB |
Output is correct |
54 |
Correct |
1536 ms |
5908 KB |
Output is correct |
55 |
Correct |
1597 ms |
6228 KB |
Output is correct |
56 |
Correct |
1584 ms |
6124 KB |
Output is correct |
57 |
Correct |
1591 ms |
6096 KB |
Output is correct |
58 |
Correct |
1647 ms |
6204 KB |
Output is correct |
59 |
Correct |
1673 ms |
6512 KB |
Output is correct |
60 |
Correct |
740 ms |
49652 KB |
Output is correct |
61 |
Correct |
941 ms |
49712 KB |
Output is correct |
62 |
Correct |
1024 ms |
49612 KB |
Output is correct |
63 |
Correct |
967 ms |
49616 KB |
Output is correct |
64 |
Correct |
234 ms |
3504 KB |
Output is correct |
65 |
Correct |
396 ms |
26132 KB |
Output is correct |
66 |
Correct |
463 ms |
26212 KB |
Output is correct |
67 |
Correct |
482 ms |
29080 KB |
Output is correct |
68 |
Correct |
556 ms |
29076 KB |
Output is correct |
69 |
Correct |
529 ms |
29124 KB |
Output is correct |
70 |
Correct |
528 ms |
29104 KB |
Output is correct |