/*
> 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 = 900;
char _buf[1 << 20], *_p1, *_p2;
#define getchar() (_p1 == _p2 ? (_p2 = _buf + fread(_p1 = _buf, 1, 1 << 20, stdin), _p1 == _p2 ? EOF : *_p1++) : *_p1++)
int read() {
int s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
w = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
return s * w;
}
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() {
int n = read(), q = read();
static int a[N];
for (int i = 0; i < n; i++)
a[i] = read();
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 = read() - 1, r = read() - 1, x = read();
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();
}
}
printf("%d\n", x);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
400 KB |
Output is correct |
2 |
Correct |
21 ms |
488 KB |
Output is correct |
3 |
Correct |
22 ms |
468 KB |
Output is correct |
4 |
Correct |
22 ms |
468 KB |
Output is correct |
5 |
Correct |
22 ms |
448 KB |
Output is correct |
6 |
Correct |
22 ms |
428 KB |
Output is correct |
7 |
Correct |
16 ms |
340 KB |
Output is correct |
8 |
Correct |
15 ms |
340 KB |
Output is correct |
9 |
Correct |
21 ms |
488 KB |
Output is correct |
10 |
Correct |
22 ms |
468 KB |
Output is correct |
11 |
Correct |
21 ms |
400 KB |
Output is correct |
12 |
Correct |
21 ms |
468 KB |
Output is correct |
13 |
Correct |
23 ms |
468 KB |
Output is correct |
14 |
Correct |
16 ms |
468 KB |
Output is correct |
15 |
Correct |
17 ms |
492 KB |
Output is correct |
16 |
Correct |
13 ms |
496 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1034 ms |
56680 KB |
Output is correct |
2 |
Correct |
983 ms |
56748 KB |
Output is correct |
3 |
Correct |
163 ms |
4772 KB |
Output is correct |
4 |
Correct |
1000 ms |
57272 KB |
Output is correct |
5 |
Correct |
666 ms |
56240 KB |
Output is correct |
6 |
Correct |
923 ms |
56212 KB |
Output is correct |
7 |
Correct |
899 ms |
56352 KB |
Output is correct |
8 |
Correct |
827 ms |
56396 KB |
Output is correct |
9 |
Correct |
153 ms |
4812 KB |
Output is correct |
10 |
Correct |
568 ms |
53456 KB |
Output is correct |
11 |
Correct |
151 ms |
4884 KB |
Output is correct |
12 |
Correct |
577 ms |
53964 KB |
Output is correct |
13 |
Correct |
996 ms |
56600 KB |
Output is correct |
14 |
Correct |
982 ms |
56580 KB |
Output is correct |
15 |
Correct |
177 ms |
4816 KB |
Output is correct |
16 |
Correct |
1012 ms |
56544 KB |
Output is correct |
17 |
Correct |
643 ms |
56396 KB |
Output is correct |
18 |
Correct |
988 ms |
56344 KB |
Output is correct |
19 |
Correct |
953 ms |
56404 KB |
Output is correct |
20 |
Correct |
841 ms |
56392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
400 KB |
Output is correct |
2 |
Correct |
21 ms |
488 KB |
Output is correct |
3 |
Correct |
22 ms |
468 KB |
Output is correct |
4 |
Correct |
22 ms |
468 KB |
Output is correct |
5 |
Correct |
22 ms |
448 KB |
Output is correct |
6 |
Correct |
22 ms |
428 KB |
Output is correct |
7 |
Correct |
16 ms |
340 KB |
Output is correct |
8 |
Correct |
15 ms |
340 KB |
Output is correct |
9 |
Correct |
21 ms |
488 KB |
Output is correct |
10 |
Correct |
22 ms |
468 KB |
Output is correct |
11 |
Correct |
21 ms |
400 KB |
Output is correct |
12 |
Correct |
21 ms |
468 KB |
Output is correct |
13 |
Correct |
23 ms |
468 KB |
Output is correct |
14 |
Correct |
16 ms |
468 KB |
Output is correct |
15 |
Correct |
17 ms |
492 KB |
Output is correct |
16 |
Correct |
13 ms |
496 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1034 ms |
56680 KB |
Output is correct |
24 |
Correct |
983 ms |
56748 KB |
Output is correct |
25 |
Correct |
163 ms |
4772 KB |
Output is correct |
26 |
Correct |
1000 ms |
57272 KB |
Output is correct |
27 |
Correct |
666 ms |
56240 KB |
Output is correct |
28 |
Correct |
923 ms |
56212 KB |
Output is correct |
29 |
Correct |
899 ms |
56352 KB |
Output is correct |
30 |
Correct |
827 ms |
56396 KB |
Output is correct |
31 |
Correct |
153 ms |
4812 KB |
Output is correct |
32 |
Correct |
568 ms |
53456 KB |
Output is correct |
33 |
Correct |
151 ms |
4884 KB |
Output is correct |
34 |
Correct |
577 ms |
53964 KB |
Output is correct |
35 |
Correct |
996 ms |
56600 KB |
Output is correct |
36 |
Correct |
982 ms |
56580 KB |
Output is correct |
37 |
Correct |
177 ms |
4816 KB |
Output is correct |
38 |
Correct |
1012 ms |
56544 KB |
Output is correct |
39 |
Correct |
643 ms |
56396 KB |
Output is correct |
40 |
Correct |
988 ms |
56344 KB |
Output is correct |
41 |
Correct |
953 ms |
56404 KB |
Output is correct |
42 |
Correct |
841 ms |
56392 KB |
Output is correct |
43 |
Correct |
635 ms |
5452 KB |
Output is correct |
44 |
Correct |
636 ms |
5620 KB |
Output is correct |
45 |
Correct |
346 ms |
4892 KB |
Output is correct |
46 |
Correct |
628 ms |
5412 KB |
Output is correct |
47 |
Correct |
626 ms |
5344 KB |
Output is correct |
48 |
Correct |
1887 ms |
6368 KB |
Output is correct |
49 |
Correct |
2009 ms |
6356 KB |
Output is correct |
50 |
Correct |
2013 ms |
6364 KB |
Output is correct |
51 |
Correct |
2032 ms |
6536 KB |
Output is correct |
52 |
Correct |
388 ms |
5964 KB |
Output is correct |
53 |
Correct |
396 ms |
5960 KB |
Output is correct |
54 |
Correct |
1460 ms |
7880 KB |
Output is correct |
55 |
Correct |
1508 ms |
7720 KB |
Output is correct |
56 |
Correct |
1500 ms |
7616 KB |
Output is correct |
57 |
Correct |
1496 ms |
7672 KB |
Output is correct |
58 |
Correct |
1563 ms |
7904 KB |
Output is correct |
59 |
Correct |
1518 ms |
7904 KB |
Output is correct |
60 |
Correct |
674 ms |
56168 KB |
Output is correct |
61 |
Correct |
921 ms |
56344 KB |
Output is correct |
62 |
Correct |
901 ms |
56120 KB |
Output is correct |
63 |
Correct |
924 ms |
56144 KB |
Output is correct |
64 |
Correct |
193 ms |
4616 KB |
Output is correct |
65 |
Correct |
364 ms |
29600 KB |
Output is correct |
66 |
Correct |
395 ms |
30156 KB |
Output is correct |
67 |
Correct |
373 ms |
33080 KB |
Output is correct |
68 |
Correct |
519 ms |
33196 KB |
Output is correct |
69 |
Correct |
498 ms |
33056 KB |
Output is correct |
70 |
Correct |
520 ms |
33096 KB |
Output is correct |