Submission #211500

# Submission time Handle Problem Language Result Execution time Memory
211500 2020-03-20T15:47:30 Z DrumpfTheGodEmperor Bitaro, who Leaps through Time (JOI19_timeleap) C++14
0 / 100
611 ms 524292 KB
#include <bits/stdc++.h>
#define int long long
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 3e5 + 5;
int n, q, le[N], ri[N];
class SegTree {
private:
	struct Node {
		int st, en, len, cost;
		//We start at [st....st + len], and ends at [en...en + len]
		Node(int st = -1, int en = -1, int len = 0, int cost = 0): st(st), en(en), len(len), cost(cost) {};
		int get(int x, int y) {
			int ans = cost;
			if (x < st) x = st;
			if (x > st + len) {
//				cout << "x = " << x << " st + len = " << st + len << "\n";
				ans += (x - st - len);
				x = st + len;
			}
			x += (en - st);
			if (y < x) ans += (x - y);
			return ans;
		}
	} t[N << 2];
	friend Node operator+(Node lhs, Node rhs) {
		if (lhs.st == -1) return rhs;
		if (rhs.st == -1) return lhs;
		//2 sections does not intersect
		if (lhs.en + lhs.len < rhs.st) {
			return Node(lhs.st + lhs.len, rhs.en, 0, lhs.cost + rhs.cost);
		} else if (rhs.st + rhs.len < lhs.en) {
			return Node(lhs.st, rhs.en + rhs.len, 0, lhs.cost + rhs.cost + (lhs.en - rhs.st - rhs.len));
		} else {
			//2 sections [lhs.en...lhs.en + lhs.len] and [rhs.st....rhs.st + rhs.len] do intersect.
			int l = max(lhs.en, rhs.st), r = min(lhs.en + lhs.len, rhs.st + rhs.len);
			int diffl = lhs.en - lhs.st, diffr = rhs.en - rhs.st;
			
			return Node(l - diffl, l + diffr, r - l, lhs.cost + rhs.cost);
		}
	}
	void build(int l, int r, int x) {
		if (l == r) {
			if (!rev) t[x] = Node(le[l], le[l] + 1, ri[l] - le[l], 0);
			else t[x] = Node(le[n - 1 - l], le[n - 1 - l] + 1, ri[n - 1 - l] - le[n - 1 - l], 0);
//			cout << "t[" << l << "->" << r << "].st = " << t[x].st << " en = " << t[x].en << " len = " << t[x].len << "\n";
			return;
		}
		int m = (l + r) >> 1;
		build(l, m, x << 1), build(m + 1, r, x << 1 | 1);
		t[x] = t[x << 1] + t[x << 1 | 1];
//		cout << "t[" << l << "->" << r << "].st = " << t[x].st << " en = " << t[x].en << " len = " << t[x].len << "\n";
	}
	void upd(int l, int r, int pos, int x) {
		if (l == r) {
			if (!rev) t[x] = Node(le[l], le[l] + 1, ri[l] - le[l], 0);
			else t[x] = Node(le[n - 1 - l], le[n - 1 - l] + 1, ri[n - 1 - l] - le[n - 1 - l], 0);
			return;
		}
		int m = (l + r) >> 1;
		if (pos <= m) upd(l, m, pos, x << 1);
		else upd(m + 1, r, pos, x << 1 | 1);
		t[x] = t[x << 1] + t[x << 1 | 1];
//		cout << "t[" << x << "].st = " << t[x].st << "\n";
	}
	Node get(int l, int r, int s, int e, int x) {
		if (l > e || r < s) return Node();
		if (s <= l && r <= e) return t[x];
		int m = (l + r) >> 1;
		return get(l, m, s, e, x << 1) + get(m + 1, r, s, e, x << 1 | 1);
	}
public:
	int n;
	bool rev;
	void init(int _n, bool _rev) {
		n = _n;
		rev = _rev;
		build(0, n - 1, 1);
	}
	void upd(int pos) {
		if (!rev) upd(0, n - 1, pos, 1);
		else upd(0, n - 1, n - 1 - pos, 1);
	}
	int get(int a, int b, int x, int y) {
//		cout << "a = " << a << " b = " << b << "\n";
		Node ans = get(0, n - 1, a, b - 1, 1);
//		cout << ans.st << " " << ans.en << " " << ans.len << " cost = " << ans.cost << "\n";
		return ans.get(x, y);
	}
} st, st2;
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> q;
	fw (i, 0, n - 1) {
		cin >> le[i] >> ri[i];
		ri[i]--;
	}
//	We want to go from city A to B, there is some segment of time that allows us to spend the minimum cost.
//	Everything outside the range can be moved into the range and spend the aforementioned cost.
	st.init(n - 1, 0);
