Submission #578499

# Submission time Handle Problem Language Result Execution time Memory
578499 2022-06-17T04:58:48 Z alireza_kaviani Bitaro, who Leaps through Time (JOI19_timeleap) C++17
100 / 100
2710 ms 128512 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define all(x)      (x).begin(),(x).end()
#define X           first
#define Y           second
#define sep         ' '
#define endl        '\n'
#define SZ(x)       ll(x.size())
#define lc          id << 1
#define rc          lc | 1

const ll MAXN = 3e5 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;

ll n , q , L[2][MAXN] , R[2][MAXN] , mn[2][MAXN << 2] , mx[2][MAXN << 2];
pair<pll , pll> seg[2][MAXN << 2];
pll ans;

int Find(int ind , int x , int id , int l , int r){
	if(mx[ind][id] <= x && x <= mn[ind][id])	return -1;
	if(r - l == 1)	return (x < mx[ind][id]);
	int mid = l + r >> 1;
	int res = Find(ind , x , lc , l , mid);
	if(res != -1)	return res;
	return Find(ind , x , rc , mid , r);
}

pll mergeOne(pll x , int ind , int id , int l , int r){
	int res = Find(ind , x.Y , id , l , r);
	if(res == -1)	return x;
	if(res == 0){
		return {x.X + x.Y + seg[ind][id].Y.X - R[ind][l] , seg[ind][id].Y.Y};
	}
	return {x.X + seg[ind][id].X.X , seg[ind][id].X.Y};
}

pair<pll , pll> merge(pair<pll , pll> x , int ind , int id , int l , int r){
	return {mergeOne(x.X , ind , id , l , r) , mergeOne(x.Y , ind , id , l , r)};
}

void build(int ind , int id = 1 , int l = 1 , int r = n){
	if(r - l == 1){
		mn[ind][id] = R[ind][l];
		mx[ind][id] = L[ind][l];
		seg[ind][id] = {{0, L[ind][l]}, {0, R[ind][l]}};
		return;
	}
	int mid = l + r >> 1;
	build(ind , lc , l , mid);
	build(ind , rc , mid , r);
	mn[ind][id] = min(mn[ind][lc] , mn[ind][rc]);
	mx[ind][id] = max(mx[ind][lc] , mx[ind][rc]);
	seg[ind][id] = merge(seg[ind][lc] , ind , rc , mid , r);
}

void modify(int ind , int pos , int id = 1 , int l = 1 , int r = n){
	if(r - l == 1){
		mn[ind][id] = R[ind][l];
		mx[ind][id] = L[ind][l];
		seg[ind][id] = {{0, L[ind][l]}, {0, R[ind][l]}};
		return;
	}
	int mid = l + r >> 1;
	if(pos < mid)	modify(ind , pos , lc , l , mid);
	else	modify(ind , pos , rc , mid , r);
	mn[ind][id] = min(mn[ind][lc] , mn[ind][rc]);
	mx[ind][id] = max(mx[ind][lc] , mx[ind][rc]);
	seg[ind][id] = merge(seg[ind][lc] , ind , rc , mid , r);
}

void get(int ind , int ql , int qr , int id = 1 , int l = 1 , int r = n){
	if(qr <= l || r <= ql)	return;
	if(ql <= l && r <= qr){
		ans = mergeOne(ans , ind , id , l , r);
		return;
	}
	int mid = l + r >> 1;
	get(ind , ql , qr , lc , l , mid);
	get(ind , ql , qr , rc , mid , r);
}

void update(int ind , int i , int l , int r){
	L[ind][i] = l - (i - 1);
	R[ind][i] = r - (i - 1);
	modify(ind , i);
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

	cin >> n >> q;
	for(int i = 1 ; i < n ; i++){
		cin >> L[0][i] >> R[0][i];
		R[0][i]--;
		L[1][n - i] = L[0][i];
		R[1][n - i] = R[0][i];
	}
	for(int i = 1 ; i < n ; i++){
		L[0][i] -= i - 1; R[0][i] -= i - 1;
		L[1][i] -= i - 1; R[1][i] -= i - 1;
	}
	if(n != 1){
		build(0);
		build(1);
	}
	while(q--){
		int type , pos , l , r , x , y;
		cin >> type;
		if(type == 1){
			cin >> pos >> l >> r; r--;
			if(n == 1)	continue;
			update(0 , pos , l , r);
			update(1 , n - pos , l , r);
		}
		else{
			cin >> l >> x >> r >> y;
			if(n == 1){
				cout << max(0 , x - y) << endl;
				continue;
			}
			if(l <= r){
				x -= (l - 1); y -= (r - 1);
				ans = {0 , x};
				get(0 , l , r);
//				cout << ans.X << sep << ans.Y << sep << x << sep << y << endl;
				cout << ans.X + max(0ll , ans.Y - y) << endl;
			}
			else{
				l = n + 1 - l; r = n + 1 - r;
				x -= (l - 1); y -= (r - 1);
				ans = {0 , x};
				get(1 , l , r);
//				cout << ans.X << sep << ans.Y << sep << x << sep << y << endl;
				cout << ans.X + max(0ll , ans.Y - y) << endl;
			}
		}
	}

    return 0;
}
/*

*/

