Submission #102097

# Submission time Handle Problem Language Result Execution time Memory
102097 2019-03-22T09:30:43 Z gs14004 Bitaro, who Leaps through Time (JOI19_timeleap) C++17
100 / 100
2413 ms 14068 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<lint, lint>;
const int MAXN = 600005;
const int B = 500;
 
int n, q;
pi a[MAXN];
 
struct bucket{
	lint ints, inte;
	int destination;
	lint offset;
	bool found;
	void remake(int s, int e){
		s = max(s, 1); e = min(e, 2 * n - 1);
		ints = -1e9, inte = 2e9;
		offset = 0;
		found = 0;
		for(int i=s; i<=e; i++){
			if(max(ints, a[i].first) <= min(inte, a[i].second)){
				inte = min(inte, a[i].second);
				ints = max(ints, a[i].first);
			}
			else{
				found = 1; 
				if(a[i].first > inte) ints = inte;
				if(a[i].second < ints) inte = ints;
				int x = inte;
				for(int j=i; j<=e; j++){
					if(x < a[j].first) x = a[j].first;
					if(x > a[j].second){
						offset += x - a[j].second;
						x = a[j].second;
					}
				}
				destination = x;
				break;
			}
		}
	}
	pi process(int x){
		if(!found){
			if(x > inte) return pi(x - inte, inte);
			else return pi(0ll, max(x * 1ll, ints));
		}
		lint ret = offset + max(x - ints, 0ll);
		return pi(ret, 1ll * destination);
	}
}bkt[2222];
 
pi solve(int s, int e, int x){
	if(e - s <= 3*B){
		lint ans = 0;
		for(int i=s; i<=e; i++){
			if(x < a[i].first) x = a[i].first;
			if(x > a[i].second){
				ans += x - a[i].second;
				x = a[i].second;
			}
		}
		return pi(ans, x);
	}
	else{
		auto ret1 = solve(s, (s/B + 1) * B - 1, x);
		lint ans = ret1.first;
		x = ret1.second;
		for(int i=s/B+1; i<e/B; i++){
			auto nxt = bkt[i].process(x);
			ans += nxt.first;
			x = nxt.second;
		}
		auto ret2 = solve((e / B) * B, e, x);
		ans += ret2.first;
		x = ret2.second;
		return pi(ans, x);
	}
}
 
int main(){
	scanf("%d %d",&n,&q);
	for(int i=1; i<n; i++){
		scanf("%lld %lld",&a[i].first,&a[i].second);
		a[2*n-i] = a[i];
	}
	a[n] = pi(6, 9);
	for(int i=1; i<=2*n-1; i++){
		a[i].first -= i;
		a[i].second -= i + 1;
	}
	for(int i=0; i<=2*n-1; i+=B){
		bkt[i/B].remake(i, i + B - 1);
	}
	for(int i=0; i<q; i++){
		int t; scanf("%d",&t);
		if(t == 1){
			int p, s, e; scanf("%d %d %d",&p,&s,&e);
			a[p] = pi(s - p, e - p - 1);
			a[2*n-p] = pi(s - 2 * n + p, e - 2 * n + p - 1);
			{
				int key = p / B;
				bkt[key].remake(key * B, key * B + B - 1);
			}
			{
				int key = (2 * n - p) / B;
				bkt[key].remake(key * B, key * B + B - 1);
			}
		}
		else{
			int p, q, r, s; scanf("%d %d %d %d",&p,&q,&r,&s);
			lint ans = 0;
			if(p < r){
				q -= p;
				s -= r;
				auto dap = solve(p, r - 1, q);
				ans = dap.first + max(dap.second - s, 0ll);
			}
			else if(p > r){
				q -= 2 * n - p + 1;
				s -= 2 * n - r + 1;
				auto dap =  solve(2 * n - p + 1, 2 * n - r, q);
				ans = dap.first + max(dap.second - s, 0ll);
			}
			else{
				ans = max(q - s, 0);
			}
			printf("%lld\n", ans);
		}
	}
}

Compilation message

timeleap.cpp: In function 'int main()':
timeleap.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&q);
  ~~~~~^~~~~~~~~~~~~~~
timeleap.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld",&a[i].first,&a[i].second);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:96:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int t; scanf("%d",&t);
          ~~~~~^~~~~~~~~
