답안 #594992

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594992 2022-07-13T08:45:18 Z balbit Bitaro, who Leaps through Time (JOI19_timeleap) C++14
100 / 100
1087 ms 94512 KB
#include <vector>
#include <bits/stdc++.h>
#undef BALBIT
#define int ll
using namespace std;
 
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
 
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
 
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define pb push_back
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
 
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
 
#endif // BALBIT

struct state{
	int L, R; // (-oo, L] maps to Lto, (L,R] maps to y+len, (R, oo) maps to Rto
	int len;
	int Lto, Rto;
	int Lval, Rval; // Lval is Lval, R's value is y+Rval
	pii eval(int y) { // {point to, cost}
		if (y <= L) {
			return {Lto, Lval};
		}
		if (y > R) {
			return {Rto, y + Rval};
		}
		return {y+len, 0};
	}

};

state base(int L, int R) {
	return {L,R-1,1,L+1,R-1+1,0,-(R-1)};
}

state merge (state a, state b){
	state re;
	re.len = a.len + b.len;
	int oldL=a.Lval, oldR=a.Rval;
	bool eq = a.L == a.R;

	if (b.L - a.len >= a.R) {
		a.L = a.R;
	}else if (b.R - a.len <= a.L) {
		a.R = a.L;
	}else{
		MX(a.L, b.L - a.len);
		MN(a.R, b.R - a.len);                  
	}
	if (!eq){
		pii ga = b.eval(a.L + a.len);
		pii gb = b.eval(a.R + a.len);
		re.Lto = ga.f, re.Lval = ga.s;
		re.Rto = gb.f, re.Rval = gb.s - a.R;
	}else{
		pii get = b.eval(a.Lto);
		re.Lto = get.f, re.Lval = get.s + oldL;
		re.Rto = get.f, re.Rval = get.s + oldR;
	}
	re.L = a.L; re.R = a.R;
	return re;
}

const int maxn = 3e5+5;

pii A[maxn];
state s[maxn*4];

void build(int o, int l, int r) {
	if (l == r) {
		s[o] = base(A[l].f, A[l].s);
		return;
	}
	int mid = (l+r)/2;
	build(o*2,l,mid); build(o*2+1,mid+1,r);
	s[o] = merge(s[o*2], s[o*2+1]);
}

pii QU(int L, int R, int pt, int o, int l, int r) {
	if (l > R || r < L) return {pt,0};
	if (l >= L && r <= R) {
		return s[o].eval(pt);
	}
	int mid = (l+r)/2;       
	pii gl = QU(L,R,pt,o*2,l,mid);
	pii gr = QU(L,R,gl.f,o*2+1,mid+1,r);
	gr.s += gl.s;
	return gr;
}

void MO(int p, pii v, int o, int l, int r) {
	if (l > p || r < p) return;
	if (l==r) {
		s[o] = base(v.f, v.s);
		return;
	}
	int mid = (l+r)/2;
	MO(p,v,o*2,l,mid); MO(p,v,o*2+1,mid+1,r);
	s[o] = merge(s[o*2], s[o*2+1]);
}

struct query{
	int l, r, t1, t2; // if l == -1, it's a modify operation on point r, range [t1,t2]
};

query ask[maxn];
int ans[maxn];

