답안 #206688

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
206688 2020-03-04T14:09:45 Z atoiz Bitaro, who Leaps through Time (JOI19_timeleap) C++14
100 / 100
554 ms 49016 KB
#include <iostream>
#include <cassert>

using namespace std;

const int MAXN = 300007, INF = 1e9 + 7;

struct Path
{
	int from, to, len;
	int64_t cost;

	Path(int _from = 0, int _to = 0, int _len = INF, int64_t _cost = 0):
		from(_from), to(_to), len(_len), cost(_cost) {}

	void from_edge(int l, int r)
	{
		cost = 1;
		from = l, to = l + 1, len = r - l;
	}

	friend Path operator+ (Path lhs, Path rhs)
	{
		if (lhs.to + lhs.len < rhs.from) {
			return {lhs.from + lhs.len, rhs.to, 0, lhs.cost + (rhs.from - (lhs.to + lhs.len)) + rhs.cost};
		} else if (rhs.from + rhs.len < lhs.to) {
			return {lhs.from, rhs.to + rhs.len, 0, lhs.cost + (lhs.to - (rhs.from + rhs.len)) + rhs.cost};
		} else {
			if (lhs.to < rhs.from) {
				int d = rhs.from - lhs.to;
				lhs.from += d, lhs.to += d, lhs.len -= d;
			} else {
				int d = lhs.to - rhs.from;
				rhs.from += d, rhs.to += d, rhs.len -= d;
			}

			if (lhs.to + lhs.len < rhs.from + rhs.len) {
				rhs.len -= (rhs.from + rhs.len) - (lhs.to + lhs.len);
			} else {
				lhs.len -= (lhs.to + lhs.len) - (rhs.from + rhs.len);
			}

			assert(lhs.len == rhs.len);
			return {lhs.from, rhs.to, lhs.len, lhs.cost + rhs.cost};
		}
	}

	int64_t get(int x, int y)
	{
		int64_t ans = cost;

		int pos = x;
		if (pos < from) ans += from - pos, pos = from;
		if (pos > from + len) ans += pos - (from + len), pos = from + len;

		pos += to - from;
		ans += abs(pos - y);

		ans = (ans - (y - x)) / 2;
		return ans;
	}
} fw[MAXN << 1], bw[MAXN << 1];

int N, Q;

void modify(int i, int l, int r)
{
	for (i += N, fw[i].from_edge(l, r), bw[i].from_edge(l, r), i >>= 1; i >= 1; i >>= 1) {
		fw[i] = fw[i << 1] + fw[i << 1 | 1];
		bw[i] = bw[i << 1 | 1] + bw[i << 1];
	}
}

Path get_fw(int l, int r)
{
	Path lef, rig;
	for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
		if (l & 1) lef = lef + fw[l++];
		if (r & 1) rig = fw[--r] + rig;
	}
	return lef + rig;
}