Compilation message

timeleap.cpp: In function 'int Find(int, int, int, int, int)':
timeleap.cpp:29:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |  int mid = l + r >> 1;
      |            ~~^~~
timeleap.cpp: In function 'void build(int, int, int, int)':
timeleap.cpp:55:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |  int mid = l + r >> 1;
      |            ~~^~~
timeleap.cpp: In function 'void modify(int, int, int, int, int)':
timeleap.cpp:70:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |  int mid = l + r >> 1;
      |            ~~^~~
timeleap.cpp: In function 'void get(int, int, int, int, int, int)':
timeleap.cpp:84:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |  int mid = l + r >> 1;
      |            ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 3 ms 596 KB Output is correct
16 Correct 3 ms 676 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 3 ms 536 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 4 ms 672 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 592 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 3 ms 596 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 3 ms 596 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 4 ms 596 KB Output is correct
39 Correct 2 ms 596 KB Output is correct
40 Correct 3 ms 592 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1207 ms 127244 KB Output is correct
2 Correct 1112 ms 77168 KB Output is correct
3 Correct 1139 ms 77356 KB Output is correct
4 Correct 1126 ms 76852 KB Output is correct
5 Correct 1280 ms 127208 KB Output is correct
6 Correct 1120 ms 127076 KB Output is correct
7 Correct 1068 ms 127296 KB Output is correct
8 Correct 1108 ms 128416 KB Output is correct
9 Correct 968 ms 77132 KB Output is correct
10 Correct 1197 ms 127316 KB Output is correct
11 Correct 1253 ms 127220 KB Output is correct
12 Correct 1177 ms 128228 KB Output is correct
13 Correct 1335 ms 128512 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 3 ms 596 KB Output is correct
16 Correct 3 ms 676 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 3 ms 536 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 4 ms 672 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 2 ms 596 KB Output is correct
27 Correct 2 ms 592 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 3 ms 596 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 3 ms 596 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 4 ms 596 KB Output is correct
39 Correct 2 ms 596 KB Output is correct
40 Correct 3 ms 592 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1207 ms 127244 KB Output is correct
43 Correct 1112 ms 77168 KB Output is correct
44 Correct 1139 ms 77356 KB Output is correct
45 Correct 1126 ms 76852 KB Output is correct
46 Correct 1280 ms 127208 KB Output is correct
47 Correct 1120 ms 127076 KB Output is correct
48 Correct 1068 ms 127296 KB Output is correct
49 Correct 1108 ms 128416 KB Output is correct
50 Correct 968 ms 77132 KB Output is correct
51 Correct 1197 ms 127316 KB Output is correct
52 Correct 1253 ms 127220 KB Output is correct
53 Correct 1177 ms 128228 KB Output is correct
54 Correct 1335 ms 128512 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 2106 ms 124008 KB Output is correct
57 Correct 1847 ms 73684 KB Output is correct
58 Correct 2131 ms 124384 KB Output is correct
59 Correct 2389 ms 124504 KB Output is correct
60 Correct 2172 ms 74080 KB Output is correct
61 Correct 2255 ms 125088 KB Output is correct
62 Correct 2280 ms 125120 KB Output is correct
63 Correct 2330 ms 124940 KB Output is correct
64 Correct 2316 ms 125104 KB Output is correct
65 Correct 2710 ms 124196 KB Output is correct
66 Correct 2642 ms 124208 KB Output is correct
67 Correct 2663 ms 124824 KB Output is correct
68 Correct 2286 ms 109448 KB Output is correct
69 Correct 2217 ms 125260 KB Output is correct
70 Correct 1927 ms 74240 KB Output is correct
71 Correct 2213 ms 73168 KB Output is correct
72 Correct 2031 ms 101216 KB Output is correct
73 Correct 2068 ms 124588 KB Output is correct
74 Correct 2247 ms 124680 KB Output is correct
75 Correct 2230 ms 124912 KB Output is correct
76 Correct 2134 ms 125436 KB Output is correct
77 Correct 1 ms 328 KB Output is correct