signed main(){
	ios::sync_with_stdio(0), cin.tie(0);
	bug(1,2);

	int n,q; cin>>n>>q;
	REP(i,n-1) {
		cin>>A[i].f>>A[i].s;
	}

	REP(i,q) {
		int T; cin>>T;
		if (T == 1) {
			int p,s,e; cin>>p>>s>>e;
			--p;
			ask[i] = {-1, p, s, e};
		}else{
			int l,r,t1,t2;
			cin>>l>>t1>>r>>t2;
			--l; --r;
			ask[i] = {l,r,t1,t2};
		}
	}
	memset(ans, -1, sizeof ans);
	REP(round, 2) {
		build(1,0,n-1);
		REP(i,q) {
			query Q = ask[i];
			if (Q.l == Q.r) {
				ans[i] = max(0ll, Q.t1 - Q.t2);
			}
			if (Q.l < Q.r) {
				if (Q.l == -1) {
					MO(Q.r, make_pair(Q.t1, Q.t2), 1,0,n-1);
				}else{
					pii ha = QU(Q.l, Q.r-1, Q.t1, 1, 0, n-1);
					bug(i, ha.f, ha.s);
					int re = ha.s + max(0ll, ha.f - Q.t2);
					ans[i] = re;
				}
			}
		}

		reverse(A, A+n-1);
		for (query &qqq : ask) {
			if (qqq.l == -1) {
				qqq.r = n-qqq.r-2;
			}else{
				qqq.l = n-qqq.l-1;
				qqq.r = n-qqq.r-1;
			}
			
		}
	}

	REP(i,q) if (ans[i] != -1) cout<<ans[i]<<endl;
}








# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 11988 KB Output is correct
2 Correct 10 ms 11996 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11972 KB Output is correct
5 Correct 8 ms 12080 KB Output is correct
6 Correct 9 ms 11988 KB Output is correct
7 Correct 7 ms 11972 KB Output is correct
8 Correct 8 ms 11988 KB Output is correct
9 Correct 8 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 9 ms 12244 KB Output is correct
12 Correct 14 ms 12244 KB Output is correct
13 Correct 10 ms 12244 KB Output is correct
14 Correct 12 ms 12204 KB Output is correct
15 Correct 12 ms 12244 KB Output is correct
16 Correct 10 ms 12244 KB Output is correct
17 Correct 10 ms 12244 KB Output is correct
18 Correct 10 ms 12192 KB Output is correct
19 Correct 9 ms 12224 KB Output is correct
20 Correct 9 ms 12256 KB Output is correct
21 Correct 9 ms 12168 KB Output is correct
22 Correct 9 ms 12244 KB Output is correct
23 Correct 10 ms 12244 KB Output is correct
24 Correct 9 ms 12264 KB Output is correct
25 Correct 9 ms 12228 KB Output is correct
26 Correct 9 ms 12244 KB Output is correct
27 Correct 9 ms 12248 KB Output is correct
28 Correct 9 ms 12244 KB Output is correct
29 Correct 9 ms 12244 KB Output is correct
30 Correct 9 ms 12244 KB Output is correct
31 Correct 10 ms 12164 KB Output is correct
32 Correct 9 ms 12244 KB Output is correct
33 Correct 9 ms 12248 KB Output is correct
34 Correct 9 ms 12244 KB Output is correct
35 Correct 10 ms 12244 KB Output is correct
36 Correct 9 ms 12152 KB Output is correct
37 Correct 9 ms 12244 KB Output is correct
38 Correct 10 ms 12200 KB Output is correct
39 Correct 9 ms 12244 KB Output is correct
40 Correct 9 ms 12244 KB Output is correct
41 Correct 8 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 828 ms 77968 KB Output is correct
2 Correct 766 ms 64332 KB Output is correct
3 Correct 775 ms 64364 KB Output is correct
4 Correct 781 ms 64072 KB Output is correct
5 Correct 806 ms 93592 KB Output is correct
6 Correct 776 ms 85860 KB Output is correct
7 Correct 790 ms 93520 KB Output is correct
8 Correct 842 ms 94512 KB Output is correct
9 Correct 853 ms 64384 KB Output is correct
10 Correct 897 ms 93684 KB Output is correct
11 Correct 861 ms 93544 KB Output is correct
12 Correct 843 ms 94360 KB Output is correct
13 Correct 917 ms 94492 KB Output is correct
14 Correct 8 ms 11972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 11988 KB Output is correct
2 Correct 10 ms 11996 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 7 ms 11972 KB Output is correct
5 Correct 8 ms 12080 KB Output is correct
6 Correct 9 ms 11988 KB Output is correct
7 Correct 7 ms 11972 KB Output is correct
8 Correct 8 ms 11988 KB Output is correct
9 Correct 8 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 9 ms 12244 KB Output is correct
12 Correct 14 ms 12244 KB Output is correct
13 Correct 10 ms 12244 KB Output is correct
14 Correct 12 ms 12204 KB Output is correct
15 Correct 12 ms 12244 KB Output is correct
16 Correct 10 ms 12244 KB Output is correct
17 Correct 10 ms 12244 KB Output is correct
18 Correct 10 ms 12192 KB Output is correct
19 Correct 9 ms 12224 KB Output is correct
20 Correct 9 ms 12256 KB Output is correct
21 Correct 9 ms 12168 KB Output is correct
22 Correct 9 ms 12244 KB Output is correct
23 Correct 10 ms 12244 KB Output is correct
24 Correct 9 ms 12264 KB Output is correct
25 Correct 9 ms 12228 KB Output is correct
26 Correct 9 ms 12244 KB Output is correct
27 Correct 9 ms 12248 KB Output is correct
28 Correct 9 ms 12244 KB Output is correct
29 Correct 9 ms 12244 KB Output is correct
30 Correct 9 ms 12244 KB Output is correct
31 Correct 10 ms 12164 KB Output is correct
32 Correct 9 ms 12244 KB Output is correct
33 Correct 9 ms 12248 KB Output is correct
34 Correct 9 ms 12244 KB Output is correct
35 Correct 10 ms 12244 KB Output is correct
36 Correct 9 ms 12152 KB Output is correct
37 Correct 9 ms 12244 KB Output is correct
38 Correct 10 ms 12200 KB Output is correct
39 Correct 9 ms 12244 KB Output is correct
40 Correct 9 ms 12244 KB Output is correct
41 Correct 8 ms 11988 KB Output is correct
42 Correct 828 ms 77968 KB Output is correct
43 Correct 766 ms 64332 KB Output is correct
44 Correct 775 ms 64364 KB Output is correct
45 Correct 781 ms 64072 KB Output is correct
46 Correct 806 ms 93592 KB Output is correct
47 Correct 776 ms 85860 KB Output is correct
48 Correct 790 ms 93520 KB Output is correct
49 Correct 842 ms 94512 KB Output is correct
50 Correct 853 ms 64384 KB Output is correct
51 Correct 897 ms 93684 KB Output is correct
52 Correct 861 ms 93544 KB Output is correct
53 Correct 843 ms 94360 KB Output is correct
54 Correct 917 ms 94492 KB Output is correct
55 Correct 8 ms 11972 KB Output is correct
56 Correct 849 ms 90376 KB Output is correct
57 Correct 741 ms 60864 KB Output is correct
58 Correct 814 ms 90704 KB Output is correct
59 Correct 809 ms 90768 KB Output is correct
60 Correct 726 ms 61212 KB Output is correct
61 Correct 826 ms 91088 KB Output is correct
62 Correct 847 ms 91092 KB Output is correct
63 Correct 771 ms 90916 KB Output is correct
64 Correct 879 ms 91048 KB Output is correct
65 Correct 787 ms 90480 KB Output is correct
66 Correct 829 ms 90496 KB Output is correct
67 Correct 875 ms 90556 KB Output is correct
68 Correct 818 ms 70604 KB Output is correct
69 Correct 1087 ms 91220 KB Output is correct
70 Correct 860 ms 61328 KB Output is correct
71 Correct 809 ms 60368 KB Output is correct
72 Correct 753 ms 66356 KB Output is correct
73 Correct 797 ms 90616 KB Output is correct
74 Correct 757 ms 90648 KB Output is correct
75 Correct 797 ms 90732 KB Output is correct
76 Correct 820 ms 91444 KB Output is correct
77 Correct 8 ms 11988 KB Output is correct