답안 #447157

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

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep = ""; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream &os, const set<A> &s) { os << '{'; string sep = ""; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << '{'; string sep = ""; for (const auto &x : m) os << sep << x.first << " -> " << x.second, sep = ", "; return os << '}'; }

void debug() { cerr << endl; }
template<typename Ini, typename... Fim> void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << ", "; debug(F...);}
#define db(...) cerr << "DEBUG (" << #__VA_ARGS__ << ") == ", debug(__VA_ARGS__)

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;
		// fprintf(stderr, "%d %d %d : %lld\n", node, i, j, lazy[node]);
		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); }
	void deb(int node, int i, int j) {
		flush(node, i, j);
		if(i == j) return (void)(fprintf(stderr, "(%d %lld) ", i, tree[node][0]));
		int m = (i+j) >> 1;
		deb(node<<1, i, m); deb(node<<1|1, m+1, j);
	}
	int last(int node, int i, int j, int val) {
		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(int val) { return last(1, 0, n-1, val); }
	int 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; // n-1 == j
		return ultimo(node<<1|1, m+1);
	}
} seg;

int get(int c) {
	int l = 0, r = n-1;
	long long ans = 0; // quanto eu vou remover
	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);
	
	fprintf(stderr, "ans %lld\n", ans);

	return -1;
}

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
    n = (int)(v.size());
	for(int i = 0; i < n; i++)
		seg.upd(i, v[i]);
	long long mn = seg.mn(0);
	if(mn < 0) seg.upd(0, -mn);

	vector<int> ans;
	for(int i = 0; i < (int)(c.size()); i++)
		// fprintf(stderr, "%d\n", get(c[i]));
		ans.push_back(get(c[i]));

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1351 ms 20012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 125 ms 18656 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -