Submission #260493

# Submission time Handle Problem Language Result Execution time Memory
260493 2020-08-10T11:22:08 Z patrikpavic2 Bitaro, who Leaps through Time (JOI19_timeleap) C++17
34 / 100
2142 ms 314612 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

#define X first
#define Y second

using namespace std;

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

const int OFF = (1 << 19);
const int INF = 0x3f3f3f3f;
const int N = 3e5 + 500;
const int LOG = 20;

pii T[2 * OFF][2];
int n, q;

inline pii mrg(pii A, pii B){
	return {max(A.X, B.X), min(A.Y, B.Y)};
}

void update(int i, int j, pii nw){
	T[i + OFF][j] = nw;
	for(i = (i + OFF) / 2 ; i ; i /= 2)
		T[i][j] = mrg(T[2 * i][j], T[2 * i + 1][j]);
}

int q_gr, q_x, q_j;

int desnoo(int i, int a, int b){
	//printf("i = %d a = %d b = %d q_gr = %d q_x = %d q_j = %d\n", i, a, b, q_gr, q_x, q_j);
	if(b < q_gr || (T[i][q_j].X <= q_x && q_x <= T[i][q_j].Y))
		return -1;
	if(a == b) return a;
	int L = desnoo(2 * i, a, (a + b) / 2);
	if(L != -1)
		return L;
	return desnoo(2 * i + 1, (a + b) / 2 + 1, b);
}

inline int desno(int gdje, int kol, int j){
	q_gr = gdje; q_x = kol; q_j = j;
	return desnoo(1, 0, OFF - 1);
}

int par[2 * N][2][LOG], LL[N], RR[N], L[N][2], R[N][2];
ll cst[2 * N][2][LOG];
int vis[2 * N][2];

inline int get_pos(int x){
	if(x == 2 * n) return n;
	return x % n;
}

int main(){
	for(int i = 0;i < 2 * OFF;i++)
		T[i][0] = {-INF, INF}, T[i][1] = {-INF, INF};
	scanf("%d%d", &n, &q); n--;
	for(int i = 0;i < n;i++){
		scanf("%d%d", LL + i, RR + i); RR[i]--;
		L[i][0] = LL[i] - i, R[i][0] = RR[i] - i;
		L[n - i - 1][1] = LL[i] - (n - i - 1);
		R[n - i - 1][1] = RR[i] - (n - i - 1);
	}
	if(n <= 1000 && q <= 1000){
		for(;q--;){
			int ty; scanf("%d", &ty);
			if(ty == 1){
				int i, a, b; scanf("%d%d%d", &i, &a, &b); i--; b--;
				L[i][0] = a - i, R[i][0] = b - i;
				L[n - i - 1][1] = a - (n - i - 1);
				R[n - i - 1][1] = b - (n - i - 1);
				continue;
			}
			int st, en, v_st, v_en;
			scanf("%d%d%d%d", &st, &v_st, &en, &v_en); st--; en--;
			int k = (st > en);
			if(k){
				st = n - st; en = n - en;
			}
			v_st -= st; v_en -= en;
			ll ans = 0;
			int sad = v_st, cur = st;
			for(;cur < en;cur++){
				if(L[cur][k] > sad)
					sad = L[cur][k];
				if(R[cur][k] < sad){
					ans += (ll)(sad - R[cur][k]);
					sad = R[cur][k];
				}
			}
			if(sad > v_en)
				ans += sad - v_en;
			printf("%lld\n", ans);
		}
		return 0;
	}
	for(int k = 0;k < 2;k++){
		for(int i = 0;i < n;i++){
			update(i, k, {L[i][k], R[i][k]});
			vis[i][k] = L[i][k]; vis[i + n][k] = R[i][k];
		}
		update(n, k, {INF, -INF});
		for(int i = 0;i < 2 * n;i++){
			par[i][k][0] = desno(i % n, vis[i][k], k);
			if(par[i][k][0] == n){
				par[i][k][0] += n;
			}
			else if(vis[i][k] > R[par[i][k][0]][k]){
				cst[i][k][0] = vis[i][k] - R[par[i][k][0] % n][k];
				par[i][k][0] += n;
			}
			//printf("%d --> %d\n", i, par[i][k][0]);
		}	
		//printf("tu sam\n");
		par[2 * n][k][0] = 2 * n;
		for(int j = 1;j < LOG;j++){
			par[2 * n][k][j] = 2 * n;
			for(int i = 0;i < 2 * n;i++){
				par[i][k][j] = par[par[i][k][j - 1]][k][j - 1];
				cst[i][k][j] = cst[i][k][j - 1] + cst[par[i][k][j - 1]][k][j - 1];
			}
		}
		//printf("gotov precompute\n");
	}
	for(;q--;){
		int ty; scanf("%d", &ty);
		if(ty == 1) return 0;
		int st, en, v_st, v_en;
		scanf("%d%d%d%d", &st, &v_st, &en, &v_en); st--; en--;
		int k = (st > en);
		if(k){
			st = n - st; en = n - en;
		}
		v_st -= st; v_en -= en;
		//printf("st time = %d en time = %d\n", v_st, v_en);
		int cur = desno(st, v_st, k);
		//printf("vis %d\n", R[cur][k]);
		//printf("cur = %d\n", cur);
		if(cur >= en){
			printf("%lld\n", max((ll)v_st - v_en, 0LL));
			continue;
		}
		ll ans = 0;
		if(v_st > R[cur][k]){
			ans += v_st - R[cur][k];
			cur += n;
		}
		for(int i = LOG - 1;i >= 0;i--){
			if(get_pos(par[cur][k][i]) < en){	
				ans += cst[cur][k][i];
				cur = par[cur][k][i];
			}
		}
		//printf("cur = %d\n", cur);
		//printf("ans: %lld\n", ans);
		//printf("vis : %d krj : %d\n", vis[cur][k], v_en);
		ans += max((ll)vis[cur][k] - v_en, 0LL);
		printf("%lld\n", ans);
	}
}




























