Submission #520578

# Submission time Handle Problem Language Result Execution time Memory
520578 2022-01-30T09:23:01 Z fatemetmhr Bitaro, who Leaps through Time (JOI19_timeleap) C++17
100 / 100
664 ms 122924 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;
	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];
	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) return node1[v];
	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) return node2[v];
	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);
			}
			
			re = recal(re, b);
			if(re.ft > d)
				re.ans += re.ft - d;
			cout << re.ans << '\n';
		}
	}
}










# Verdict Execution time Memory Grader output
1 Correct 42 ms 112972 KB Output is correct
2 Correct 42 ms 112972 KB Output is correct
3 Correct 43 ms 113020 KB Output is correct
4 Correct 44 ms 112972 KB Output is correct
5 Correct 43 ms 113012 KB Output is correct
6 Correct 43 ms 112932 KB Output is correct
7 Correct 43 ms 112964 KB Output is correct
8 Correct 45 ms 112992 KB Output is correct
9 Correct 46 ms 113020 KB Output is correct
10 Correct 43 ms 112960 KB Output is correct
11 Correct 45 ms 113040 KB Output is correct
12 Correct 44 ms 113044 KB Output is correct
13 Correct 44 ms 112992 KB Output is correct
14 Correct 48 ms 113072 KB Output is correct
15 Correct 44 ms 113000 KB Output is correct
16 Correct 44 ms 113052 KB Output is correct
17 Correct 45 ms 113056 KB Output is correct
18 Correct 47 ms 113044 KB Output is correct
19 Correct 46 ms 113060 KB Output is correct
20 Correct 45 ms 113104 KB Output is correct
21 Correct 47 ms 113100 KB Output is correct
22 Correct 45 ms 113068 KB Output is correct
23 Correct 45 ms 113092 KB Output is correct
24 Correct 44 ms 113056 KB Output is correct
25 Correct 48 ms 113000 KB Output is correct
26 Correct 50 ms 113064 KB Output is correct
27 Correct 45 ms 113068 KB Output is correct
28 Correct 48 ms 113040 KB Output is correct
29 Correct 44 ms 113088 KB Output is correct
30 Correct 45 ms 113084 KB Output is correct
31 Correct 44 ms 113108 KB Output is correct
32 Correct 44 ms 113092 KB Output is correct
33 Correct 44 ms 112980 KB Output is correct
34 Correct 48 ms 113092 KB Output is correct
35 Correct 48 ms 113032 KB Output is correct
36 Correct 44 ms 113092 KB Output is correct
37 Correct 45 ms 113084 KB Output is correct
38 Correct 44 ms 113092 KB Output is correct
39 Correct 45 ms 113128 KB Output is correct
40 Correct 44 ms 113028 KB Output is correct
41 Correct 43 ms 112924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 592 ms 122004 KB Output is correct
2 Correct 571 ms 122224 KB Output is correct
3 Correct 618 ms 122284 KB Output is correct
4 Correct 590 ms 122252 KB Output is correct
5 Correct 619 ms 122568 KB Output is correct
6 Correct 635 ms 122368 KB Output is correct
7 Correct 583 ms 122676 KB Output is correct
8 Correct 640 ms 122876 KB Output is correct
9 Correct 581 ms 122308 KB Output is correct
10 Correct 613 ms 122556 KB Output is correct
11 Correct 592 ms 122488 KB Output is correct
12 Correct 604 ms 122884 KB Output is correct
13 Correct 600 ms 122924 KB Output is correct
14 Correct 41 ms 112964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 112972 KB Output is correct
2 Correct 42 ms 112972 KB Output is correct
3 Correct 43 ms 113020 KB Output is correct
4 Correct 44 ms 112972 KB Output is correct
5 Correct 43 ms 113012 KB Output is correct
6 Correct 43 ms 112932 KB Output is correct
7 Correct 43 ms 112964 KB Output is correct
8 Correct 45 ms 112992 KB Output is correct
9 Correct 46 ms 113020 KB Output is correct
10 Correct 43 ms 112960 KB Output is correct
11 Correct 45 ms 113040 KB Output is correct
12 Correct 44 ms 113044 KB Output is correct
13 Correct 44 ms 112992 KB Output is correct
14 Correct 48 ms 113072 KB Output is correct
15 Correct 44 ms 113000 KB Output is correct
16 Correct 44 ms 113052 KB Output is correct
17 Correct 45 ms 113056 KB Output is correct
18 Correct 47 ms 113044 KB Output is correct
19 Correct 46 ms 113060 KB Output is correct
20 Correct 45 ms 113104 KB Output is correct
21 Correct 47 ms 113100 KB Output is correct
22 Correct 45 ms 113068 KB Output is correct
23 Correct 45 ms 113092 KB Output is correct
24 Correct 44 ms 113056 KB Output is correct
25 Correct 48 ms 113000 KB Output is correct
26 Correct 50 ms 113064 KB Output is correct
27 Correct 45 ms 113068 KB Output is correct
28 Correct 48 ms 113040 KB Output is correct
29 Correct 44 ms 113088 KB Output is correct
30 Correct 45 ms 113084 KB Output is correct
31 Correct 44 ms 113108 KB Output is correct
32 Correct 44 ms 113092 KB Output is correct
33 Correct 44 ms 112980 KB Output is correct
34 Correct 48 ms 113092 KB Output is correct
35 Correct 48 ms 113032 KB Output is correct
36 Correct 44 ms 113092 KB Output is correct
37 Correct 45 ms 113084 KB Output is correct
38 Correct 44 ms 113092 KB Output is correct
39 Correct 45 ms 113128 KB Output is correct
40 Correct 44 ms 113028 KB Output is correct
41 Correct 43 ms 112924 KB Output is correct
42 Correct 592 ms 122004 KB Output is correct
43 Correct 571 ms 122224 KB Output is correct
44 Correct 618 ms 122284 KB Output is correct
45 Correct 590 ms 122252 KB Output is correct
46 Correct 619 ms 122568 KB Output is correct
47 Correct 635 ms 122368 KB Output is correct
48 Correct 583 ms 122676 KB Output is correct
49 Correct 640 ms 122876 KB Output is correct
50 Correct 581 ms 122308 KB Output is correct
51 Correct 613 ms 122556 KB Output is correct
52 Correct 592 ms 122488 KB Output is correct
53 Correct 604 ms 122884 KB Output is correct
54 Correct 600 ms 122924 KB Output is correct
55 Correct 41 ms 112964 KB Output is correct
56 Correct 592 ms 120284 KB Output is correct
57 Correct 621 ms 120096 KB Output is correct
58 Correct 614 ms 120456 KB Output is correct
59 Correct 613 ms 120612 KB Output is correct
60 Correct 591 ms 120200 KB Output is correct
61 Correct 601 ms 120644 KB Output is correct
62 Correct 636 ms 120612 KB Output is correct
63 Correct 631 ms 120596 KB Output is correct
64 Correct 646 ms 120580 KB Output is correct
65 Correct 586 ms 120376 KB Output is correct
66 Correct 593 ms 120260 KB Output is correct
67 Correct 609 ms 120316 KB Output is correct
68 Correct 627 ms 119516 KB Output is correct
69 Correct 623 ms 120056 KB Output is correct
70 Correct 595 ms 119524 KB Output is correct
71 Correct 586 ms 119212 KB Output is correct
72 Correct 609 ms 119476 KB Output is correct
73 Correct 613 ms 119916 KB Output is correct
74 Correct 624 ms 120068 KB Output is correct
75 Correct 618 ms 120060 KB Output is correct
76 Correct 664 ms 120084 KB Output is correct
77 Correct 44 ms 112952 KB Output is correct