Submission #377125

#TimeUsernameProblemLanguageResultExecution timeMemory
377125Kevin_Zhang_TWCake 3 (JOI19_cake3)C++17
100 / 100
1604 ms13240 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long ; #define pb emplace_back #define AI(i) begin(i), end(i) template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); } template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); } #ifdef KEV #define DE(args...) kout("[ " + string(#args) + " ] = ", args) void kout() { cerr << endl; } template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); } template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; } #else #define DE(...) 0 #define debug(...) 0 #endif const int MAX_N = 1<<18; const ll linf = 1ll << 55; int n, k; // color, value pair<int,int> cake[MAX_N]; struct lisan : vector<int> { void done() { sort(AI()), erase(unique(AI()), end()); } int operator()(int i) { return lower_bound(AI(), i) - begin(); } }allv; ll res = -linf; struct sgt { ll sum[MAX_N << 1], cnt[MAX_N << 1]; int cl = 0, cr = -1; void add(int i, int d) { int val = allv[i]; for (i += MAX_N;i;i>>=1) sum[i] += val * d, cnt[i] += d; } ll qry(int k = ::k) { if (cnt[1] < k) return -linf; int now = 1; ll res = 0; while (now < MAX_N) { DE(cnt[now<<1|1], k); if (cnt[now<<1|1] > k) { now = now << 1 | 1; } else { k -= cnt[now<<1|1]; res += sum[now<<1|1]; now = now << 1; } } return res + (ll)k * allv[now - MAX_N]; } void reset() { memset(sum, 0, sizeof(sum)); memset(cnt, 0, sizeof(cnt)); cl = 0, cr = -1; } void update(int l, int r) { assert(l <= r); if (r < cr || l < cl) reset(); while (cr < r) add(cake[++cr].second, 1); while (cl < l) add(cake[cl++].second,-1); assert(cl == l && cr == r); } }tree; ll cost(int i, int j) { return 2ll * abs(cake[i].first - cake[j].first); } void solve() { // lisan values for (int i = 0;i < n;++i) { auto &[c, v] = cake[i]; v = allv(v); } queue< tuple<int,int,int,int> > q; q.emplace(k-1, n-1, 0, n-1); while (q.size()) { auto [l, r, fl, fr] = q.front(); q.pop(); if (l > r) continue; int mid = l + r >> 1; ll mx = -linf, pos = -1; assert(mid - fl + 1 >= k); for (int i = fl;i <= min(mid, fr);++i) { tree.update(i, mid); ll val = tree.qry() - cost(i, mid); if (chmax(mx, val)) pos = i; } if (pos == -1) continue; chmax(res, mx); q.emplace(l, mid-1, fl, pos); q.emplace(mid+1, r, pos, fr); } } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> k; for (int i = 0;i < n;++i) { auto &[c, v] = cake[i]; cin >> v >> c; allv.pb(v); } allv.done(); sort(cake, cake + n); solve(); cout << res << '\n'; }

Compilation message (stderr)

cake3.cpp: In member function 'll sgt::qry(int)':
cake3.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
cake3.cpp:44:4: note: in expansion of macro 'DE'
   44 |    DE(cnt[now<<1|1], k);
      |    ^~
cake3.cpp: In function 'void solve()':
cake3.cpp:86:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |   int mid = l + r >> 1;
      |             ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...