Submission #435207

#TimeUsernameProblemLanguageResultExecution timeMemory
435207CodePlatinaDistributing Candies (IOI21_candies)C++17
100 / 100
868 ms51084 KiB
#include "candies.h" #include <vector> #include <algorithm> #include <tuple> #define pii pair<int, int> #define ff first #define ss second using namespace std; const long long INF = (long long)8e18 + 100; struct Node { long long mx, mn, up, dn, l; Node(void) : mx(0), mn(0), up(0), dn(0), l(0) {} }seg[808080]; void prop(int ind) { if(!seg[ind].l) return; seg[ind << 1].mx += seg[ind].l; seg[ind << 1].mn += seg[ind].l; seg[ind << 1].l += seg[ind].l; seg[ind << 1 | 1].mx += seg[ind].l; seg[ind << 1 | 1].mn += seg[ind].l; seg[ind << 1 | 1].l += seg[ind].l; seg[ind].l = 0; } void mrge(int ind) { Node &nd = seg[ind], &x = seg[ind << 1], &y = seg[ind << 1 | 1]; nd.mx = max(x.mx, y.mx); nd.mn = min(x.mn, y.mn); nd.up = max({x.up, y.up, y.mx - x.mn}); nd.dn = max({x.dn, y.dn, x.mx - y.mn}); } void upd(int ind, int s, int e, int l, int r, long long x) { if(e <= l || r <= s) return; if(l <= s && e <= r) { seg[ind].mn += x; seg[ind].mx += x; seg[ind].l += x; return; } int mid = s + e >> 1; prop(ind); upd(ind << 1, s, mid, l, r, x); upd(ind << 1 | 1, mid, e, l, r, x); mrge(ind); } int ub(int ind, int s, int e, long long x, long long mx) { if(s + 1 == e) { if(mx - seg[ind].mn > x) return s; else return s - 1; } int mid = s + e >> 1; prop(ind); if(seg[ind << 1 | 1].up > x || mx - seg[ind << 1 | 1].mn > x) return ub(ind << 1 | 1, mid, e, x, mx); else return ub(ind << 1, s, mid, x, max(mx, seg[ind << 1 | 1].mx)); } int lb(int ind, int s, int e, long long x, long long mn) { if(s + 1 == e) { if(seg[ind].mx - mn > x) return s; else return s - 1; } int mid = s + e >> 1; prop(ind); if(seg[ind << 1 | 1].dn > x || seg[ind << 1 | 1].mx - mn > x) return lb(ind << 1 | 1, mid, e, x, mn); else return lb(ind << 1, s, mid, x, min(mn, seg[ind << 1 | 1].mn)); } long long xqry(int ind, int s, int e, int l, int r) { if(e <= l || r <= s) return -INF; if(l <= s && e <= r) return seg[ind].mx; int mid = s + e >> 1; prop(ind); return max(xqry(ind << 1, s, mid, l, r), xqry(ind << 1 | 1, mid, e, l, r)); } long long nqry(int ind, int s, int e, int l, int r) { if(e <= l || r <= s) return INF; if(l <= s && e <= r) return seg[ind].mn; int mid = s + e >> 1; prop(ind); return min(nqry(ind << 1, s, mid, l, r), nqry(ind << 1 | 1, mid, e, l, r)); } vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) { int n = c.size(); int T = l.size(); vector<pii> ls[n + 1]; for(int i = 0; i < T; ++i) { ls[l[i]].push_back({i + 1, v[i]}); ls[r[i] + 1].push_back({i + 1, -v[i]}); } long long sum = 0; long long pr = 0; vector<int> ans; for(int i = 0; i < n; ++i) { for(auto [p, x] : ls[i]) upd(1, 0, T + 2, p + 1, T + 2, x), sum += x; upd(1, 0, T + 2, 1, T + 2, -c[i] - 1 - pr); pr = -c[i] - 1; int a = ub(1, 0, T + 2, c[i], -INF); int b = lb(1, 0, T + 2, c[i], INF); if(a > b) ans.push_back(-xqry(1, 0, T + 2, a, T + 2) + sum - 1); else ans.push_back(-nqry(1, 0, T + 2, b, T + 2) - c[i] + sum - 1); } return ans; }

Compilation message (stderr)

candies.cpp: In function 'void upd(int, int, int, int, int, long long int)':
candies.cpp:52:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |     int mid = s + e >> 1;
      |               ~~^~~
candies.cpp: In function 'int ub(int, int, int, long long int, long long int)':
candies.cpp:67:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |     int mid = s + e >> 1;
      |               ~~^~~
candies.cpp: In function 'int lb(int, int, int, long long int, long long int)':
candies.cpp:81:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |     int mid = s + e >> 1;
      |               ~~^~~
candies.cpp: In function 'long long int xqry(int, int, int, int, int)':
candies.cpp:92:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |     int mid = s + e >> 1;
      |               ~~^~~
candies.cpp: In function 'long long int nqry(int, int, int, int, int)':
candies.cpp:102:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  102 |     int mid = s + e >> 1;
      |               ~~^~~
#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...