#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define pb push_back
#define pii pair<int, int>
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define st first
#define nd second
#define ll long long
#define LL node * 2
#define RR node * 2 + 1
#define N 300005
#define LOGN 20
const int modulo = 1e9 + 7;
const ll INF = 2e18 + 7;
int tree[4 * N], lazy[4 * N], lazy_set[4 * N];
int arr[N];
void push(int node, int l, int r){
tree[node] += lazy[node];
if (lazy_set[node] != -1){
if (l == r){
if (lazy_set[node] == 1) tree[node] = arr[l];
else if (lazy_set[node] == 0) tree[node] = 0;
}
else{
lazy_set[LL] = lazy_set[node];
lazy_set[RR] = lazy_set[node];
}
}
if (l != r){
lazy[LL] += lazy[node];
lazy[RR] += lazy[node];
}
lazy[node] = 0;
lazy_set[node] = -1;
}
void build(int node, int l, int r){
tree[node] = 0;
lazy[node] = 0;
lazy_set[node] = -1;
if (l == r) return;
int mid = (l + r) / 2;
build(LL,l, mid);
build(RR, mid + 1, r);
}
void update1(int node, int l, int r, int sl, int sr, int val){
push(node, l, r);
if (l > sr || r < sl) return;
if (l >= sl && r <= sr){
lazy[node] += val;
push(node, l , r);
return;
}
int mid = (l + r) / 2;
update1(LL, l, mid, sl, sr, val);
update1(RR, mid + 1, r, sl, sr, val);
}
void update2(int node, int l, int r, int sl, int sr, int val){
push(node, l, r);
if (l > sr || r < sl) return;
if (l >= sl && r <= sr){
lazy_set[node] = val;
push(node, l, r);
return;
}
int mid = (l + r) / 2;
update2(LL, l, mid, sl, sr, val);
update2(RR, mid + 1, r, sl, sr, val);
}
int query(int node, int l, int r, int sl){
push(node, l, r);
if (l > r || l > sl || r < sl) return 0;
if (l == r) {
return tree[node];
}
int mid = (l + r) / 2;
if (sl <= mid) return query(LL, l, mid, sl);
return query(RR, mid + 1, r, sl);
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
vector<pii> vv;
for (int i = 0; i < n; i++) vv.pb({c[i], i});
sort(vv.begin(), vv.end());
for (int i = 0; i < n; i++)
arr[i] = vv[i].st;
build(1, 0, n - 1);
int m = l.size();
for (int i = 0; i < m; i++){
int val = v[i];
if (val > 0){
int pos = 0;
for (int j = LOGN; j >= 0; j--){
int tmp = pos + (1<<j);
if (tmp >= n) continue;
int gh = query(1, 0, n - 1, tmp);
if (gh + val >= arr[tmp]) pos = tmp;
}
if (query(1, 0, n - 1, pos) + val >= arr[pos]) pos++;
update2(1, 0, n - 1, 0, pos - 1, 1);
update1(1, 0, n - 1, pos, n - 1, val);
}
else{
int pos = 0;
for (int j = LOGN; j >= 0; j--){
int tmp = pos + (1<<j);
if (tmp >= n) continue;
int gh = query(1, 0, n - 1, tmp);
if (gh + val <= 0) pos = tmp;
}
if (query(1, 0, n - 1, pos) + val <= 0) pos++;
update2(1, 0, n - 1, 0, pos - 1, 0);
update1(1, 0, n - 1, pos, n - 1, val);
}
/*
for (int j = 0; j < n; j++) cout<<query(1, 0, n - 1, j)<<sp;
cout<<endl;*/
}
vector<int> s(n);
for (int i = 0; i < n; i++){
s[vv[i].nd] = query(1, 0, n - 1, i);
}
return s;
}
/*
int main() {
fileio();
int n;
assert(1 == scanf("%d", &n));
std::vector<int> c(n);
for(int i = 0; i < n; ++i) {
assert(scanf("%d", &c[i]) == 1);
}
int q;
assert(1 == scanf("%d", &q));
std::vector<int> l(q), r(q), v(q);
for(int i = 0; i < q; ++i) {
assert(scanf("%d %d %d", &l[i], &r[i], &v[i]) == 3);
}
std::vector<int> ans = distribute_candies(c, l, r, v);
for(int i = 0; i < n; ++i) {
if (i > 0) {
printf(" ");
}
printf("%d", ans[i]);
}
printf("\n");
fclose(stdout);
return 0;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
252 ms |
21360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |