Submission #520574

# Submission time Handle Problem Language Result Execution time Memory
520574 2022-01-30T09:20:13 Z fatemetmhr Bitaro, who Leaps through Time (JOI19_timeleap) C++17
100 / 100
729 ms 138032 KB
//  ~Be name khoda~  //

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
#define cl       clear
#define endll    '\n'

const int maxn  =  1e6   + 10;
const int maxn5 =  3e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  2e18;

struct javab{

	ll ans, st, ft, ub, ch;
	bool rad;
	javab(){
		rad = true;
	}

} node1[maxnt], node2[maxnt];

javab operator +(javab a, javab b){
	if(a.rad)
		return b;
	if(b.rad)
		return a;
	/*
	cout << "operator having " << '\n';
	cout << a.st << ' ' << a.ft << ' ' << a.ub << ' ' << a.ch << '\n';
	cout << b.st << ' ' << b.ft << ' ' << b.ub << ' ' << b.ch << '\n';
	//*/
	javab ret;
	ret.rad = false;
	ret.st = a.st;
	ret.ans = a.ans + b.ans;
	if(a.ft <= b.st){
		ret.ft = b.ft;
		if(a.ch <= a.ub){
			ll ft = a.ft + (a.ub - a.ch + 1);
			if(ft < b.st){
				ret.ub = a.ub;
				ret.ch = ret.ub + 2;
				if(ret.ch == ret.st)
					ret.ch++;
				return ret;
			}
			ret.ub = a.ub;
			if(ft > b.ub)
				ret.ub -= ft - b.ub;
			
			ft = a.ft + (a.ch <= ret.ub ? ret.ub - a.ch + 1 : 0);
			if(ft >= b.ch)
				ret.ch = ret.ub - (ft - b.ch);
			else
				ret.ch = ret.ub + 2;
			if(ret.ch == ret.st)
				ret.ch++;
			return ret;
		}
		ret.ub = a.ub;
		ret.ch = a.ch;
		if(ret.ch == ret.st)
			ret.ch++;
		return ret;
	}
	
	if(a.ft > b.ub){
		ret.ans += a.ft - b.ub;
		ret.ft = b.ft + (b.ch <= b.ub ? b.ub - b.ch + 1 : 0);
		ret.ub = a.ch > a.ub ? a.ub : a.ch - 1;
		ret.ch = ret.ub + 2;
		return ret;
	}
	
	ret.ft = b.ft + (a.ft >= b.ch ? a.ft - b.ch + 1 : 0);
	ll ft = a.ft + (a.ub >= a.ch ? a.ub - a.ch + 1 : 0);
	if(ft > b.ub){
		a.ub -= ft - b.ub;
		ft = b.ub;
	}
	//cout << a.ub << '\n';
	ret.ub = min(a.ub, a.ch + b.ub - a.ft - 1);
	if(ret.ub < a.ch){
		ret.ch = ret.ub + 2;
	}
	else{
		ret.ch = a.ch;
		if(b.ch > a.ft + 1){
			if(b.ch > ft){
				ret.ch = ret.ub + 2;
			}
			else{
				ret.ch += b.ch - (a.ft + 1);
			}
		}
	}
	
	if(ret.ch == a.st)
		ret.ch++;
	return ret;
}

ll ls[maxn5], rs[maxn5];

inline javab recal(javab a, ll ti){
	javab b;
	b.rad = false;
	b.st = ti;
	b.ft = ti;
	b.ub = ti;
	b.ch = ti + 2;
	b.ans = 0;
	return b + a;
}

inline void build(int l, int r, int v){
	if(r - l == 1){
		node1[v].ans = node2[v].ans = 0;
		node1[v].st = node2[v].st = ls[l];
		node1[v].ft = node2[v].ft = ls[l] + 1;
		node1[v].ub = node2[v].ub = rs[l] - 1;
		node1[v].ch = node2[v].ch = ls[l] + 1;
		node1[v].rad = node2[v].rad = false;
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, v * 2);
	build(mid, r, v * 2 + 1);
	node1[v] = node1[v * 2] + node1[v * 2 + 1];
	node2[v] = node2[v * 2 + 1] + node2[v * 2];
	//cout << "well seems like " << l << ' '<< r << '\n';
	//cout << node2[v].st << ' ' << node2[v].ft << ' '<< node2[v].ub << ' ' << node2[v].ch << '\n';
	return;
}

inline void upd(int l, int r, int ind, int v){
	if(r - l == 1){
		node1[v].ans = node2[v].ans = 0;
		node1[v].st = node2[v].st = ls[l];
		node1[v].ft = node2[v].ft = ls[l] + 1;
		node1[v].ub = node2[v].ub = rs[l] - 1;
		node1[v].ch = node2[v].ch = ls[l] + 1;
		node1[v].rad = node2[v].rad = false;
		return;
	}
	int mid = (l + r) >> 1;
	if(ind < mid)
		upd(l, mid, ind, v * 2);
	else
		upd(mid, r, ind, v * 2 + 1);
	node1[v] = node1[v * 2] + node1[v * 2 + 1];
	node2[v] = node2[v * 2 + 1] + node2[v * 2];
	return;
}

