Submission #435895

#TimeUsernameProblemLanguageResultExecution timeMemory
435895illyakrDistributing Candies (IOI21_candies)C++17
40 / 100
879 ms24752 KiB
#include <bits/stdc++.h> #include "candies.h" using namespace std; #define ll long long vector<int> res; int n, q; bool scbl = true; ll _res[201010]; ll t[801010]; ll tAdd[801010]; ll tPress[801010]; pair<ll, ll> Up[201010]; void push(int v, int vl, int vr) { if (tPress[v] == -1 && tAdd[v] == 0)return; if (vl == vr) { if (tPress[v] != -1){t[v] = tPress[v];t[v] = min(t[v], Up[vl].first);} if (tAdd[v] != 0)t[v] += tAdd[v]; if (t[v] < 0 || t[v] > Up[vl].first) { exit(1); } } else { if (tPress[v] != -1) { tAdd[2 * v] = tAdd[v]; tPress[2 * v] = tPress[v]; tAdd[2 * v + 1] = tAdd[v]; tPress[2 * v + 1] = tPress[v]; } else if (tAdd[v] != 0) { tAdd[2 * v] += tAdd[v]; tAdd[2 * v + 1] += tAdd[v]; } } tPress[v] = -1; tAdd[v] = 0; } void updAdd(int v, int vl, int vr, int l, int r, ll val) { push(v, vl, vr); if (l <= vl && vr <= r) { tAdd[v] += val; push(v, vl, vr); return; } if (r < vl || vr < l)return; int vm = (vl + vr) / 2; updAdd(2 * v, vl, vm, l, r, val); updAdd(2 * v + 1, vm + 1, vr, l, r, val); } void updPress(int v, int vl, int vr, int l, int r, int val) { push(v, vl, vr); if (l <= vl && vr <= r) { tPress[v] = val; push(v, vl, vr); return; } if (r < vl || vr < l)return; int vm = (vl + vr) / 2; updPress(2 * v, vl, vm, l, r, val); updPress(2 * v + 1, vm + 1, vr, l, r, val); } int gt(int v, int vl, int vr, int pos) { push(v, vl, vr); if (vl == vr)return t[v]; int vm = (vl + vr) / 2; if (pos <= vm)return gt(2 * v, vl, vm, pos); return gt(2 * v + 1, vm + 1, vr, pos); } std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l, std::vector<int> r, std::vector<int> v) { n = c.size(); q = v.size(); res.resize(n, 0); for (int i = 0; i < q; i++) if (v[i] < 0){scbl = false;break;} if (n * q <= 100000000) { for (int i = 0; i < q; i++) { for (int j = l[i]; j <= r[i]; j++) { res[j] += v[i]; if (res[j] < 0)res[j] = 0; else if (res[j] > c[j])res[j] = c[j]; } } return res; } if (scbl) { for (int i = 0; i < q; i++) { _res[l[i]] += v[i]; _res[r[i] + 1] -= v[i]; } for (int i = 1; i < n; i++) _res[i] += _res[i - 1]; for (int i = 0; i < n; i++) { _res[i] = min(_res[i], (ll)c[i]); res[i] = _res[i]; } return res; } for (int i = 0; i < n; i++) Up[i] = {c[i], i}; sort(Up, Up + n, [](pair<ll, ll> q, pair<ll, ll> w) { return q.first < w.first; }); memset(tPress, -1, sizeof(tPress)); // for (int i = 0; i < n; i++) { // cout << Up[i].first << " " << Up[i].second << endl; // } for (int id = 0; id < q; id++) { int AllLeft = -1; { int l = -1, r = n; while (l + 1 < r) { int mid = (l + r) / 2; int el = gt(1, 0, n - 1, mid); if (v[id] < 0) { if (el + v[id] <= 0)l = mid; else r = mid; } else { if (el + v[id] >= Up[mid].first)l = mid; else r = mid; } } AllLeft = l; } if (v[id] < 0) { if (AllLeft != -1)updPress(1, 0, n - 1, 0, AllLeft, 0); if (AllLeft != n - 1)updAdd(1, 0, n - 1, AllLeft + 1, n - 1, v[id]); } else { if (AllLeft != -1)updPress(1, 0, n - 1, 0, AllLeft, 1101010101010101010); if (AllLeft != n - 1)updAdd(1, 0, n - 1, AllLeft + 1, n - 1, v[id]); } } for (int i = 0; i < n; i++) res[Up[i].second] = gt(1, 0, n - 1, i); return res; } /** 5 8 9 3 5 10 3 0 5 3 0 5 5 0 5 -4 8 8 3 5 8 3 10 15 13 2 0 2 20 0 1 -11 0 4 13 */

Compilation message (stderr)

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:130:65: warning: overflow in conversion from 'long int' to 'int' changes value from '1101010101010101010' to '2094199570' [-Woverflow]
  130 |             if (AllLeft != -1)updPress(1, 0, n - 1, 0, AllLeft, 1101010101010101010);
      |                                                                 ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...