Submission #831056

# Submission time Handle Problem Language Result Execution time Memory
831056 2023-08-19T16:23:01 Z NothingXD Distributing Candies (IOI21_candies) C++17
100 / 100
2568 ms 17672 KB
#include "candies.h"
#include <bits/stdc++.h>

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx")

using namespace std;

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

void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
	cerr << H << ' ';
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)

const int maxn = 2e5 + 10;

int n, q, a[maxn], c[maxn], l[maxn], r[maxn], v[maxn];

void add1(int l, int r, int x){
//	debug(1, l, r, x);
	for (int i = l; i <= r; i++){
		a[i] = (a[i]+x > c[i]? c[i]: a[i]+x);
	}
}

void add2(int l, int r, int x){
//	debug(2, l, r, x);
	for (int i = l; i <= r; i++){
		a[i] = (a[i]+x < 0? 0: a[i]+x);
	}
}

void add3(int l, int r, int x, int y){
//	debug(3, l, r, x, y);
	for (int i = l; i <= r; i++){
		a[i] = (a[i]+x > c[i]? c[i]: a[i]+x);
//		debug(i, a[i]);
		a[i] = (a[i]+y < 0? 0: a[i]+y);
//		debug(i, a[i]);
	}
}

void add4(int l, int r, int x, int y){
	//debug(4, l, r, x, y);
	for (int i = l; i <= r; i++){
		a[i] = (a[i]+x < 0? 0: a[i]+x);
		a[i] = (a[i]+y > c[i]? c[i]: a[i]+y);
	}
}

vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V) {
	n = C.size();
	q = L.size();
	for (int i = 0; i < n; i++){
		c[i] = C[i];
	}
	for (int i = 0; i < q; i++){
		l[i] = L[i];
		r[i] = R[i];
		v[i] = V[i];
	}
	for (int i = 0; i < q; i += 2){
		if (i + 1 == q){
			if (v[i] > 0) add1(l[i], r[i], v[i]);
			if (v[i] < 0) add2(l[i], r[i], v[i]);
			break;
		}
		if (v[i] == 0 && v[i+1] == 0) continue;
		if (v[i] >= 0 && v[i+1] >= 0){
			add1(l[i], min(r[i], l[i+1]-1), v[i]);
			add1(l[i+1], min(r[i+1], l[i]-1), v[i+1]);
			add1(max(l[i], l[i+1]), min(r[i], r[i+1]), min(v[i]+v[i+1],(int)1e9));
			add1(max(l[i], r[i+1]+1), r[i], v[i]);
			add1(max(l[i+1], r[i]+1), r[i+1], v[i+1]);
		}
		else if (v[i] <= 0 && v[i+1] <= 0){
			add2(l[i], min(r[i], l[i+1]-1), v[i]);
			add2(l[i+1], min(r[i+1], l[i]-1), v[i+1]);
			add2(max(l[i], l[i+1]), min(r[i], r[i+1]), max(v[i]+v[i+1],(int)-1e9));
			add2(max(l[i], r[i+1]+1), r[i], v[i]);
			add2(max(l[i+1], r[i]+1), r[i+1], v[i+1]);
		}
		else if (v[i] >= 0 && v[i+1] <= 0){
			add1(l[i], min(r[i], l[i+1]-1), v[i]);
			add2(l[i+1], min(r[i+1], l[i]-1), v[i+1]);
			add3(max(l[i], l[i+1]), min(r[i], r[i+1]), v[i], v[i+1]);
			add1(max(l[i], r[i+1]+1), r[i], v[i]);
			add2(max(l[i+1], r[i]+1), r[i+1], v[i+1]);
		}
		else{
			add2(l[i], min(r[i], l[i+1]-1), v[i]);
			add1(l[i+1], min(r[i+1], l[i]-1), v[i+1]);
			add4(max(l[i], l[i+1]), min(r[i], r[i+1]), v[i], v[i+1]);
			add2(max(l[i], r[i+1]+1), r[i], v[i]);
			add1(max(l[i+1], r[i]+1), r[i+1], v[i+1]);
		}
	}
	vector<int> ans;
	for (int i = 0; i < n; i++){
		ans.push_back(a[i]);
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1600 ms 11632 KB Output is correct
2 Correct 1538 ms 11628 KB Output is correct
3 Correct 1585 ms 11692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 66 ms 7408 KB Output is correct
3 Correct 55 ms 5548 KB Output is correct
4 Correct 1514 ms 11632 KB Output is correct
5 Correct 1524 ms 11636 KB Output is correct
6 Correct 1477 ms 11628 KB Output is correct
7 Correct 1512 ms 17672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 63 ms 7400 KB Output is correct
4 Correct 60 ms 4548 KB Output is correct
5 Correct 2456 ms 11632 KB Output is correct
6 Correct 2506 ms 11636 KB Output is correct
7 Correct 2528 ms 11632 KB Output is correct
8 Correct 2454 ms 11632 KB Output is correct
9 Correct 2568 ms 11636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1600 ms 11632 KB Output is correct
7 Correct 1538 ms 11628 KB Output is correct
8 Correct 1585 ms 11692 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 66 ms 7408 KB Output is correct
11 Correct 55 ms 5548 KB Output is correct
12 Correct 1514 ms 11632 KB Output is correct
13 Correct 1524 ms 11636 KB Output is correct
14 Correct 1477 ms 11628 KB Output is correct
15 Correct 1512 ms 17672 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 63 ms 7400 KB Output is correct
19 Correct 60 ms 4548 KB Output is correct
20 Correct 2456 ms 11632 KB Output is correct
21 Correct 2506 ms 11636 KB Output is correct
22 Correct 2528 ms 11632 KB Output is correct
23 Correct 2454 ms 11632 KB Output is correct
24 Correct 2568 ms 11636 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 52 ms 5804 KB Output is correct
27 Correct 63 ms 9940 KB Output is correct
28 Correct 1504 ms 16180 KB Output is correct
29 Correct 1510 ms 16604 KB Output is correct
30 Correct 1485 ms 16868 KB Output is correct
31 Correct 1526 ms 16856 KB Output is correct
32 Correct 1500 ms 17152 KB Output is correct