#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
const int mxN = 2e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
pair<pair<int,int>,int> seg[5 * mxN];
int lazy[5 * mxN], n, q, len;
void push(int ind, int l, int r) {
seg[ind].f.f += lazy[ind], seg[ind].f.s += lazy[ind];
if (l != r) {
lazy[2 * ind] += lazy[ind];
lazy[2 * ind + 1] += lazy[ind];
}
lazy[ind] = 0;
}
void update(int lo, int hi, int val, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (lo > r || l > hi) {
return;
}
if (lo <= l && r <= hi) {
lazy[ind] += val;
push(ind, l, r);
if (l == r) {
seg[ind].s = l;
}
return;
}
int mid = (l + r) / 2;
update(lo, hi, val, 2 * ind, l, mid);
update(lo, hi, val, 2 * ind + 1, mid + 1, r);
seg[ind].f.f = min(seg[2 * ind].f.f, seg[2 * ind + 1].f.f);
seg[ind].f.s = max(seg[2 * ind].f.s, seg[2 * ind + 1].f.s);
}
int query(int pos, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (l == r) {
return seg[ind].f.f;
}
int mid = (l + r) / 2;
if (pos <= mid) {
return query(pos, 2 * ind, l, mid);
}
else {
return query(pos, 2 * ind + 1, mid + 1, r);
}
}
int walk(int lo, int val, bool t, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (lo > r || (t && seg[ind].f.s <= val) || (!t && seg[ind].f.f >= val)) {
return (t ? INT_MIN : INT_MAX);
}
if (l == r) {
if (t) {
update(l, len, -seg[ind].f.s + val);
}
else {
update(l, len, -seg[ind].f.f);
}
return seg[ind].s;
}
int mid = (l + r) / 2;
if (l >= lo) {
if ((t && seg[2*ind].f.s > val) || (!t && seg[2*ind].f.f < val)) {
return walk(lo, val, t, 2 * ind, l, mid);
}
else {
return walk(lo, val, t, 2 * ind + 1, mid + 1, r);
}
}
int res = walk(lo, val, t, 2 * ind, l, mid);
if (res == INT_MIN || res == INT_MAX) {
res = walk(lo, val, t, 2 * ind + 1, mid + 1, r);
}
return res;
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
n = c.size(), q = v.size();
int cur = 0;
vector<int> calc;
for (int i = 0; i < q; ++i) {
cur += v[i];
calc.push_back(cur);
if (cur <= 0) {
cur = 0;
calc.clear();
}
}
len = calc.size();
for (int i = 0; i < calc.size(); ++i) {
update(i + 1, i + 1, calc[i]);
}
vector<pair<int,int>> arr;
for (int i = 0; i < n; ++i) {
arr.push_back({c[i], i});
}
sort(arr.begin(), arr.end(), greater<pair<int,int>>());
int ind = 1;
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
while (walk(ind, arr[i].f, 1) != INT_MIN) {
int nex = walk(ind, 0, 0);
if (nex != INT_MAX) {
ind = nex + 1;
}
}
/*
for (int j = ind; j <= len; ++j) {
cout << query(j) << ' ';
}
cout << "\n";
*/
ans[arr[i].s] = query(len);
}
return ans;
}
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:113:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int i = 0; i < calc.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
210 ms |
10924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
308 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |