/*
> 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>
using namespace std;
const int N = 4e5, B = 633;
struct T {
int *a, n;
priority_queue<int, vector<int>, greater<int>> u;
priority_queue<int> v;
void build(int *a_, int n_) {
a = a_, n = n_;
pull();
}
void pull() {
while (!v.empty())
v.pop();
for (int i = 0; i < n; i++)
v.push(a[i]);
}
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(x);
x = t;
}
return x;
}
void push() {
if (u.empty())
return;
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();
}
while (!u.empty())
u.pop();
}
} b[(N - 1) / B + 1];
int update(int l, int r, int x) {
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();
}
return x;
}
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)
x = update(l, r, x);
else {
swap(l, r);
x = update(r, n - 1, x);
x = update(0, l, x);
}
cout << x << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
444 KB |
Output is correct |
2 |
Correct |
173 ms |
444 KB |
Output is correct |
3 |
Correct |
179 ms |
436 KB |
Output is correct |
4 |
Correct |
174 ms |
444 KB |
Output is correct |
5 |
Correct |
189 ms |
428 KB |
Output is correct |
6 |
Correct |
175 ms |
424 KB |
Output is correct |
7 |
Correct |
67 ms |
500 KB |
Output is correct |
8 |
Correct |
67 ms |
340 KB |
Output is correct |
9 |
Correct |
180 ms |
464 KB |
Output is correct |
10 |
Correct |
185 ms |
444 KB |
Output is correct |
11 |
Correct |
185 ms |
532 KB |
Output is correct |
12 |
Correct |
179 ms |
456 KB |
Output is correct |
13 |
Correct |
191 ms |
452 KB |
Output is correct |
14 |
Correct |
232 ms |
444 KB |
Output is correct |
15 |
Correct |
229 ms |
452 KB |
Output is correct |
16 |
Correct |
85 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
376 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 |
3589 ms |
82664 KB |
Output is correct |
2 |
Correct |
3732 ms |
81348 KB |
Output is correct |
3 |
Correct |
747 ms |
5652 KB |
Output is correct |
4 |
Correct |
3520 ms |
82432 KB |
Output is correct |
5 |
Correct |
3248 ms |
81696 KB |
Output is correct |
6 |
Correct |
4070 ms |
81692 KB |
Output is correct |
7 |
Correct |
3915 ms |
81668 KB |
Output is correct |
8 |
Correct |
3900 ms |
81652 KB |
Output is correct |
9 |
Correct |
619 ms |
5608 KB |
Output is correct |
10 |
Correct |
2885 ms |
76264 KB |
Output is correct |
11 |
Correct |
602 ms |
5552 KB |
Output is correct |
12 |
Correct |
2965 ms |
77320 KB |
Output is correct |
13 |
Correct |
3479 ms |
83252 KB |
Output is correct |
14 |
Correct |
3555 ms |
81364 KB |
Output is correct |
15 |
Correct |
688 ms |
5728 KB |
Output is correct |
16 |
Correct |
3439 ms |
82744 KB |
Output is correct |
17 |
Correct |
2954 ms |
81572 KB |
Output is correct |
18 |
Correct |
3770 ms |
81640 KB |
Output is correct |
19 |
Correct |
3753 ms |
81600 KB |
Output is correct |
20 |
Correct |
3709 ms |
81696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
444 KB |
Output is correct |
2 |
Correct |
173 ms |
444 KB |
Output is correct |
3 |
Correct |
179 ms |
436 KB |
Output is correct |
4 |
Correct |
174 ms |
444 KB |
Output is correct |
5 |
Correct |
189 ms |
428 KB |
Output is correct |
6 |
Correct |
175 ms |
424 KB |
Output is correct |
7 |
Correct |
67 ms |
500 KB |
Output is correct |
8 |
Correct |
67 ms |
340 KB |
Output is correct |
9 |
Correct |
180 ms |
464 KB |
Output is correct |
10 |
Correct |
185 ms |
444 KB |
Output is correct |
11 |
Correct |
185 ms |
532 KB |
Output is correct |
12 |
Correct |
179 ms |
456 KB |
Output is correct |
13 |
Correct |
191 ms |
452 KB |
Output is correct |
14 |
Correct |
232 ms |
444 KB |
Output is correct |
15 |
Correct |
229 ms |
452 KB |
Output is correct |
16 |
Correct |
85 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
376 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3589 ms |
82664 KB |
Output is correct |
24 |
Correct |
3732 ms |
81348 KB |
Output is correct |
25 |
Correct |
747 ms |
5652 KB |
Output is correct |
26 |
Correct |
3520 ms |
82432 KB |
Output is correct |
27 |
Correct |
3248 ms |
81696 KB |
Output is correct |
28 |
Correct |
4070 ms |
81692 KB |
Output is correct |
29 |
Correct |
3915 ms |
81668 KB |
Output is correct |
30 |
Correct |
3900 ms |
81652 KB |
Output is correct |
31 |
Correct |
619 ms |
5608 KB |
Output is correct |
32 |
Correct |
2885 ms |
76264 KB |
Output is correct |
33 |
Correct |
602 ms |
5552 KB |
Output is correct |
34 |
Correct |
2965 ms |
77320 KB |
Output is correct |
35 |
Correct |
3479 ms |
83252 KB |
Output is correct |
36 |
Correct |
3555 ms |
81364 KB |
Output is correct |
37 |
Correct |
688 ms |
5728 KB |
Output is correct |
38 |
Correct |
3439 ms |
82744 KB |
Output is correct |
39 |
Correct |
2954 ms |
81572 KB |
Output is correct |
40 |
Correct |
3770 ms |
81640 KB |
Output is correct |
41 |
Correct |
3753 ms |
81600 KB |
Output is correct |
42 |
Correct |
3709 ms |
81696 KB |
Output is correct |
43 |
Correct |
3330 ms |
9636 KB |
Output is correct |
44 |
Correct |
3368 ms |
9864 KB |
Output is correct |
45 |
Correct |
1482 ms |
5768 KB |
Output is correct |
46 |
Correct |
3309 ms |
9832 KB |
Output is correct |
47 |
Correct |
3314 ms |
9716 KB |
Output is correct |
48 |
Correct |
4722 ms |
11644 KB |
Output is correct |
49 |
Correct |
5440 ms |
11404 KB |
Output is correct |
50 |
Correct |
5623 ms |
11380 KB |
Output is correct |
51 |
Correct |
5334 ms |
11340 KB |
Output is correct |
52 |
Correct |
4344 ms |
10488 KB |
Output is correct |
53 |
Correct |
4149 ms |
10444 KB |
Output is correct |
54 |
Correct |
5445 ms |
13892 KB |
Output is correct |
55 |
Correct |
5889 ms |
13872 KB |
Output is correct |
56 |
Correct |
5826 ms |
14128 KB |
Output is correct |
57 |
Correct |
5935 ms |
13944 KB |
Output is correct |
58 |
Correct |
4067 ms |
14028 KB |
Output is correct |
59 |
Correct |
4075 ms |
14332 KB |
Output is correct |
60 |
Correct |
3676 ms |
81664 KB |
Output is correct |
61 |
Correct |
4517 ms |
81728 KB |
Output is correct |
62 |
Correct |
4392 ms |
81728 KB |
Output is correct |
63 |
Correct |
3978 ms |
81676 KB |
Output is correct |
64 |
Correct |
1123 ms |
5732 KB |
Output is correct |
65 |
Correct |
2486 ms |
43096 KB |
Output is correct |
66 |
Correct |
2477 ms |
43528 KB |
Output is correct |
67 |
Correct |
4672 ms |
48848 KB |
Output is correct |
68 |
Correct |
5375 ms |
48848 KB |
Output is correct |
69 |
Correct |
5455 ms |
48828 KB |
Output is correct |
70 |
Correct |
5266 ms |
48840 KB |
Output is correct |