#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
//#define int ll
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
const int block = 200;
const int MAX_N = 3e5 + 5;
int nx[MAX_N][2];
pair<ll, int> dp[MAX_N][2];
pair<int, int> dq[2 * block + 1];
int rangesl[MAX_N / block + 1][block];
int rangesr[MAX_N / block + 1][block];
int ran[MAX_N][2];
int sz[MAX_N / block + 1];
void Update(int a, int b) {
int dl = block, dr = block;
for (int i = a; i < b; i++) {
while (dl < dr) {
if (ran[dq[dl].x][dq[dl].y] < ran[i][0]) {
nx[dq[dl].x][dq[dl].y] = i;
dl++;
} else if (ran[dq[dr - 1].x][dq[dr - 1].y] > ran[i][1]) {
nx[dq[dr - 1].x][dq[dr - 1].y] = i;
dr--;
} else break;
}
dq[--dl] = {i, 0}, dq[dr++] = {i, 1};
}
while (dl < dr) {
nx[dq[dl].x][dq[dl].y] = b;
dl++;
}
for (int i = b - 1; i >= a; i--) {
for (int j = 0; j < 2; j++) {
int x = nx[i][j];
if (x == b) {
dp[i][j] = {0, ran[i][j]};
} else {
if (ran[i][j] > ran[x][1]) {
dp[i][j] = {dp[x][1].x + ran[i][j] - ran[x][1], dp[x][1].y};
} else {
dp[i][j] = {dp[x][0].x, dp[x][0].y};
}
}
}
}
int bl = a / block;
sz[bl] = b - a;
rangesl[bl][0] = ran[a][0];
rangesr[bl][0] = ran[a][1];
for (int i = a + 1; i < b; i++) {
rangesl[bl][i - a] = max(rangesl[bl][i - a - 1], ran[i][0]);
rangesr[bl][i - a] = min(rangesr[bl][i - a - 1], ran[i][1]);
if (rangesl[bl][i - a] > rangesr[bl][i - a]) {
chkmin(sz[bl], i - a);
}
}
// sz[bl]--;
}
ll Query(int a, int b, int c, int d) {
ll ans = 0;
while (a < c && a % block) {
if (b < ran[a][0]) b = ran[a][0], a = nx[a][0];
else if (b > ran[a][1]) ans += b - ran[a][1], b = ran[a][1], a = nx[a][1];
else a++;
}
while (a + block < c) {
int bl = a / block;
if (b > rangesr[bl][sz[bl] - 1]) {
ans += b - rangesr[bl][sz[bl] - 1];
b = rangesr[bl][sz[bl] - 1];
} else if (b < rangesl[bl][sz[bl] - 1]) {
b = rangesl[bl][sz[bl] - 1];
}
int i = a + sz[bl];
if (i < a + block) {
if (b <= ran[i][0]) {
ans += dp[i][0].x;
b = dp[i][0].y;
} else if (b >= ran[i][1]) {
ans += dp[i][1].x;
ans += b - ran[i][1];
b = dp[i][1].y;
}
}
// int i = min(lower_bound(rangesl[bl], rangesl[bl] + sz[bl], b) - rangesl[bl],
// lower_bound(rangesr[bl], rangesr[bl] + sz[bl], b, greater<int>()) - rangesr[bl]) + a;
a += block;
}
while (a < c) {
if (b < ran[a][0]) b = ran[a][0], a = nx[a][0];
else if (b > ran[a][1]) ans += b - ran[a][1], b = ran[a][1], a = nx[a][1];
else a++;
}
return ans + max<ll>(0, b - d);
}
int L[MAX_N], R[MAX_N], t[MAX_N], a[MAX_N], b[MAX_N], c[MAX_N], d[MAX_N];
ll ans[MAX_N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i + 1 < n; i++) cin >> L[i] >> R[i];
for (int i = 0; i < q; i++) {
cin >> t[i] >> a[i] >> b[i] >> c[i];
if (t[i] == 2) cin >> d[i];
}
for (int i = 0; i < q; i++) a[i]--, c[i]--;
for (int i = 0; i + 1 < n; i++) R[i]--;
for (int ti = 0; ti < 2; ti++) {
for (int i = 0; i + 1 < n; i++) {
ran[i][0] = L[i] - i, ran[i][1] = R[i] - i;
}
for (int i = 0; i + 1 < n; i += block) {
Update(i, min(i + block, n - 1));
}
for (int i = 0; i < q; i++) {
if (t[i] == 1) {
ran[a[i]][0] = b[i] - a[i], ran[a[i]][1] = c[i] - a[i];
Update(a[i] / block * block, min(n - 1, a[i] / block * block + block));
} else {
if (a[i] <= c[i]) ans[i] = Query(a[i], b[i] - a[i], c[i], d[i] - c[i]);
}
}
reverse(L, L + n - 1), reverse(R, R + n - 1);
for (int i = 0; i < q; i++) {
if (t[i] == 1) a[i] = n - 2 - a[i];
else a[i] = n - 1 - a[i], c[i] = n - 1 - c[i];
}
}
for (int i = 0; i < q; i++) {
if (t[i] == 2) cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
2 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16720 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16720 KB |
Output is correct |
7 |
Correct |
3 ms |
16720 KB |
Output is correct |
8 |
Correct |
2 ms |
16720 KB |
Output is correct |
9 |
Correct |
2 ms |
16720 KB |
Output is correct |
10 |
Correct |
2 ms |
16720 KB |
Output is correct |
11 |
Correct |
5 ms |
16896 KB |
Output is correct |
12 |
Correct |
5 ms |
16732 KB |
Output is correct |
13 |
Correct |
5 ms |
16888 KB |
Output is correct |
14 |
Correct |
5 ms |
16720 KB |
Output is correct |
15 |
Correct |
5 ms |
16720 KB |
Output is correct |
16 |
Correct |
5 ms |
16720 KB |
Output is correct |
17 |
Correct |
5 ms |
17056 KB |
Output is correct |
18 |
Correct |
5 ms |
16720 KB |
Output is correct |
19 |
Correct |
5 ms |
16892 KB |
Output is correct |
20 |
Correct |
5 ms |
16720 KB |
Output is correct |
21 |
Correct |
5 ms |
16880 KB |
Output is correct |
22 |
Correct |
5 ms |
16720 KB |
Output is correct |
23 |
Correct |
5 ms |
16720 KB |
Output is correct |
24 |
Correct |
6 ms |
16720 KB |
Output is correct |
25 |
Correct |
5 ms |
16720 KB |
Output is correct |
26 |
Correct |
4 ms |
16720 KB |
Output is correct |
27 |
Correct |
5 ms |
16884 KB |
Output is correct |
28 |
Correct |
7 ms |
16720 KB |
Output is correct |
29 |
Correct |
5 ms |
16720 KB |
Output is correct |
30 |
Correct |
5 ms |
16720 KB |
Output is correct |
31 |
Correct |
5 ms |
16880 KB |
Output is correct |
32 |
Correct |
7 ms |
16720 KB |
Output is correct |
33 |
Correct |
5 ms |
16720 KB |
Output is correct |
34 |
Correct |
7 ms |
16888 KB |
Output is correct |
35 |
Correct |
5 ms |
16720 KB |
Output is correct |
36 |
Correct |
5 ms |
16720 KB |
Output is correct |
37 |
Correct |
5 ms |
16720 KB |
Output is correct |
38 |
Correct |
5 ms |
16720 KB |
Output is correct |
39 |
Correct |
5 ms |
16720 KB |
Output is correct |
40 |
Correct |
5 ms |
16720 KB |
Output is correct |
41 |
Correct |
2 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
991 ms |
31452 KB |
Output is correct |
2 |
Correct |
938 ms |
31408 KB |
Output is correct |
3 |
Correct |
948 ms |
31560 KB |
Output is correct |
4 |
Correct |
889 ms |
31300 KB |
Output is correct |
5 |
Correct |
924 ms |
31404 KB |
Output is correct |
6 |
Correct |
879 ms |
31440 KB |
Output is correct |
7 |
Correct |
1189 ms |
31440 KB |
Output is correct |
8 |
Correct |
1244 ms |
31560 KB |
Output is correct |
9 |
Correct |
1035 ms |
31444 KB |
Output is correct |
10 |
Correct |
887 ms |
31404 KB |
Output is correct |
11 |
Correct |
909 ms |
31304 KB |
Output is correct |
12 |
Correct |
967 ms |
31448 KB |
Output is correct |
13 |
Correct |
1012 ms |
31560 KB |
Output is correct |
14 |
Correct |
2 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
2 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16720 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
2 ms |
16720 KB |
Output is correct |
7 |
Correct |
3 ms |
16720 KB |
Output is correct |
8 |
Correct |
2 ms |
16720 KB |
Output is correct |
9 |
Correct |
2 ms |
16720 KB |
Output is correct |
10 |
Correct |
2 ms |
16720 KB |
Output is correct |
11 |
Correct |
5 ms |
16896 KB |
Output is correct |
12 |
Correct |
5 ms |
16732 KB |
Output is correct |
13 |
Correct |
5 ms |
16888 KB |
Output is correct |
14 |
Correct |
5 ms |
16720 KB |
Output is correct |
15 |
Correct |
5 ms |
16720 KB |
Output is correct |
16 |
Correct |
5 ms |
16720 KB |
Output is correct |
17 |
Correct |
5 ms |
17056 KB |
Output is correct |
18 |
Correct |
5 ms |
16720 KB |
Output is correct |
19 |
Correct |
5 ms |
16892 KB |
Output is correct |
20 |
Correct |
5 ms |
16720 KB |
Output is correct |
21 |
Correct |
5 ms |
16880 KB |
Output is correct |
22 |
Correct |
5 ms |
16720 KB |
Output is correct |
23 |
Correct |
5 ms |
16720 KB |
Output is correct |
24 |
Correct |
6 ms |
16720 KB |
Output is correct |
25 |
Correct |
5 ms |
16720 KB |
Output is correct |
26 |
Correct |
4 ms |
16720 KB |
Output is correct |
27 |
Correct |
5 ms |
16884 KB |
Output is correct |
28 |
Correct |
7 ms |
16720 KB |
Output is correct |
29 |
Correct |
5 ms |
16720 KB |
Output is correct |
30 |
Correct |
5 ms |
16720 KB |
Output is correct |
31 |
Correct |
5 ms |
16880 KB |
Output is correct |
32 |
Correct |
7 ms |
16720 KB |
Output is correct |
33 |
Correct |
5 ms |
16720 KB |
Output is correct |
34 |
Correct |
7 ms |
16888 KB |
Output is correct |
35 |
Correct |
5 ms |
16720 KB |
Output is correct |
36 |
Correct |
5 ms |
16720 KB |
Output is correct |
37 |
Correct |
5 ms |
16720 KB |
Output is correct |
38 |
Correct |
5 ms |
16720 KB |
Output is correct |
39 |
Correct |
5 ms |
16720 KB |
Output is correct |
40 |
Correct |
5 ms |
16720 KB |
Output is correct |
41 |
Correct |
2 ms |
6480 KB |
Output is correct |
42 |
Correct |
991 ms |
31452 KB |
Output is correct |
43 |
Correct |
938 ms |
31408 KB |
Output is correct |
44 |
Correct |
948 ms |
31560 KB |
Output is correct |
45 |
Correct |
889 ms |
31300 KB |
Output is correct |
46 |
Correct |
924 ms |
31404 KB |
Output is correct |
47 |
Correct |
879 ms |
31440 KB |
Output is correct |
48 |
Correct |
1189 ms |
31440 KB |
Output is correct |
49 |
Correct |
1244 ms |
31560 KB |
Output is correct |
50 |
Correct |
1035 ms |
31444 KB |
Output is correct |
51 |
Correct |
887 ms |
31404 KB |
Output is correct |
52 |
Correct |
909 ms |
31304 KB |
Output is correct |
53 |
Correct |
967 ms |
31448 KB |
Output is correct |
54 |
Correct |
1012 ms |
31560 KB |
Output is correct |
55 |
Correct |
2 ms |
6480 KB |
Output is correct |
56 |
Correct |
2375 ms |
29356 KB |
Output is correct |
57 |
Correct |
2310 ms |
43336 KB |
Output is correct |
58 |
Correct |
2335 ms |
44064 KB |
Output is correct |
59 |
Correct |
2274 ms |
44104 KB |
Output is correct |
60 |
Correct |
2312 ms |
43680 KB |
Output is correct |
61 |
Correct |
2374 ms |
44376 KB |
Output is correct |
62 |
Correct |
2382 ms |
44332 KB |
Output is correct |
63 |
Correct |
2190 ms |
44236 KB |
Output is correct |
64 |
Correct |
2461 ms |
44392 KB |
Output is correct |
65 |
Correct |
2326 ms |
43980 KB |
Output is correct |
66 |
Correct |
1802 ms |
43848 KB |
Output is correct |
67 |
Correct |
1849 ms |
44188 KB |
Output is correct |
68 |
Correct |
1745 ms |
43348 KB |
Output is correct |
69 |
Correct |
1783 ms |
44380 KB |
Output is correct |
70 |
Correct |
1841 ms |
43620 KB |
Output is correct |
71 |
Correct |
2279 ms |
42948 KB |
Output is correct |
72 |
Correct |
2263 ms |
43192 KB |
Output is correct |
73 |
Correct |
2260 ms |
43876 KB |
Output is correct |
74 |
Correct |
2413 ms |
43908 KB |
Output is correct |
75 |
Correct |
2424 ms |
44052 KB |
Output is correct |
76 |
Correct |
2455 ms |
44636 KB |
Output is correct |
77 |
Correct |
2 ms |
6480 KB |
Output is correct |