답안 #872344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
872344 2023-11-12T22:48:42 Z bemiko8984 Bitaro, who Leaps through Time (JOI19_timeleap) C++14
100 / 100
273 ms 48468 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 3e5 + 5;

inline int gi()
{
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	int sum = 0;
	while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
	return sum;
}

int n, q;
int l[maxn], r[maxn], op[maxn], a[maxn], b[maxn], c[maxn], d[maxn];
ll ans[maxn];

struct node
{
	int op, x, y;
	ll c;
} seq[maxn], val[maxn << 2];

node operator + (const node &a, const node &b)
{
	if (a.op) {
		if (b.op) return (node) {1, a.x, b.y, a.c + b.c + max(0, a.y - b.x)};
		else return (node) {1, a.x, max(b.x, min(b.y, a.y)), a.c + max(0, a.y - b.y)};
	} else {
		if (b.op) return (node) {1, min(a.y, max(a.x, b.x)), b.y, b.c + max(0, a.x - b.x)};
		else if (a.y < b.x) return (node) {1, a.y, b.x, 0};
		else if (a.x > b.y) return (node) {1, a.x, b.y, a.x - b.y};
		else return (node) {0, max(a.x, b.x), min(a.y, b.y), 0};
	}
}

#define mid ((l + r) >> 1)
#define lch (s << 1)
#define rch (s << 1 | 1)

void build(int s, int l, int r)
{
	if (l == r) return val[s] = seq[l], void();
	build(lch, l, mid);
	build(rch, mid + 1, r);
	val[s] = val[lch] + val[rch];
}

void modify(int s, int l, int r, int p, node v)
{
	if (l == r) return val[s] = v, void();
	if (p <= mid) modify(lch, l, mid, p, v);
	if (p >= mid + 1) modify(rch, mid + 1, r, p, v);
	val[s] = val[lch] + val[rch];
}

node query(int s, int l, int r, int ql, int qr)
{
	if (ql <= l && r <= qr) return val[s];
	if (qr <= mid) return query(lch, l, mid, ql, qr);
	else if (ql >= mid + 1) return query(rch, mid + 1, r, ql, qr);
	else return query(lch, l, mid, ql, qr) + query(rch, mid + 1, r, ql, qr);
}

void reverse()
{
	reverse(l + 1, l + n);
	reverse(r + 1, r + n);
	for (int i = 1; i <= q; ++i)
		if (op[i] == 1) a[i] = n - a[i];
		else a[i] = n + 1 - a[i], c[i] = n + 1 - c[i];
}

void solve()
{
	for (int i = 1; i < n; ++i) seq[i] = (node) {0, l[i] - i, r[i] - i - 1, 0};
	build(1, 1, n - 1);
	for (int i = 1; i <= q; ++i)
		if (op[i] == 1) modify(1, 1, n - 1, a[i], (node) {0, b[i] - a[i], c[i] - a[i] - 1, 0});
		else if (a[i] == c[i]) ans[i] = max(0, b[i] - d[i]);
		else if (a[i] < c[i]) ans[i] = ((node) {0, b[i] - a[i], b[i] - a[i], 0} + query(1, 1, n - 1, a[i], c[i] - 1) + (node) {d[i] - c[i], d[i] - c[i]}).c;
}