//	cout << "Revtree\n";
	st2.init(n - 1, 1);
	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int p, s, e;
			cin >> p >> s >> e;
			p--;
			le[p] = s, ri[p] = e - 1;
			st.upd(p);
			st2.upd(p);
		} else {
			int a, b, x, y;
			cin >> a >> x >> b >> y;
			if (a == b) {
				if (y < x) cout << x - y << "\n";
				else cout << "0\n";
				continue;
			}
			if (a < b) {
				a--, b--;
				cout << st.get(a, b, x, y) << "\n";
			} else {
				a--, b--;
				a = n - 1 - a, b = n - 1 - b;
				cout << st2.get(a, b, x, y) << "\n";
			}
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 75512 KB Output is correct
2 Correct 36 ms 75392 KB Output is correct
3 Correct 36 ms 75512 KB Output is correct
4 Correct 37 ms 75520 KB Output is correct
5 Correct 37 ms 75520 KB Output is correct
6 Correct 37 ms 75512 KB Output is correct
7 Correct 37 ms 75512 KB Output is correct
8 Correct 36 ms 75452 KB Output is correct
9 Correct 37 ms 75488 KB Output is correct
10 Correct 37 ms 75512 KB Output is correct
11 Correct 38 ms 75648 KB Output is correct
12 Correct 38 ms 75640 KB Output is correct
13 Correct 38 ms 75512 KB Output is correct
14 Correct 38 ms 75520 KB Output is correct
15 Correct 37 ms 75640 KB Output is correct
16 Correct 39 ms 75520 KB Output is correct
17 Correct 38 ms 75640 KB Output is correct
18 Correct 38 ms 75648 KB Output is correct
19 Correct 40 ms 75640 KB Output is correct
20 Correct 37 ms 75512 KB Output is correct
21 Correct 38 ms 75520 KB Output is correct
22 Correct 37 ms 75520 KB Output is correct
23 Correct 37 ms 75640 KB Output is correct
24 Correct 38 ms 75512 KB Output is correct
25 Correct 37 ms 75520 KB Output is correct
26 Correct 38 ms 75520 KB Output is correct
27 Correct 37 ms 75520 KB Output is correct
28 Correct 38 ms 75512 KB Output is correct
29 Correct 39 ms 75640 KB Output is correct
30 Correct 37 ms 75640 KB Output is correct
31 Correct 40 ms 75640 KB Output is correct
32 Correct 37 ms 75640 KB Output is correct
33 Correct 38 ms 75512 KB Output is correct
34 Correct 38 ms 75512 KB Output is correct
35 Correct 64 ms 75512 KB Output is correct
36 Correct 38 ms 75512 KB Output is correct
37 Correct 39 ms 75520 KB Output is correct
38 Correct 38 ms 75640 KB Output is correct
39 Correct 37 ms 75512 KB Output is correct
40 Correct 38 ms 75648 KB Output is correct
41 Runtime error 250 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 604 ms 99664 KB Output is correct
2 Correct 600 ms 99212 KB Output is correct
3 Correct 590 ms 99192 KB Output is correct
4 Correct 570 ms 98808 KB Output is correct
5 Correct 595 ms 99580 KB Output is correct
6 Correct 611 ms 99320 KB Output is correct
7 Correct 595 ms 99740 KB Output is correct
8 Correct 608 ms 100504 KB Output is correct
9 Correct 588 ms 99132 KB Output is correct
10 Correct 594 ms 99832 KB Output is correct
11 Correct 592 ms 99580 KB Output is correct
12 Correct 607 ms 100344 KB Output is correct
13 Correct 598 ms 100576 KB Output is correct
14 Runtime error 248 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 37 ms 75512 KB Output is correct
2 Correct 36 ms 75392 KB Output is correct
3 Correct 36 ms 75512 KB Output is correct
4 Correct 37 ms 75520 KB Output is correct
5 Correct 37 ms 75520 KB Output is correct
6 Correct 37 ms 75512 KB Output is correct
7 Correct 37 ms 75512 KB Output is correct
8 Correct 36 ms 75452 KB Output is correct
9 Correct 37 ms 75488 KB Output is correct
10 Correct 37 ms 75512 KB Output is correct
11 Correct 38 ms 75648 KB Output is correct
12 Correct 38 ms 75640 KB Output is correct
13 Correct 38 ms 75512 KB Output is correct
14 Correct 38 ms 75520 KB Output is correct
15 Correct 37 ms 75640 KB Output is correct
16 Correct 39 ms 75520 KB Output is correct
17 Correct 38 ms 75640 KB Output is correct
18 Correct 38 ms 75648 KB Output is correct
19 Correct 40 ms 75640 KB Output is correct
20 Correct 37 ms 75512 KB Output is correct
21 Correct 38 ms 75520 KB Output is correct
22 Correct 37 ms 75520 KB Output is correct
23 Correct 37 ms 75640 KB Output is correct
24 Correct 38 ms 75512 KB Output is correct
25 Correct 37 ms 75520 KB Output is correct
26 Correct 38 ms 75520 KB Output is correct
27 Correct 37 ms 75520 KB Output is correct
28 Correct 38 ms 75512 KB Output is correct
29 Correct 39 ms 75640 KB Output is correct
30 Correct 37 ms 75640 KB Output is correct
31 Correct 40 ms 75640 KB Output is correct
32 Correct 37 ms 75640 KB Output is correct
33 Correct 38 ms 75512 KB Output is correct
34 Correct 38 ms 75512 KB Output is correct
35 Correct 64 ms 75512 KB Output is correct
36 Correct 38 ms 75512 KB Output is correct
37 Correct 39 ms 75520 KB Output is correct
38 Correct 38 ms 75640 KB Output is correct
39 Correct 37 ms 75512 KB Output is correct
40 Correct 38 ms 75648 KB Output is correct
41 Runtime error 250 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)