#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#include "candies.h"
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long long inf = 2e9;
using namespace std;
struct Nodemaxmin {
Nodemaxmin* l = 0, * r = 0;
int lo, hi, mset = inf, madd = 0;
pair<int,int> val = make_pair(-inf,-inf);
Nodemaxmin(int lo, int hi) :lo(lo), hi(hi) {} // Large interval of -inf
Nodemaxmin(vector<pair<int,int>> & v, int lo, int hi) : lo(lo), hi(hi) {
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Nodemaxmin(v, lo, mid); r = new Nodemaxmin(v, mid, hi);
val = max(l->val, r->val);
}
else val = make_pair(v[lo].first,-v[lo].second);
}
pair<int,int> query(int L, int R) {
if (R <= lo || hi <= L) return make_pair(-inf,-inf);
if (L <= lo && hi <= R) return val;
push();
return max(l->query(L, R), r->query(L, R));
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) mset = val.first = x, madd = 0;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = max(l->val, r->val);
}
}
void add(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
if (mset != inf) mset += x;
else madd += x;
val.first += x;
}
else {
push(), l->add(L, R, x), r->add(L, R, x);
val = max(l->val, r->val);
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo) / 2;
l = new Nodemaxmin(lo, mid); r = new Nodemaxmin(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
}
};
struct Nodemaxmax {
Nodemaxmax* l = 0, * r = 0;
int lo, hi, mset = inf, madd = 0;
pair<int,int> val = make_pair(-inf,-inf);
Nodemaxmax(int lo, int hi) :lo(lo), hi(hi) {} // Large interval of -inf
Nodemaxmax(vector<pair<int,int>>& v, int lo, int hi) : lo(lo), hi(hi) {
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Nodemaxmax(v, lo, mid); r = new Nodemaxmax(v, mid, hi);
val = max(l->val, r->val);
}
else val = v[lo];
}
pair<int,int> query(int L, int R) {
if (R <= lo || hi <= L) return make_pair(-inf,-inf);
if (L <= lo && hi <= R) return val;
push();
return max(l->query(L, R), r->query(L, R));
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) mset = val.first = x, madd = 0;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = max(l->val, r->val);
}
}
void add(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
if (mset != inf) mset += x;
else madd += x;
val.first += x;
}
else {
push(), l->add(L, R, x), r->add(L, R, x);
val = max(l->val, r->val);
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo) / 2;
l = new Nodemaxmax(lo, mid); r = new Nodemaxmax(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
}
};
struct Nodeminmax {
Nodeminmax* l = 0, * r = 0;
int lo, hi, mset = inf, madd = 0;
pair<int,int> val = make_pair(inf,inf);
Nodeminmax(int lo, int hi) :lo(lo), hi(hi) {} // Large interval of -inf
Nodeminmax(vector<pair<int,int>>& v, int lo, int hi) : lo(lo), hi(hi) {
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Nodeminmax(v, lo, mid); r = new Nodeminmax(v, mid, hi);
val = min(l->val, r->val);
}
else val = make_pair(v[lo].first,-v[lo].second);
}
pair<int,int> query(int L, int R) {
if (R <= lo || hi <= L) return make_pair(inf,inf);
if (L <= lo && hi <= R) return val;
push();
return min(l->query(L, R), r->query(L, R));
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) mset = val.first = x, madd = 0;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = min(l->val, r->val);
}
}
void add(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
if (mset != inf) mset += x;
else madd += x;
val.first += x;
}
else {
push(), l->add(L, R, x), r->add(L, R, x);
val = min(l->val, r->val);
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo) / 2;
l = new Nodeminmax(lo, mid); r = new Nodeminmax(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
}
};
struct Nodeminmin {
Nodeminmin* l = 0, * r = 0;
int lo, hi, mset = inf, madd = 0;
pair<int,int> val = make_pair(inf,inf);
Nodeminmin(int lo, int hi) :lo(lo), hi(hi) {} // Large interval of -inf
Nodeminmin(vector<pair<int,int>>& v, int lo, int hi) : lo(lo), hi(hi) {
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Nodeminmin(v, lo, mid); r = new Nodeminmin(v, mid, hi);
val = min(l->val, r->val);
}
else val = v[lo];
}
pair<int,int> query(int L, int R) {
if (R <= lo || hi <= L) return make_pair(inf,inf);
if (L <= lo && hi <= R) return val;
push();
return min(l->query(L, R), r->query(L, R));
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) mset = val.first = x, madd = 0;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = min(l->val, r->val);
}
}
void add(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
if (mset != inf) mset += x;
else madd += x;
val.first += x;
}
else {
push(), l->add(L, R, x), r->add(L, R, x);
val = min(l->val, r->val);
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo) / 2;
l = new Nodeminmin(lo, mid); r = new Nodeminmin(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
}
};
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l, std::vector<int> r, std::vector<int> v) {
int n = c.size();
vector<int> ans(n);
vector<pair<int, int>> vec(n);
rep(i, 0, n)vec[i].second = i;
Nodemaxmax* nxx = new Nodemaxmax(vec, 0, n);
Nodemaxmin* nxn = new Nodemaxmin(vec, 0, n);
Nodeminmax* nnx = new Nodeminmax(vec, 0, n);
Nodeminmin* nnn = new Nodeminmin(vec, 0, n);
rep(i, 0, l.size()) {
nxx->add(l[i], r[i] + 1, v[i]);
nxn->add(l[i], r[i] +1, v[i]);
nnx->add(l[i], r[i] + 1, v[i]);
nnn->add(l[i], r[i] + 1, v[i]);
if (nxx->query(0, n).first > c[0]) {
pair<int, int> cur1 = nxn->query(0, n);
pair<int, int> cur2 = nxx->query(0, n);
nxx->add(-cur1.second, cur2.second + 1, c[0] - cur1.first);
nxn->add(-cur1.second, cur2.second + 1, c[0] - cur1.first);
nnx->add(-cur1.second, cur2.second + 1, c[0] - cur1.first);
nnn->add(-cur1.second, cur2.second+ 1, c[0] - cur1.first);
}
if (nnn->query(0, n).first < 0) {
pair<int, int> cur1 = nnn->query(0, n);
pair<int, int> cur2 = nnx->query(0, n);
nxx->add(cur1.second, cur2.second + 1, c[0] - cur1.first);
nxn->add(cur1.second, cur2.second+ 1, c[0] - cur1.first);
nnx->add(cur1.second, cur2.second+ 1, c[0] - cur1.first);
nnn->add(cur1.second, cur2.second+ 1, c[0] - cur1.first);
}
}
rep(i, 0, n)ans[i] = nxx->query(i, i + 1).first;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2805 ms |
84064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
588 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 |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |