Submission #436893

#TimeUsernameProblemLanguageResultExecution timeMemory
436893maomao90Distributing Candies (IOI21_candies)C++17
3 / 100
820 ms37824 KiB
#include "candies.h" #include <bits/stdc++.h> using namespace std; #define REP(i, j, k) for (int i = j; i < k; i++) #define RREP(i, j, k) for (int i = j; i >= k; i--) template <class T> inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;} template <class T> inline bool mxto(T& a, T b) {return a < b ? a = b, 1 : 0;} typedef long long ll; #define MP make_pair #define FI first #define SE second typedef pair<int, int> ii; #define MT make_tuple typedef tuple<int, int, int> iii; #define ALL(x) x.begin(), x.end() #define pb emplace_back typedef vector<int> vi; typedef vector<ii> vii; #ifdef DEBUG #define debug(args...) _debug(args) void _debug(const char* format, ...) { va_list args; va_start(args, format); vprintf(format, args); va_end(args); } #else #define debug(args...) #endif #define INF 1000000005 #define LINF 1000000000000000005 #define MAXN 200005 int n, q; vi c; vi ans; vi qs[MAXN], qe[MAXN]; #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1 ll mx[MAXN * 4], mn[MAXN * 4], lazy[MAXN * 4]; void propo(int u, int lo, int hi) { if (lazy[u] == 0 || lo == hi) return; MLR; mx[lc] += lazy[u]; mn[lc] += lazy[u]; mx[rc] += lazy[u]; mn[rc] += lazy[u]; lazy[lc] += lazy[u]; lazy[rc] += lazy[u]; lazy[u] = 0; } void incre(int s, int e, int x, int u, int lo, int hi) { if (lo >= s && hi <= e) { lazy[u] += x; mn[u] += x; mx[u] += x; return; } propo(u, lo, hi); MLR; if (s <= mid) incre(s, e, x, lc, lo, mid); if (e > mid) incre(s, e, x, rc, mid + 1, hi); mn[u] = min(mn[lc], mn[rc]); mx[u] = max(mx[lc], mx[rc]); } void incre(int s, int e, int x) { incre(s, e, x, 1, 0, q); } ll getmax(int s, int e, int u, int lo, int hi) { if (lo >= s && hi <= e) { return mx[u]; } propo(u, lo, hi); MLR; if (e <= mid) return getmax(s, e, lc, lo, mid); else if (s > mid) return getmax(s, e, rc, mid + 1, hi); else return max(getmax(s, e, lc, lo, mid), getmax(s, e, rc, mid + 1, hi)); } ll getmax(int s, int e) { return getmax(s, e, 1, 0, q); } ll getmin(int s, int e, int u, int lo, int hi) { if (lo >= s && hi <= e) { return mn[u]; } propo(u, lo, hi); MLR; if (e <= mid) return getmin(s, e, lc, lo, mid); else if (s > mid) return getmin(s, e, rc, mid + 1, hi); else return min(getmin(s, e, lc, lo, mid), getmin(s, e, rc, mid + 1, hi)); } ll getmin(int s, int e) { return getmin(s, e, 1, 0, q); } int upper(int x, ll pmn, ll pmx, int u, int lo, int hi) { if (lo == hi) { return lo; } propo(u, lo, hi); MLR; int npmn = min(pmn, mn[rc]), npmx = max(pmx, mx[rc]); if (npmx - npmn > x) { return upper(x, pmn, pmx, rc, mid + 1, hi); } else { return upper(x, npmn, npmx, lc, lo, mid); } } int upper(int s, ll pmn, ll pmx) { return upper(s, pmn, pmx, 1, 0, q); } vi distribute_candies(vi c, vi l, vi r, vi v) { ::c = c; n = c.size(); q = l.size(); l.insert(l.begin(), 0); r.insert(r.begin(), 0); v.insert(v.begin(), 0); REP (i, 1, q + 1) { qs[l[i]].pb(i); qe[r[i]].pb(i); } ans.resize(n); REP (i, 0, n) { for (int &j : qs[i]) { incre(j, q, v[j]); debug(" incre %d %d: %d\n", j, q, v[j]); } ll sum = getmax(q, q); debug("%lld %lld %lld\n", sum, mx[1], mn[1]); if (mx[1] - mn[1] <= c[i]) { ans[i] = sum - mn[1]; } else { int id = upper(c[i], LINF, -LINF); ll cur = getmax(id, id); ll mn = getmin(id + 1, q); ll mx = getmax(id + 1, q); debug(" %d %lld %lld %lld\n", id, cur, mn, mx); if (cur < mn) { ans[i] = sum - (mx - c[i]); } else { ans[i] = sum - mn; } } for (int &j : qe[i]) { incre(j, q, -v[j]); debug(" decre %d %d: %d\n", j, q, -v[j]); } } return ans; }

Compilation message (stderr)

candies.cpp: In function 'void propo(int, int, int)':
candies.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1
      |                       ~~~^~~~
candies.cpp:51:2: note: in expansion of macro 'MLR'
   51 |  MLR;
      |  ^~~
candies.cpp:47:17: warning: unused variable 'mid' [-Wunused-variable]
   47 | #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1
      |                 ^~~
candies.cpp:51:2: note: in expansion of macro 'MLR'
   51 |  MLR;
      |  ^~~
candies.cpp: In function 'void incre(int, int, int, int, int, int)':
candies.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1
      |                       ~~~^~~~
candies.cpp:68:2: note: in expansion of macro 'MLR'
   68 |  MLR;
      |  ^~~
candies.cpp: In function 'll getmax(int, int, int, int, int)':
candies.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1
      |                       ~~~^~~~
candies.cpp:82:2: note: in expansion of macro 'MLR'
   82 |  MLR;
      |  ^~~
candies.cpp: In function 'll getmin(int, int, int, int, int)':
candies.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1
      |                       ~~~^~~~
candies.cpp:95:2: note: in expansion of macro 'MLR'
   95 |  MLR;
      |  ^~~
candies.cpp: In function 'int upper(int, ll, ll, int, int, int)':
candies.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1; int lc = u * 2, rc = u * 2 + 1
      |                       ~~~^~~~
candies.cpp:109:2: note: in expansion of macro 'MLR'
  109 |  MLR;
      |  ^~~
#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...