Submission #979349

# Submission time Handle Problem Language Result Execution time Memory
979349 2024-05-10T16:58:12 Z Isam Distributing Candies (IOI21_candies) C++17
3 / 100
5000 ms 31156 KB
//#include "candies.h"

#include<bits/stdc++.h>
using namespace std;

#define all(x) x.begin(),x.end()

constexpr int sz = 2e5 + 5;

constexpr long long inf = (long long)1E18 + 7;

int n, Q;

vector<int> a;

struct Node{
	long long sm, mn, mx;
} st[sz << 2];

long long lazy[sz<<2], lazy1[sz<<2];

Node operator+(Node i, Node j){
	return {i.sm + j.sm, min(i.mn, j.mn), max(i.mx, j.mx)};
}


inline void build(int l, int r, int in){
	if(l == r){
		st[in] = {0ll, inf, -inf};
		return;
	}
	int mid = l + ((r - l) >> 1);
	build(l, mid, in << 1);
	build(mid + 1, r, in << 1 | 1);
	st[in] = st[in << 1] + st[in << 1 | 1];
}


inline void relax(int l, int r, int in){
	if(l ^ r){
		lazy[in<<1] += lazy[in], lazy[in<<1|1] += lazy[in];
	}
	st[in].sm += (r - l + 1) * lazy[in];
	st[in].mn += (r - l + 1) * lazy[in];
	st[in].mx += (r - l + 1) * lazy[in];
	lazy[in] = 0;
	return;
}

inline void relax1(int l, int r, int in){
	if(l ^ r){
		lazy1[in<<1] = lazy1[in<<1|1] = lazy1[in];
	}
	st[in].sm = (r - l + 1) * lazy1[in];
	st[in].mn = (r - l + 1) * lazy1[in];
	st[in].mx = (r - l + 1) * lazy1[in];
	lazy1[in] = -1;
	return;
}


inline void update(int l, int r, int in, int L, int R, int val){
	if(lazy[in]) relax(l, r, in);
	if(l > R || r < L || r < l) return;
	if(l >= L && r <= R){
		lazy[in] = val;
		relax(l, r, in);
		return;
	}else{
		int mid = l + ((r - l) >> 1);
		update(l, mid, in << 1, L, R, val);
		update(mid + 1, r, in << 1 | 1, L, R, val);
		st[in] = st[in << 1] + st[in << 1 | 1];	
	}
	return;
}

inline void update1(int l, int r, int in, int L, int R, int val){
	if(lazy[in]) relax(l, r, in);
	if(lazy1[in] != -1) relax1(l, r, in);
	if(l > R || r < L || r < l) return;
	if(l >= L && r <= R){
		lazy1[in] = val;
		relax1(l, r, in);
		return;
	}else{
		int mid = l + ((r - l) >> 1);
		update1(l, mid, in << 1, L, R, val);
		update1(mid + 1, r, in << 1 | 1, L, R, val);
		st[in] = st[in << 1] + st[in << 1 | 1];	
	}
	return;
}


Node zro = {0ll, inf, -inf};

Node get_ans(int l, int r, int in, int L, int R){
	if(lazy[in]) relax(l, r, in);
	if(lazy1[in] != -1) relax1(l, r, in);
	if(l > R || r < L || r < l) return zro;
	if(l >= L && r <= R) return st[in];
	int mid = l + ((r - l) >> 1);
	return get_ans(l, mid, in << 1, L, R) + get_ans(mid + 1, r, in << 1 | 1, L, R);
} 


vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v){
	
	n = (int)c.size(), Q = (int)r.size();
	
	a.assign(n, 0);
	
	int mn = *min_element(all(v));
	
	if(n <= 2000 && Q <= 2000){
		
		for(register int q = 0; q < Q; ++q){
		
			for(register int i = l[q]; i <= r[q]; ++i){
			
				if(v[q] > 0) a[i] = min(c[i], a[i] + v[q]);
				else a[i] = max(0, a[i] + v[q]);
				
			}
		
		}
		
		return a;
	}
	
	if(mn >= 0){
	
		for(register int q = 0; q < Q; ++q) update(1, n, 1, l[q] + 1, r[q] + 1, v[q]);
		
		for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
	
		return a;
	
	}
	
	if(*max_element(all(c)) == *min_element(all(c))){
		
		memset(lazy1, -1, sizeof(lazy1));
		
		build(1, n, 1);
		
		for(register int q = 0; q < Q; ++q){
			
			int x = l[q] + 1, y = r[q] + 1, val = v[q];
			
			Node cur = get_ans(1, n, 1, x, y);
			
			if(cur.mx <= abs(val) && val < 0){
				update1(1, n, 1, x, y, 0ll);
				continue;
			}
			
			if(val > 0 && cur.mn + val > c[0]){
				
				update1(1, n, 1, x, y, c[0]);
				
				continue;
			}
			
			
			update(1, n, 1, x, y, val);
			
			cur = get_ans(1, n, 1, x, y);
			
			while(cur.mx > c[0]){
				
				int L = x, R = y, mid, best;
				
				while(L <= R){
					mid = L + ((R - L) >> 1);
					if(get_ans(1, n, 1, x, mid).mx > c[0]){
						best = mid, R = mid - 1;
					}else{
						L = mid + 1;
					}
				}
				
				int ll = best;
				
				int LL = ll, RR = y;
				
				while(LL <= RR){
					mid = ((LL + RR) >> 1);
					
					if(get_ans(1, n, 1, ll, mid).mn > c[0]){
						LL = mid + 1, best = mid;
					}else{
						RR = mid - 1;
					}
					
				}
				
				int rr = best;
				
				update1(1, n, 1, ll, rr, c[0]);
				
				cur = get_ans(1, n, 1, x, y);
				
				
				
			}
			
			while(cur.mn < 0ll){
				
				int L = x, R = y, mid, best;
				
				while(L <= R){
					mid = L + ((R - L) >> 1);
					if(get_ans(1, n, 1, x, mid).mn < 0ll){
						best = mid, R = mid - 1;
					}else{
						L = mid + 1;
					}
				}
				
				int ll = best;
				
				int LL = ll, RR = y;
				
				while(LL <= RR){
					mid = ((LL + RR) >> 1);
					
					if(get_ans(1, n, 1, ll, mid).mx < 0ll){
						LL = mid + 1, best = mid;
					}else{
						RR = mid - 1;
					}
					
				}
				
				int rr = best;
				
				update1(1, n, 1, ll, rr, 0ll);
				
				cur = get_ans(1, n, 1, x, y);
				
				
				
			}
			
		}
		
		for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
		
		
		return a;
		
	}
	
	
	
	
	
	
	
	assert(0);
	
	
	
}

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:118:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  118 |   for(register int q = 0; q < Q; ++q){
      |                    ^
candies.cpp:120:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  120 |    for(register int i = l[q]; i <= r[q]; ++i){
      |                     ^
candies.cpp:134:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  134 |   for(register int q = 0; q < Q; ++q) update(1, n, 1, l[q] + 1, r[q] + 1, v[q]);
      |                    ^
candies.cpp:136:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  136 |   for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
      |                    ^
candies.cpp:148:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  148 |   for(register int q = 0; q < Q; ++q){
      |                    ^
candies.cpp:249:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  249 |   for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
      |                    ^
candies.cpp:229:33: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
  229 |      if(get_ans(1, n, 1, ll, mid).mx < 0ll){
      |                                 ^
candies.cpp:191:33: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
  191 |      if(get_ans(1, n, 1, ll, mid).mn > c[0]){
      |                                 ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 260 ms 31156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Execution timed out 5048 ms 13308 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 51 ms 9960 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Incorrect 260 ms 31156 KB Output isn't correct
7 Halted 0 ms 0 KB -