Path get_bw(int l, int r)
{
	Path lef, rig;
	for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
		if (l & 1) rig = bw[l++] + rig;
		if (r & 1) lef = lef + bw[--r];
	}
	return lef + rig;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> N >> Q;
	for (int i = 1; i < N; ++i) {
		int l, r;
		cin >> l >> r;
		modify(i, l, r - 1);
	}

	for (int i = N - 1; i >= 1; --i) {
		fw[i] = fw[i << 1] + fw[i << 1 | 1];
		bw[i] = bw[i << 1 | 1] + bw[i << 1];
	}

	while (Q--) {
		int t;
		cin >> t;
		if (t == 1) {
			int i, l, r;
			cin >> i >> l >> r;
			modify(i, l, r - 1);
		} else {
			int l, r, x, y;
			cin >> l >> x >> r >> y;
			if (l <= r) cout << get_fw(l, r).get(x, y) << '\n';
			else cout << get_bw(r, l).get(x, y) << '\n';
		}
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 28536 KB Output is correct
2 Correct 21 ms 28536 KB Output is correct
3 Correct 21 ms 28408 KB Output is correct
4 Correct 21 ms 28536 KB Output is correct
5 Correct 21 ms 28664 KB Output is correct
6 Correct 20 ms 28664 KB Output is correct
7 Correct 22 ms 28524 KB Output is correct
8 Correct 20 ms 28536 KB Output is correct
9 Correct 20 ms 28536 KB Output is correct
10 Correct 20 ms 28536 KB Output is correct
11 Correct 21 ms 28536 KB Output is correct
12 Correct 22 ms 28536 KB Output is correct
13 Correct 21 ms 28540 KB Output is correct
14 Correct 21 ms 28536 KB Output is correct
15 Correct 20 ms 28536 KB Output is correct
16 Correct 21 ms 28540 KB Output is correct
17 Correct 21 ms 28536 KB Output is correct
18 Correct 21 ms 28536 KB Output is correct
19 Correct 21 ms 28536 KB Output is correct
20 Correct 21 ms 28536 KB Output is correct
21 Correct 21 ms 28664 KB Output is correct
22 Correct 21 ms 28536 KB Output is correct
23 Correct 22 ms 28536 KB Output is correct
24 Correct 22 ms 28536 KB Output is correct
25 Correct 20 ms 28536 KB Output is correct
26 Correct 21 ms 28536 KB Output is correct
27 Correct 20 ms 28536 KB Output is correct
28 Correct 21 ms 28664 KB Output is correct
29 Correct 21 ms 28536 KB Output is correct
30 Correct 22 ms 28664 KB Output is correct
31 Correct 21 ms 28536 KB Output is correct
32 Correct 21 ms 28536 KB Output is correct
33 Correct 21 ms 28536 KB Output is correct
34 Correct 22 ms 28792 KB Output is correct
35 Correct 21 ms 28664 KB Output is correct
36 Correct 21 ms 28536 KB Output is correct
37 Correct 21 ms 28536 KB Output is correct
38 Correct 21 ms 28536 KB Output is correct
39 Correct 27 ms 28668 KB Output is correct
40 Correct 22 ms 28540 KB Output is correct
41 Correct 20 ms 28536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 529 ms 48376 KB Output is correct
2 Correct 513 ms 47964 KB Output is correct
3 Correct 529 ms 48120 KB Output is correct
4 Correct 511 ms 47864 KB Output is correct
5 Correct 507 ms 48328 KB Output is correct
6 Correct 522 ms 48380 KB Output is correct
7 Correct 506 ms 48248 KB Output is correct
8 Correct 528 ms 49016 KB Output is correct
9 Correct 507 ms 48248 KB Output is correct
10 Correct 520 ms 48504 KB Output is correct
11 Correct 549 ms 48248 KB Output is correct
12 Correct 535 ms 48880 KB Output is correct
13 Correct 536 ms 48828 KB Output is correct
14 Correct 22 ms 28664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 28536 KB Output is correct
2 Correct 21 ms 28536 KB Output is correct
3 Correct 21 ms 28408 KB Output is correct
4 Correct 21 ms 28536 KB Output is correct
5 Correct 21 ms 28664 KB Output is correct
6 Correct 20 ms 28664 KB Output is correct
7 Correct 22 ms 28524 KB Output is correct
8 Correct 20 ms 28536 KB Output is correct
9 Correct 20 ms 28536 KB Output is correct
10 Correct 20 ms 28536 KB Output is correct
11 Correct 21 ms 28536 KB Output is correct
12 Correct 22 ms 28536 KB Output is correct
13 Correct 21 ms 28540 KB Output is correct
14 Correct 21 ms 28536 KB Output is correct
15 Correct 20 ms 28536 KB Output is correct
16 Correct 21 ms 28540 KB Output is correct
17 Correct 21 ms 28536 KB Output is correct
18 Correct 21 ms 28536 KB Output is correct
19 Correct 21 ms 28536 KB Output is correct
20 Correct 21 ms 28536 KB Output is correct
21 Correct 21 ms 28664 KB Output is correct
22 Correct 21 ms 28536 KB Output is correct
23 Correct 22 ms 28536 KB Output is correct
24 Correct 22 ms 28536 KB Output is correct
25 Correct 20 ms 28536 KB Output is correct
26 Correct 21 ms 28536 KB Output is correct
27 Correct 20 ms 28536 KB Output is correct
28 Correct 21 ms 28664 KB Output is correct
29 Correct 21 ms 28536 KB Output is correct
30 Correct 22 ms 28664 KB Output is correct
31 Correct 21 ms 28536 KB Output is correct
32 Correct 21 ms 28536 KB Output is correct
33 Correct 21 ms 28536 KB Output is correct
34 Correct 22 ms 28792 KB Output is correct
35 Correct 21 ms 28664 KB Output is correct
36 Correct 21 ms 28536 KB Output is correct
37 Correct 21 ms 28536 KB Output is correct
38 Correct 21 ms 28536 KB Output is correct
39 Correct 27 ms 28668 KB Output is correct
40 Correct 22 ms 28540 KB Output is correct
41 Correct 20 ms 28536 KB Output is correct
42 Correct 529 ms 48376 KB Output is correct
43 Correct 513 ms 47964 KB Output is correct
44 Correct 529 ms 48120 KB Output is correct
45 Correct 511 ms 47864 KB Output is correct
46 Correct 507 ms 48328 KB Output is correct
47 Correct 522 ms 48380 KB Output is correct
48 Correct 506 ms 48248 KB Output is correct
49 Correct 528 ms 49016 KB Output is correct
50 Correct 507 ms 48248 KB Output is correct
51 Correct 520 ms 48504 KB Output is correct
52 Correct 549 ms 48248 KB Output is correct
53 Correct 535 ms 48880 KB Output is correct
54 Correct 536 ms 48828 KB Output is correct
55 Correct 22 ms 28664 KB Output is correct
56 Correct 504 ms 45204 KB Output is correct
57 Correct 483 ms 44796 KB Output is correct
58 Correct 517 ms 45304 KB Output is correct
59 Correct 500 ms 45560 KB Output is correct
60 Correct 502 ms 44916 KB Output is correct
61 Correct 509 ms 45688 KB Output is correct
62 Correct 523 ms 45832 KB Output is correct
63 Correct 512 ms 45688 KB Output is correct
64 Correct 519 ms 45932 KB Output is correct
65 Correct 501 ms 45304 KB Output is correct
66 Correct 503 ms 45412 KB Output is correct
67 Correct 505 ms 45820 KB Output is correct
68 Correct 476 ms 44692 KB Output is correct
69 Correct 504 ms 45560 KB Output is correct
70 Correct 491 ms 45180 KB Output is correct
71 Correct 497 ms 44152 KB Output is correct
72 Correct 491 ms 44536 KB Output is correct
73 Correct 554 ms 45176 KB Output is correct
74 Correct 520 ms 45176 KB Output is correct
75 Correct 526 ms 45320 KB Output is correct
76 Correct 539 ms 45816 KB Output is correct
77 Correct 23 ms 28536 KB Output is correct