timeleap.cpp:98:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int p, s, e; scanf("%d %d %d",&p,&s,&e);
                 ~~~~~^~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:111:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int p, q, r, s; scanf("%d %d %d %d",&p,&q,&r,&s);
                    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 3 ms 256 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 8 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 7 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 7 ms 384 KB Output is correct
25 Correct 6 ms 356 KB Output is correct
26 Correct 4 ms 384 KB Output is correct
27 Correct 6 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 6 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 6 ms 384 KB Output is correct
33 Correct 7 ms 384 KB Output is correct
34 Correct 7 ms 384 KB Output is correct
35 Correct 7 ms 384 KB Output is correct
36 Correct 7 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 6 ms 384 KB Output is correct
39 Correct 6 ms 384 KB Output is correct
40 Correct 5 ms 384 KB Output is correct
41 Correct 3 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1732 ms 13144 KB Output is correct
2 Correct 1603 ms 12556 KB Output is correct
3 Correct 1658 ms 12720 KB Output is correct
4 Correct 1930 ms 12584 KB Output is correct
5 Correct 1772 ms 13096 KB Output is correct
6 Correct 1688 ms 13060 KB Output is correct
7 Correct 1407 ms 13428 KB Output is correct
8 Correct 1463 ms 13900 KB Output is correct
9 Correct 1397 ms 12628 KB Output is correct
10 Correct 1626 ms 13296 KB Output is correct
11 Correct 1695 ms 13312 KB Output is correct
12 Correct 1821 ms 13880 KB Output is correct
13 Correct 1868 ms 14068 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 3 ms 256 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 8 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 7 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 7 ms 384 KB Output is correct
25 Correct 6 ms 356 KB Output is correct
26 Correct 4 ms 384 KB Output is correct
27 Correct 6 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 6 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 6 ms 384 KB Output is correct
33 Correct 7 ms 384 KB Output is correct
34 Correct 7 ms 384 KB Output is correct
35 Correct 7 ms 384 KB Output is correct
36 Correct 7 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 6 ms 384 KB Output is correct
39 Correct 6 ms 384 KB Output is correct
40 Correct 5 ms 384 KB Output is correct
41 Correct 3 ms 256 KB Output is correct
42 Correct 1732 ms 13144 KB Output is correct
43 Correct 1603 ms 12556 KB Output is correct
44 Correct 1658 ms 12720 KB Output is correct
45 Correct 1930 ms 12584 KB Output is correct
46 Correct 1772 ms 13096 KB Output is correct
47 Correct 1688 ms 13060 KB Output is correct
48 Correct 1407 ms 13428 KB Output is correct
49 Correct 1463 ms 13900 KB Output is correct
50 Correct 1397 ms 12628 KB Output is correct
51 Correct 1626 ms 13296 KB Output is correct
52 Correct 1695 ms 13312 KB Output is correct
53 Correct 1821 ms 13880 KB Output is correct
54 Correct 1868 ms 14068 KB Output is correct
55 Correct 2 ms 256 KB Output is correct
56 Correct 2267 ms 11096 KB Output is correct
57 Correct 2205 ms 10516 KB Output is correct
58 Correct 2261 ms 11228 KB Output is correct
59 Correct 2247 ms 11384 KB Output is correct
60 Correct 2413 ms 10760 KB Output is correct
61 Correct 2155 ms 11768 KB Output is correct
62 Correct 2075 ms 11712 KB Output is correct
63 Correct 2332 ms 11772 KB Output is correct
64 Correct 2267 ms 11748 KB Output is correct
65 Correct 2270 ms 11280 KB Output is correct
66 Correct 1788 ms 11384 KB Output is correct
67 Correct 1792 ms 11896 KB Output is correct
68 Correct 1669 ms 11096 KB Output is correct
69 Correct 1917 ms 12072 KB Output is correct
70 Correct 1788 ms 10756 KB Output is correct
71 Correct 2111 ms 10616 KB Output is correct
72 Correct 2279 ms 10868 KB Output is correct
73 Correct 2233 ms 11920 KB Output is correct
74 Correct 2330 ms 11868 KB Output is correct
75 Correct 2071 ms 11992 KB Output is correct
76 Correct 2277 ms 12112 KB Output is correct
77 Correct 3 ms 384 KB Output is correct