int main()
{
	n = gi(); q = gi();
	if (n == 1) {
		for (int i = 1; i <= q; ++i) {
			op[i] = gi(), a[i] = gi(), b[i] = gi(), c[i] = gi(), d[i] = op[i] == 2 ? gi() : 0;
			if (op[i] == 2) printf("%d\n", max(0, b[i] - d[i]));
		}
		return 0;
	}
	for (int i = 1; i < n; ++i) l[i] = gi(), r[i] = gi();
	for (int i = 1; i <= q; ++i) op[i] = gi(), a[i] = gi(), b[i] = gi(), c[i] = gi(), d[i] = op[i] == 2 ? gi() : 0;

	solve();
	reverse();
	solve();

	for (int i = 1; i <= q; ++i) if (op[i] == 2) printf("%lld\n", ans[i]);
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 1 ms 12716 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 1 ms 12636 KB Output is correct
6 Correct 1 ms 12636 KB Output is correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 1 ms 12636 KB Output is correct
10 Correct 1 ms 12788 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 2 ms 12636 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 2 ms 12636 KB Output is correct
23 Correct 2 ms 12636 KB Output is correct
24 Correct 2 ms 12636 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
26 Correct 2 ms 12796 KB Output is correct
27 Correct 2 ms 12636 KB Output is correct
28 Correct 2 ms 12636 KB Output is correct
29 Correct 2 ms 12636 KB Output is correct
30 Correct 2 ms 12636 KB Output is correct
31 Correct 2 ms 12636 KB Output is correct
32 Correct 2 ms 12636 KB Output is correct
33 Correct 3 ms 12636 KB Output is correct
34 Correct 2 ms 12636 KB Output is correct
35 Correct 2 ms 12636 KB Output is correct
36 Correct 2 ms 12888 KB Output is correct
37 Correct 2 ms 12636 KB Output is correct
38 Correct 2 ms 12636 KB Output is correct
39 Correct 2 ms 12636 KB Output is correct
40 Correct 2 ms 12636 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 48148 KB Output is correct
2 Correct 194 ms 35668 KB Output is correct
3 Correct 206 ms 35784 KB Output is correct
4 Correct 200 ms 35800 KB Output is correct
5 Correct 214 ms 48208 KB Output is correct
6 Correct 200 ms 48208 KB Output is correct
7 Correct 226 ms 48212 KB Output is correct
8 Correct 208 ms 48468 KB Output is correct
9 Correct 195 ms 35924 KB Output is correct
10 Correct 210 ms 48284 KB Output is correct
11 Correct 218 ms 48208 KB Output is correct
12 Correct 218 ms 48468 KB Output is correct
13 Correct 218 ms 48208 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 1 ms 12716 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 1 ms 12636 KB Output is correct
6 Correct 1 ms 12636 KB Output is correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 1 ms 12636 KB Output is correct
10 Correct 1 ms 12788 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 2 ms 12636 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 2 ms 12636 KB Output is correct
23 Correct 2 ms 12636 KB Output is correct
24 Correct 2 ms 12636 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
26 Correct 2 ms 12796 KB Output is correct
27 Correct 2 ms 12636 KB Output is correct
28 Correct 2 ms 12636 KB Output is correct
29 Correct 2 ms 12636 KB Output is correct
30 Correct 2 ms 12636 KB Output is correct
31 Correct 2 ms 12636 KB Output is correct
32 Correct 2 ms 12636 KB Output is correct
33 Correct 3 ms 12636 KB Output is correct
34 Correct 2 ms 12636 KB Output is correct
35 Correct 2 ms 12636 KB Output is correct
36 Correct 2 ms 12888 KB Output is correct
37 Correct 2 ms 12636 KB Output is correct
38 Correct 2 ms 12636 KB Output is correct
39 Correct 2 ms 12636 KB Output is correct
40 Correct 2 ms 12636 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 221 ms 48148 KB Output is correct
43 Correct 194 ms 35668 KB Output is correct
44 Correct 206 ms 35784 KB Output is correct
45 Correct 200 ms 35800 KB Output is correct
46 Correct 214 ms 48208 KB Output is correct
47 Correct 200 ms 48208 KB Output is correct
48 Correct 226 ms 48212 KB Output is correct
49 Correct 208 ms 48468 KB Output is correct
50 Correct 195 ms 35924 KB Output is correct
51 Correct 210 ms 48284 KB Output is correct
52 Correct 218 ms 48208 KB Output is correct
53 Correct 218 ms 48468 KB Output is correct
54 Correct 218 ms 48208 KB Output is correct
55 Correct 1 ms 6492 KB Output is correct
56 Correct 251 ms 46088 KB Output is correct
57 Correct 217 ms 33708 KB Output is correct
58 Correct 237 ms 46148 KB Output is correct
59 Correct 237 ms 46156 KB Output is correct
60 Correct 220 ms 33620 KB Output is correct
61 Correct 244 ms 46168 KB Output is correct
62 Correct 258 ms 46016 KB Output is correct
63 Correct 245 ms 46176 KB Output is correct
64 Correct 263 ms 46160 KB Output is correct
65 Correct 228 ms 46100 KB Output is correct
66 Correct 235 ms 46164 KB Output is correct
67 Correct 242 ms 46160 KB Output is correct
68 Correct 231 ms 46260 KB Output is correct
69 Correct 247 ms 46232 KB Output is correct
70 Correct 267 ms 33880 KB Output is correct
71 Correct 226 ms 33704 KB Output is correct
72 Correct 249 ms 46060 KB Output is correct
73 Correct 242 ms 46328 KB Output is correct
74 Correct 253 ms 46164 KB Output is correct
75 Correct 248 ms 46420 KB Output is correct
76 Correct 273 ms 46212 KB Output is correct
77 Correct 1 ms 6488 KB Output is correct