Compilation message

timeleap.cpp: In function 'int main()':
timeleap.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &q); n--;
  ~~~~~^~~~~~~~~~~~~~~~
timeleap.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", LL + i, RR + i); RR[i]--;
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:71:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int ty; scanf("%d", &ty);
            ~~~~~^~~~~~~~~~~
timeleap.cpp:73:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     int i, a, b; scanf("%d%d%d", &i, &a, &b); i--; b--;
                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:80:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d%d%d", &st, &v_st, &en, &v_en); st--; en--;
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:131:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int ty; scanf("%d", &ty);
           ~~~~~^~~~~~~~~~~
timeleap.cpp:134:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &st, &v_st, &en, &v_en); st--; en--;
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16768 KB Output is correct
2 Correct 10 ms 16768 KB Output is correct
3 Correct 10 ms 16768 KB Output is correct
4 Correct 10 ms 16768 KB Output is correct
5 Correct 10 ms 16768 KB Output is correct
6 Correct 12 ms 16768 KB Output is correct
7 Correct 10 ms 16768 KB Output is correct
8 Correct 12 ms 16768 KB Output is correct
9 Correct 10 ms 16768 KB Output is correct
10 Correct 10 ms 16768 KB Output is correct
11 Correct 12 ms 16896 KB Output is correct
12 Correct 12 ms 16896 KB Output is correct
13 Correct 11 ms 16896 KB Output is correct
14 Correct 12 ms 16896 KB Output is correct
15 Correct 12 ms 16896 KB Output is correct
16 Correct 12 ms 16888 KB Output is correct
17 Correct 11 ms 16896 KB Output is correct
18 Correct 12 ms 16896 KB Output is correct
19 Correct 11 ms 16896 KB Output is correct
20 Correct 11 ms 16896 KB Output is correct
21 Correct 11 ms 16896 KB Output is correct
22 Correct 11 ms 16896 KB Output is correct
23 Correct 11 ms 16896 KB Output is correct
24 Correct 11 ms 16896 KB Output is correct
25 Correct 11 ms 16896 KB Output is correct
26 Correct 11 ms 16896 KB Output is correct
27 Correct 11 ms 16896 KB Output is correct
28 Correct 15 ms 16896 KB Output is correct
29 Correct 12 ms 16896 KB Output is correct
30 Correct 12 ms 16896 KB Output is correct
31 Correct 12 ms 16896 KB Output is correct
32 Correct 12 ms 16896 KB Output is correct
33 Correct 12 ms 16896 KB Output is correct
34 Correct 13 ms 16768 KB Output is correct
35 Correct 12 ms 16896 KB Output is correct
36 Correct 11 ms 16896 KB Output is correct
37 Correct 12 ms 16896 KB Output is correct
38 Correct 11 ms 16896 KB Output is correct
39 Correct 12 ms 16896 KB Output is correct
40 Correct 12 ms 16896 KB Output is correct
41 Correct 10 ms 16768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1915 ms 289428 KB Output is correct
2 Correct 1826 ms 273460 KB Output is correct
3 Correct 1854 ms 276856 KB Output is correct
4 Correct 1793 ms 268232 KB Output is correct
5 Correct 1888 ms 288676 KB Output is correct
6 Correct 1923 ms 283840 KB Output is correct
7 Correct 1970 ms 292944 KB Output is correct
8 Correct 2001 ms 304788 KB Output is correct
9 Correct 1856 ms 270256 KB Output is correct
10 Correct 2142 ms 291716 KB Output is correct
11 Correct 1979 ms 289332 KB Output is correct
12 Correct 1974 ms 309752 KB Output is correct
13 Correct 2069 ms 314612 KB Output is correct
14 Correct 10 ms 16768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16768 KB Output is correct
2 Correct 10 ms 16768 KB Output is correct
3 Correct 10 ms 16768 KB Output is correct
4 Correct 10 ms 16768 KB Output is correct
5 Correct 10 ms 16768 KB Output is correct
6 Correct 12 ms 16768 KB Output is correct
7 Correct 10 ms 16768 KB Output is correct
8 Correct 12 ms 16768 KB Output is correct
9 Correct 10 ms 16768 KB Output is correct
10 Correct 10 ms 16768 KB Output is correct
11 Correct 12 ms 16896 KB Output is correct
12 Correct 12 ms 16896 KB Output is correct
13 Correct 11 ms 16896 KB Output is correct
14 Correct 12 ms 16896 KB Output is correct
15 Correct 12 ms 16896 KB Output is correct
16 Correct 12 ms 16888 KB Output is correct
17 Correct 11 ms 16896 KB Output is correct
18 Correct 12 ms 16896 KB Output is correct
19 Correct 11 ms 16896 KB Output is correct
20 Correct 11 ms 16896 KB Output is correct
21 Correct 11 ms 16896 KB Output is correct
22 Correct 11 ms 16896 KB Output is correct
23 Correct 11 ms 16896 KB Output is correct
24 Correct 11 ms 16896 KB Output is correct
25 Correct 11 ms 16896 KB Output is correct
26 Correct 11 ms 16896 KB Output is correct
27 Correct 11 ms 16896 KB Output is correct
28 Correct 15 ms 16896 KB Output is correct
29 Correct 12 ms 16896 KB Output is correct
30 Correct 12 ms 16896 KB Output is correct
31 Correct 12 ms 16896 KB Output is correct
32 Correct 12 ms 16896 KB Output is correct
33 Correct 12 ms 16896 KB Output is correct
34 Correct 13 ms 16768 KB Output is correct
35 Correct 12 ms 16896 KB Output is correct
36 Correct 11 ms 16896 KB Output is correct
37 Correct 12 ms 16896 KB Output is correct
38 Correct 11 ms 16896 KB Output is correct
39 Correct 12 ms 16896 KB Output is correct
40 Correct 12 ms 16896 KB Output is correct
41 Correct 10 ms 16768 KB Output is correct
42 Correct 1915 ms 289428 KB Output is correct
43 Correct 1826 ms 273460 KB Output is correct
44 Correct 1854 ms 276856 KB Output is correct
45 Correct 1793 ms 268232 KB Output is correct
46 Correct 1888 ms 288676 KB Output is correct
47 Correct 1923 ms 283840 KB Output is correct
48 Correct 1970 ms 292944 KB Output is correct
49 Correct 2001 ms 304788 KB Output is correct
50 Correct 1856 ms 270256 KB Output is correct
51 Correct 2142 ms 291716 KB Output is correct
52 Correct 1979 ms 289332 KB Output is correct
53 Correct 1974 ms 309752 KB Output is correct
54 Correct 2069 ms 314612 KB Output is correct
55 Correct 10 ms 16768 KB Output is correct
56 Incorrect 1035 ms 287156 KB Output isn't correct
57 Halted 0 ms 0 KB -