Submission #441545

#TimeUsernameProblemLanguageResultExecution timeMemory
441545SorahISADistributing Candies (IOI21_candies)C++17
0 / 100
1037 ms19712 KiB
#include "candies.h" #pragma GCC optimize("Ofast", "unroll-loops") #include <bits/stdc++.h> using namespace std; // #define int long long // #define double long double using pii = pair<int, int>; template<typename T> using Prior = std::priority_queue<T>; template<typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>; #define X first #define Y second #define eb emplace_back #define pb pop_back #define pf pop_front #define ALL(x) begin(x), end(x) #define RALL(x) rbegin(x), rend(x) namespace { mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 1 << 18; struct SegmentTree { int tag, val, last_change; /// tag is for range modify, val has no use if is not leaf char last_type; SegmentTree() {tag = 1, last_change = 0, last_type = 'E';} }; SegmentTree seg[maxn*2]; vector<int> C, V, pre_V; void PushTag(int idx) { if (seg[idx].tag == 1) return; seg[idx<<1].tag = seg[idx<<1|1].tag = seg[idx].tag; if (seg[idx].tag == 0) seg[idx<<1].last_type = seg[idx<<1|1].last_type = 'E'; if (seg[idx].tag == -1) seg[idx<<1].last_type = seg[idx<<1|1].last_type = 'F'; seg[idx<<1].last_change = seg[idx<<1|1].last_change = seg[idx].last_change; seg[idx].tag = 1; } void Pull(int idx) { /// no need (?) /// return; } int CheckVal(int idx, int qID) { deque<int> stk; int goal = idx + maxn; while (goal > 1) stk.eb(goal >>= 1); while (!stk.empty()) PushTag(stk.back()), stk.pb(); goal = idx + maxn; /// pre_V[qID+1] - pre_V[last_change] /// // cout << seg[goal].last_type << " " << seg[goal].last_change << ": "; if (seg[goal].last_type == 'E') return pre_V[qID] - pre_V[seg[goal].last_change]; if (seg[goal].last_type == 'F') return pre_V[qID] - pre_V[seg[goal].last_change] + C[idx]; } void PointModify(int qX, int val, int now = 1, int nL = 0, int nR = maxn-1) { if (nL == nR) return seg[now].val = val, void(); PushTag(now); int nM = nL + nR >> 1; if (qX <= nM) PointModify(qX, val, now<<1, nL, nM); else PointModify(qX, val, now<<1|1, nM+1, nR); } void RangeModify(int qL, int qR, int val, int qID, int now = 1, int nL = 0, int nR = maxn-1) { if (qL <= nL and nR <= qR) { seg[now].tag = seg[now].val = val; seg[now].last_change = qID; seg[now].last_type = val == 0 ? 'E' : 'F'; return; } if (qR < nL or nR < qL) return; PushTag(now); int nM = nL + nR >> 1; RangeModify(qL, qR, val, qID, now<<1, nL, nM); RangeModify(qL, qR, val, qID, now<<1|1, nM+1, nR); } } /// end of namespace vector<int> distribute_candies(vector<int> _C, vector<int> L, vector<int> R, vector<int> _V) { C.eb(0), V.eb(0); int N = _C.size(), Q = _V.size(); vector<pii> idx_C; for (int i = 0; i < N; ++i) C.eb(_C[i]), idx_C.eb(_C[i], i); for (auto &x : _V) V.eb(x); pre_V.assign(Q+1, 0), partial_sum(ALL(V), begin(pre_V)); sort(ALL(C)), sort(ALL(idx_C)); for (int i = 1; i <= Q; ++i) { // cout << "\n======\n"; // for (int j = 0; j <= N; ++j) cout << CheckVal(j, i-1) << "\n"; // cout << "======\n"; int lo = 0, hi = N, mi; if (V[i] > 0) { /// nothing or full /// while (lo < hi) { mi = lo + hi >> 1; if (CheckVal(mi, i-1) + V[i] >= C[mi]) lo = mi + 1; /// [0, mi] is full else hi = mi; /// [mi, N] is not full } /// [0, lo) is full /// PointModify(lo, CheckVal(lo, i-1) - CheckVal(lo-1, i-1)); RangeModify(0, lo-1, -1, i); } else { /// nothing or empty /// while (lo < hi) { mi = lo + hi >> 1; if (CheckVal(mi, i-1) + V[i] <= 0) lo = mi + 1; /// [0, mi] is empty else hi = mi; /// [mi, N] is not empty } /// [0, lo) is empty /// PointModify(lo, CheckVal(lo, i-1) - CheckVal(lo-1, i-1)); RangeModify(0, lo-1, 0, i); } // cout << "\nbinary search gets " << lo << "\n"; } // for (int i = 1; i < maxn; ++i) PushTag(i); // vector<int> tmp; // for (int i = maxn+1; tmp.size() < N; ++i) tmp.eb(seg[i].val == -1 ? C[i] : seg[i].val); // partial_sum(ALL(tmp), begin(tmp)); // cout << "\n"; vector<int> ans(N); for (int i = 1; i <= N; ++i) { int tmp_val = CheckVal(i-1, Q); // cout << tmp_val << "\n"; ans[idx_C[i-1].Y] = /*tmp[i-1];*/ tmp_val; } return ans; }

Compilation message (stderr)

candies.cpp: In function 'void {anonymous}::PointModify(int, int, int, int, int)':
candies.cpp:67:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |     int nM = nL + nR >> 1;
      |              ~~~^~~~
candies.cpp: In function 'void {anonymous}::RangeModify(int, int, int, int, int, int, int)':
candies.cpp:81:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |     int nM = nL + nR >> 1;
      |              ~~~^~~~
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:105:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  105 |                 mi = lo + hi >> 1;
      |                      ~~~^~~~
candies.cpp:116:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  116 |                 mi = lo + hi >> 1;
      |                      ~~~^~~~
candies.cpp: In function 'int {anonymous}::CheckVal(int, int)':
candies.cpp:53:16: warning: control reaches end of non-void function [-Wreturn-type]
   53 |     deque<int> stk;
      |                ^~~
candies.cpp: At global scope:
candies.cpp:47:6: warning: 'void {anonymous}::Pull(int)' defined but not used [-Wunused-function]
   47 | void Pull(int idx) {
      |      ^~~~
#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...