#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define forn(i, x, y) for(int i = (x); i < (y); i++)
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
#define int long long
#define double long double
const int maxn = 2e5 + 5, inf = 1e18;
struct node {
int sum, mx, mn;
node(): sum(0), mx(-0), mn(0) {}
node(int x): sum(x), mx(max(0ll, x)), mn(min(0ll, x)) {}
} d[maxn * 4];
node merge(node a, node b) {
node res;
res.sum = a.sum + b.sum;
res.mx = max(a.mx, b.mx + a.sum);
res.mn = min(a.mn, b.mn + a.sum);
return res;
}
void change(int v, int tl, int tr, int idx, int val) {
if(tl == tr) {
d[v] = node(val);
return;
}
int tm = (tl + tr) / 2;
if(idx <= tm) {
change(2*v, tl, tm, idx, val);
}
else {
change(2*v+1, tm+1, tr, idx, val);
}
d[v] = merge(d[2*v], d[2*v+1]);
}
node get(int v, int tl, int tr, int l, int r) {
if(l > r) {
return node(); /// check this
}
if(l == tl && r == tr) {
return d[v];
}
int tm = (tl + tr) / 2;
node x = get(2*v, tl, tm, l, min(r, tm));
node y = get(2*v+1, tm+1, tr, max(l, tm+1), r);
return merge(x, y);
}
void debug(int v, int tl, int tr) {
if(tl == tr) {
cout << d[v].sum << " ";
return;
}
int tm = (tl + tr) / 2;
debug(2*v, tl, tm);
debug(2*v+1, tm+1, tr);
}
vector<signed> distribute_candies(vector<signed> C, vector<signed> L, vector<signed> R, vector<signed> V) {
int n = C.size(), q = L.size();
vector<int> c(n);
forn(i, 0, n) {
c[i] = C[i];
}
q += 3;
vector<int> val(q);
val[0] = inf;
val[1] = -inf;
val[q-1] = 0;
vector<vector<int>> st(n), en(n);
st[0].push_back(0);
st[0].push_back(1);
en[n-1].push_back(0);
en[n-1].push_back(1);
forn(i, 2, q-1) {
int l, r, x;
l = L[i-2];
r = R[i-2];
val[i] = V[i-2];
st[l].push_back(i);
en[r].push_back(i);
}
vector<signed> ans(n);
forn(i, 0, n) {
for(auto it : st[i]) {
change(1, 0, q-1, it, val[it]);
}
cout << i << ": \n";
debug(1, 0, q-1);
cout << "\n";
int l = 0, r = q-1;
while(l < r) {
int mid = (l + r) / 2;
node cur = get(1, 0, q-1, mid, q-1);
if(cur.mx - cur.mn > c[i]) {
l = mid + 1;
}
else {
r = mid;
}
}
node prev = get(1, 0, q-1, l, q-1);
node nxt = get(1, 0, q-1, l-1, q-1);
nxt.mn -= val[l-1];
nxt.mx -= val[l-1];
node total = get(1, 0, q-1, l, q-1);
int real;
if(prev.mx == nxt.mx) {
real = c[i] - (prev.mx - total.sum);
}
else {
real = total.sum - prev.mn;
}
ans[i] = real;
// cout << real << " ";
for(auto it : en[i]) {
change(1, 0, q-1, it, 0);
}
}
return ans;
}
//signed main()
//{
//// ios_base::sync_with_stdio(0);
//// cin.tie(0);
//// cout.tie(0);
//
// vector<int> ans = distribute_candies({10, 15, 13}, {0, 0}, {2, 1}, {20, -11});
// for(auto it : ans) {
// cout << it << " ";
// }
//
// return 0;
//}
/*
1 7
3
0 0 -100
0 0 10
0 0 1
0 0 -5
0 0 1
0 0 -5
0 0 5
*/
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:83:19: warning: unused variable 'x' [-Wunused-variable]
83 | int l, r, x;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
19020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5082 ms |
186512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
19148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
19084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
19020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |