답안 #447172

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447172 2021-07-24T21:21:28 Z LucaDantas 사탕 분배 (IOI21_candies) C++17
100 / 100
4582 ms 108172 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;

#define int long long

constexpr int maxn = 2e5+10;
constexpr long long inf = 0x3f3f3f3f3f3f3f3f;

int n;

struct SegmentTree {
	long long tree[maxn<<2][2], lazy[maxn<<2];
	void flush(int node, int i, int j) {
		if(!lazy[node]) return;
		if(i != j) lazy[node<<1] += lazy[node], lazy[node<<1|1] += lazy[node];
		tree[node][0] += lazy[node]; tree[node][1] += lazy[node]; lazy[node] = 0;
	}
	void upd(int node, int i, int j, int l, int v) {
		flush(node, i, j);
		if(j < l) return;
		if(i >= l) return (void)(lazy[node] += v, flush(node, i, j));
		int m = (i+j) >> 1;
		upd(node<<1, i, m, l, v); upd(node<<1|1, m+1, j, l, v);
		tree[node][0] = min(tree[node<<1][0], tree[node<<1|1][0]);
		tree[node][1] = max(tree[node<<1][1], tree[node<<1|1][1]);
	}
	void upd(int l, int v) { upd(1, 0, n-1, l, v); }
	long long mn(int node, int i, int j, int l) {
		flush(node, i, j);
		if(j < l) return inf;
		if(i >= l) return tree[node][0];
		int m = (i+j) >> 1;
		return min(mn(node<<1, i, m, l), mn(node<<1|1, m+1, j, l));
	}
	long long mn(int l) { return mn(1, 0, n-1, l); }
	long long mx(int node, int i, int j, int l) {
		flush(node, i, j);
		if(j < l) return -inf;
		if(i >= l) return tree[node][1];
		int m = (i+j) >> 1;
		return max(mx(node<<1, i, m, l), mx(node<<1|1, m+1, j, l));
	}
	long long mx(int l) { return mx(1, 0, n-1, l); }
	int last(int node, int i, int j, long long val) {
		flush(node, i, j);
		if(tree[node][1] < val) return -1;
		if(i == j) return i;
		int m = (i+j) >> 1;
		int p1 = last(node<<1|1, m+1, j, val);
		if(p1 != -1) return p1;
		return last(node<<1, i, m, val);
	}
	int last(long long val) { return last(1, 0, n-1, val); }
	long long ultimo(int node = 1, int i = 0) {
		flush(node, i, n-1);
		if(i == n-1) return tree[node][1];
		int m = (i+n-1) >> 1;
		return ultimo(node<<1|1, m+1);
	}
	void deb(int node, int i, int j) {
		flush(node, i, j);
		if(i == j) return (void)(fprintf(stderr, "%lld ", tree[node][0]));
		int m = (i+j) >> 1;
		deb(node<<1, i, m); deb(node<<1|1, m+1, j);
	}
} seg;

int get(int c) {
	int l = 0, r = n-1;
	long long ans = 0;
	while(l <= r) {
		int m = (l+r) >> 1;
		long long mx = seg.mx(m);
		if(mx <= c) { r = m-1; continue; }
		int pos = seg.last(mx);
		long long mn = seg.mn(pos);
		if(mx - c >= mn) ans = max(ans, mn), l = m+1;
		else ans = max(ans, mx-c), r = m-1;
	}
	return (int)(seg.ultimo() - ans);
}

vector<int32_t> ans, C;

struct Seg2 {
	vector<pair<int,int>> tree[maxn<<2];
	void upd(int node, int i, int j, int l, int r, int t, int v) {
		if(i > r || j < l) return;
		if(i >= l && j <= r) return (void)(tree[node].push_back({t, v}));
		int m = (i+j) >> 1;
		upd(node<<1, i, m, l, r, t, v); upd(node<<1|1, m+1, j, l, r, t, v);
	}
	void go(int node, int i, int j) {
		for(auto [t, v] : tree[node])
			seg.upd(t, v);
		long long mn = seg.mn(0);
		if(mn < 0 && i == j) seg.upd(0, -mn);

		int m = (i+j) >> 1;
		if(i == j) ans[i] = get(C[i]);
		else go(node<<1, i, m), go(node<<1|1, m+1, j);
		
		if(mn < 0 && i == j) seg.upd(0, mn);
		for(auto [t, v] : tree[node])
			seg.upd(t, -v);
	}
} seg2;

vector<int32_t> distribute_candies(vector<int32_t> c, vector<int32_t> l, vector<int32_t> r, vector<int32_t> v) {
	n = (int)(v.size()); ans.resize(c.size());
	
	C = c;

	int m = (int)(c.size());
	for(int i = 0; i < n; i++)
		seg2.upd(1, 0, m-1, l[i], r[i], i, v[i]);

	seg2.go(1, 0, m-1);

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19100 KB Output is correct
2 Correct 11 ms 19088 KB Output is correct
3 Correct 13 ms 19288 KB Output is correct
4 Correct 15 ms 19476 KB Output is correct
5 Correct 25 ms 19704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4199 ms 106488 KB Output is correct
2 Correct 4070 ms 106624 KB Output is correct
3 Correct 4081 ms 106696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19020 KB Output is correct
2 Correct 1509 ms 73372 KB Output is correct
3 Correct 434 ms 26496 KB Output is correct
4 Correct 4199 ms 107852 KB Output is correct
5 Correct 4239 ms 108040 KB Output is correct
6 Correct 4388 ms 107884 KB Output is correct
7 Correct 4168 ms 107968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19020 KB Output is correct
2 Correct 11 ms 19020 KB Output is correct
3 Correct 205 ms 39340 KB Output is correct
4 Correct 417 ms 23320 KB Output is correct
5 Correct 1199 ms 43160 KB Output is correct
6 Correct 1315 ms 43164 KB Output is correct
7 Correct 1495 ms 43168 KB Output is correct
8 Correct 1156 ms 43164 KB Output is correct
9 Correct 1326 ms 43244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19100 KB Output is correct
2 Correct 11 ms 19088 KB Output is correct
3 Correct 13 ms 19288 KB Output is correct
4 Correct 15 ms 19476 KB Output is correct
5 Correct 25 ms 19704 KB Output is correct
6 Correct 4199 ms 106488 KB Output is correct
7 Correct 4070 ms 106624 KB Output is correct
8 Correct 4081 ms 106696 KB Output is correct
9 Correct 12 ms 19020 KB Output is correct
10 Correct 1509 ms 73372 KB Output is correct
11 Correct 434 ms 26496 KB Output is correct
12 Correct 4199 ms 107852 KB Output is correct
13 Correct 4239 ms 108040 KB Output is correct
14 Correct 4388 ms 107884 KB Output is correct
15 Correct 4168 ms 107968 KB Output is correct
16 Correct 11 ms 19020 KB Output is correct
17 Correct 11 ms 19020 KB Output is correct
18 Correct 205 ms 39340 KB Output is correct
19 Correct 417 ms 23320 KB Output is correct
20 Correct 1199 ms 43160 KB Output is correct
21 Correct 1315 ms 43164 KB Output is correct
22 Correct 1495 ms 43168 KB Output is correct
23 Correct 1156 ms 43164 KB Output is correct
24 Correct 1326 ms 43244 KB Output is correct
25 Correct 11 ms 19020 KB Output is correct
26 Correct 349 ms 25180 KB Output is correct
27 Correct 1546 ms 74740 KB Output is correct
28 Correct 4182 ms 108036 KB Output is correct
29 Correct 4297 ms 107908 KB Output is correct
30 Correct 4582 ms 108084 KB Output is correct
31 Correct 4522 ms 108172 KB Output is correct
32 Correct 4332 ms 108080 KB Output is correct