inline javab get1(int l, int r, int lq, int rq, int v){
	if(rq <= l || r <= lq){
		javab a;
		return a;
	}
	if(lq <= l && r <= rq){
		//cout << "perfect result of " << l << ' ' << r << '\n';
		//cout << node1[v].st << ' ' << node1[v].ft << ' ' << node1[v].ub << ' ' << node1[v].ch << '\n';
		return node1[v];
	}
	//cout << "very well " << l << ' ' << r << '\n';
	int mid = (l + r) >> 1;
	return get1(l, mid, lq, rq, v * 2) + get1(mid, r, lq, rq, v * 2 + 1);
}	

inline javab get2(int l, int r, int lq, int rq, int v){
	if(rq <= l || r <= lq){
		javab a;
		return a;
	}
	if(lq <= l && r <= rq){
		//cout << "perfect result of " << l << ' ' << r << '\n';
		//cout << node2[v].st << ' ' << node2[v].ft << ' ' << node2[v].ub << ' ' << node2[v].ch << '\n';
		return node2[v];
	}
	//cout << "very well " << l << ' ' << r << '\n';
	int mid = (l + r) >> 1;
	return get2(mid, r, lq, rq, v * 2 + 1) + get2(l, mid, lq, rq, v * 2);
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	
	int n, q; cin >> n >> q;
	n--;
	for(int i = 0; i < n; i++)
		cin >> ls[i] >> rs[i];
	
	if(n == 0){
		for(int i = 0; i < q; i++){
			int ty; cin >> ty;
			if(ty == 1){
				int p, s, e; cin >> p >> s >> e;
				p--;
				ls[p] = s;
				rs[p] = e;
			}
			else{
				int a, b, c, d; cin >> a >> b >> c >> d;
				a--;
				c--;
				if(a == c){
					cout << (b <= d ? 0 : b - d) << '\n';
				}
			}
		}
		return 0;
	}
	
	build(0, n, 1);
	for(int i = 0; i < q; i++){
		int ty; cin >> ty;
		if(ty == 1){
			int p, s, e; cin >> p >> s >> e;
			p--;
			ls[p] = s;
			rs[p] = e;
			upd(0, n, p, 1);
		}
		else{
			ll a, b, c, d; cin >> a >> b >> c >> d;
			a--; c--;
			if(a == c){
				cout << (b > d ? b - d : 0) << '\n';
				continue;
			}
			javab re;
			if(a < c){
				re = get1(0, n, a, c, 1);
			}
			else{
				re = get2(0, n, c, a, 1);
			}
			
			//cout << re.st << ' ' << re.ft << ' ' << re.ans << ' ' << re.ub << ' ' << re.ch << '\n';
			
			re = recal(re, b);
			if(re.ft > d)
				re.ans += re.ft - d;
			cout << re.ans << '\n';
		}
	}
}

/*
4 2
2 4
4 6
2 4
1 2 4 8
2 4 4 1 3

4 1
2 4
4 8
2 4
2 4 4 1 3
*/










