Submission #872354

#TimeUsernameProblemLanguageResultExecution timeMemory
872354bemiko8984Bitaro, who Leaps through Time (JOI19_timeleap)C++14
100 / 100
280 ms48464 KiB
#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) {0, (int)-1e9, d[i] - c[i], 0}).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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...