Submission #211534

# Submission time Handle Problem Language Result Execution time Memory
211534 2020-03-20T16:55:55 Z DrumpfTheGodEmperor Bitaro, who Leaps through Time (JOI19_timeleap) C++14
0 / 100
647 ms 524292 KB
#include <bits/stdc++.h>
#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;
typedef long long ll;
const int N = 3e5 + 5;
int le[N], ri[N];
class SegTree {
private:
	struct Node {
		int st, en, len;
		ll cost;
		//We start at [st....st + len], and ends at [en...en + len]
		Node(int st = -1, int en = -1, int len = 0, ll cost = 0): st(st), en(en), len(len), cost(cost) {};
		ll get(int x, int y) {
			ll 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);
	}
	ll 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;
int n, q;
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 31 ms 56704 KB Output is correct
2 Correct 31 ms 56704 KB Output is correct
3 Correct 29 ms 56704 KB Output is correct
4 Correct 30 ms 56696 KB Output is correct
5 Correct 29 ms 56704 KB Output is correct
6 Correct 31 ms 56704 KB Output is correct
7 Correct 31 ms 56704 KB Output is correct
8 Correct 30 ms 56704 KB Output is correct
9 Correct 30 ms 56696 KB Output is correct
10 Correct 30 ms 56704 KB Output is correct
11 Correct 32 ms 56696 KB Output is correct
12 Correct 31 ms 56704 KB Output is correct
13 Correct 31 ms 56824 KB Output is correct
14 Correct 31 ms 56832 KB Output is correct
15 Correct 31 ms 56832 KB Output is correct
16 Correct 30 ms 56696 KB Output is correct
17 Correct 31 ms 56824 KB Output is correct
18 Correct 31 ms 56824 KB Output is correct
19 Correct 31 ms 56704 KB Output is correct
20 Correct 32 ms 56824 KB Output is correct
21 Correct 32 ms 56832 KB Output is correct
22 Correct 31 ms 56704 KB Output is correct
23 Correct 33 ms 56704 KB Output is correct
24 Correct 33 ms 56952 KB Output is correct
25 Correct 32 ms 56824 KB Output is correct
26 Correct 31 ms 56700 KB Output is correct
27 Correct 33 ms 56704 KB Output is correct
28 Correct 31 ms 56828 KB Output is correct
29 Correct 32 ms 56704 KB Output is correct
30 Correct 32 ms 56824 KB Output is correct
31 Correct 31 ms 56960 KB Output is correct
32 Correct 30 ms 56704 KB Output is correct
33 Correct 34 ms 56832 KB Output is correct
34 Correct 31 ms 56704 KB Output is correct
35 Correct 31 ms 56704 KB Output is correct
36 Correct 32 ms 56704 KB Output is correct
37 Correct 31 ms 56704 KB Output is correct
38 Correct 31 ms 56824 KB Output is correct
39 Correct 32 ms 56696 KB Output is correct
40 Correct 31 ms 56704 KB Output is correct
41 Runtime error 265 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 611 ms 64548 KB Output is correct
2 Correct 615 ms 64376 KB Output is correct
3 Correct 601 ms 64504 KB Output is correct
4 Correct 603 ms 64252 KB Output is correct
5 Correct 636 ms 64412 KB Output is correct
6 Correct 647 ms 64416 KB Output is correct
7 Correct 631 ms 64596 KB Output is correct
8 Correct 618 ms 64632 KB Output is correct
9 Correct 609 ms 64504 KB Output is correct
10 Correct 642 ms 64504 KB Output is correct
11 Correct 628 ms 64380 KB Output is correct
12 Correct 637 ms 64760 KB Output is correct
13 Correct 620 ms 64756 KB Output is correct
14 Runtime error 256 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 31 ms 56704 KB Output is correct
2 Correct 31 ms 56704 KB Output is correct
3 Correct 29 ms 56704 KB Output is correct
4 Correct 30 ms 56696 KB Output is correct
5 Correct 29 ms 56704 KB Output is correct
6 Correct 31 ms 56704 KB Output is correct
7 Correct 31 ms 56704 KB Output is correct
8 Correct 30 ms 56704 KB Output is correct
9 Correct 30 ms 56696 KB Output is correct
10 Correct 30 ms 56704 KB Output is correct
11 Correct 32 ms 56696 KB Output is correct
12 Correct 31 ms 56704 KB Output is correct
13 Correct 31 ms 56824 KB Output is correct
14 Correct 31 ms 56832 KB Output is correct
15 Correct 31 ms 56832 KB Output is correct
16 Correct 30 ms 56696 KB Output is correct
17 Correct 31 ms 56824 KB Output is correct
18 Correct 31 ms 56824 KB Output is correct
19 Correct 31 ms 56704 KB Output is correct
20 Correct 32 ms 56824 KB Output is correct
21 Correct 32 ms 56832 KB Output is correct
22 Correct 31 ms 56704 KB Output is correct
23 Correct 33 ms 56704 KB Output is correct
24 Correct 33 ms 56952 KB Output is correct
25 Correct 32 ms 56824 KB Output is correct
26 Correct 31 ms 56700 KB Output is correct
27 Correct 33 ms 56704 KB Output is correct
28 Correct 31 ms 56828 KB Output is correct
29 Correct 32 ms 56704 KB Output is correct
30 Correct 32 ms 56824 KB Output is correct
31 Correct 31 ms 56960 KB Output is correct
32 Correct 30 ms 56704 KB Output is correct
33 Correct 34 ms 56832 KB Output is correct
34 Correct 31 ms 56704 KB Output is correct
35 Correct 31 ms 56704 KB Output is correct
36 Correct 32 ms 56704 KB Output is correct
37 Correct 31 ms 56704 KB Output is correct
38 Correct 31 ms 56824 KB Output is correct
39 Correct 32 ms 56696 KB Output is correct
40 Correct 31 ms 56704 KB Output is correct
41 Runtime error 265 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)