# Verdict Execution time Memory Grader output
1 Correct 41 ms 112964 KB Output is correct
2 Correct 39 ms 113016 KB Output is correct
3 Correct 42 ms 112952 KB Output is correct
4 Correct 40 ms 112972 KB Output is correct
5 Correct 40 ms 112984 KB Output is correct
6 Correct 44 ms 112960 KB Output is correct
7 Correct 41 ms 112968 KB Output is correct
8 Correct 41 ms 112984 KB Output is correct
9 Correct 40 ms 112972 KB Output is correct
10 Correct 40 ms 112928 KB Output is correct
11 Correct 41 ms 113032 KB Output is correct
12 Correct 41 ms 113028 KB Output is correct
13 Correct 43 ms 112968 KB Output is correct
14 Correct 41 ms 113092 KB Output is correct
15 Correct 42 ms 112976 KB Output is correct
16 Correct 41 ms 112980 KB Output is correct
17 Correct 41 ms 113044 KB Output is correct
18 Correct 41 ms 113092 KB Output is correct
19 Correct 41 ms 113012 KB Output is correct
20 Correct 41 ms 113220 KB Output is correct
21 Correct 42 ms 113100 KB Output is correct
22 Correct 41 ms 113080 KB Output is correct
23 Correct 42 ms 113028 KB Output is correct
24 Correct 41 ms 113024 KB Output is correct
25 Correct 40 ms 113092 KB Output is correct
26 Correct 40 ms 113092 KB Output is correct
27 Correct 40 ms 113040 KB Output is correct
28 Correct 40 ms 113092 KB Output is correct
29 Correct 41 ms 113068 KB Output is correct
30 Correct 42 ms 113092 KB Output is correct
31 Correct 43 ms 113088 KB Output is correct
32 Correct 42 ms 113036 KB Output is correct
33 Correct 43 ms 112960 KB Output is correct
34 Correct 41 ms 113100 KB Output is correct
35 Correct 46 ms 113052 KB Output is correct
36 Correct 47 ms 113096 KB Output is correct
37 Correct 45 ms 113020 KB Output is correct
38 Correct 42 ms 113096 KB Output is correct
39 Correct 42 ms 113024 KB Output is correct
40 Correct 39 ms 113020 KB Output is correct
41 Correct 40 ms 112916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 610 ms 121432 KB Output is correct
2 Correct 619 ms 136452 KB Output is correct
3 Correct 619 ms 136608 KB Output is correct
4 Correct 642 ms 136228 KB Output is correct
5 Correct 615 ms 137008 KB Output is correct
6 Correct 618 ms 136920 KB Output is correct
7 Correct 601 ms 137152 KB Output is correct
8 Correct 600 ms 137828 KB Output is correct
9 Correct 562 ms 136600 KB Output is correct
10 Correct 649 ms 137088 KB Output is correct
11 Correct 605 ms 137100 KB Output is correct
12 Correct 633 ms 137864 KB Output is correct
13 Correct 614 ms 138032 KB Output is correct
14 Correct 44 ms 113012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 112964 KB Output is correct
2 Correct 39 ms 113016 KB Output is correct
3 Correct 42 ms 112952 KB Output is correct
4 Correct 40 ms 112972 KB Output is correct
5 Correct 40 ms 112984 KB Output is correct
6 Correct 44 ms 112960 KB Output is correct
7 Correct 41 ms 112968 KB Output is correct
8 Correct 41 ms 112984 KB Output is correct
9 Correct 40 ms 112972 KB Output is correct
10 Correct 40 ms 112928 KB Output is correct
11 Correct 41 ms 113032 KB Output is correct
12 Correct 41 ms 113028 KB Output is correct
13 Correct 43 ms 112968 KB Output is correct
14 Correct 41 ms 113092 KB Output is correct
15 Correct 42 ms 112976 KB Output is correct
16 Correct 41 ms 112980 KB Output is correct
17 Correct 41 ms 113044 KB Output is correct
18 Correct 41 ms 113092 KB Output is correct
19 Correct 41 ms 113012 KB Output is correct
20 Correct 41 ms 113220 KB Output is correct
21 Correct 42 ms 113100 KB Output is correct
22 Correct 41 ms 113080 KB Output is correct
23 Correct 42 ms 113028 KB Output is correct
24 Correct 41 ms 113024 KB Output is correct
25 Correct 40 ms 113092 KB Output is correct
26 Correct 40 ms 113092 KB Output is correct
27 Correct 40 ms 113040 KB Output is correct
28 Correct 40 ms 113092 KB Output is correct
29 Correct 41 ms 113068 KB Output is correct
30 Correct 42 ms 113092 KB Output is correct
31 Correct 43 ms 113088 KB Output is correct
32 Correct 42 ms 113036 KB Output is correct
33 Correct 43 ms 112960 KB Output is correct
34 Correct 41 ms 113100 KB Output is correct
35 Correct 46 ms 113052 KB Output is correct
36 Correct 47 ms 113096 KB Output is correct
37 Correct 45 ms 113020 KB Output is correct
38 Correct 42 ms 113096 KB Output is correct
39 Correct 42 ms 113024 KB Output is correct
40 Correct 39 ms 113020 KB Output is correct
41 Correct 40 ms 112916 KB Output is correct
42 Correct 610 ms 121432 KB Output is correct
43 Correct 619 ms 136452 KB Output is correct
44 Correct 619 ms 136608 KB Output is correct
45 Correct 642 ms 136228 KB Output is correct
46 Correct 615 ms 137008 KB Output is correct
47 Correct 618 ms 136920 KB Output is correct
48 Correct 601 ms 137152 KB Output is correct
49 Correct 600 ms 137828 KB Output is correct
50 Correct 562 ms 136600 KB Output is correct
51 Correct 649 ms 137088 KB Output is correct
52 Correct 605 ms 137100 KB Output is correct
53 Correct 633 ms 137864 KB Output is correct
54 Correct 614 ms 138032 KB Output is correct
55 Correct 44 ms 113012 KB Output is correct
56 Correct 589 ms 133820 KB Output is correct
57 Correct 590 ms 133280 KB Output is correct
58 Correct 622 ms 134224 KB Output is correct
59 Correct 629 ms 134208 KB Output is correct
60 Correct 609 ms 133428 KB Output is correct
61 Correct 616 ms 134540 KB Output is correct
62 Correct 619 ms 134572 KB Output is correct
63 Correct 627 ms 134532 KB Output is correct
64 Correct 625 ms 134680 KB Output is correct
65 Correct 636 ms 134028 KB Output is correct
66 Correct 640 ms 134156 KB Output is correct
67 Correct 649 ms 134520 KB Output is correct
68 Correct 691 ms 133340 KB Output is correct
69 Correct 719 ms 134760 KB Output is correct
70 Correct 687 ms 133548 KB Output is correct
71 Correct 712 ms 132528 KB Output is correct
72 Correct 632 ms 133012 KB Output is correct
73 Correct 725 ms 134100 KB Output is correct
74 Correct 658 ms 134096 KB Output is correct
75 Correct 688 ms 134316 KB Output is correct
76 Correct 729 ms 134888 KB Output is correct
77 Correct 43 ms 112964 KB Output is correct