Submission #440251

#TimeUsernameProblemLanguageResultExecution timeMemory
440251BaraaArmoushDistributing Candies (IOI21_candies)C++17
29 / 100
936 ms21736 KiB
#include <bits/stdc++.h> #define mid ((l + r) >> 1) #define left (node << 1) #define right (node << 1 | 1) using namespace std; typedef long long ll; const int N = 200200; const int M = 4 * N; int n; int A[N]; int C[N]; ll P[N]; ll tree[M]; int lazy1[M]; ll lazy2[M]; void Merge(int node) { tree[node] = tree[left] + tree[right]; } void push_lazy(int node, int l, int r) { if (lazy1[node]) { tree[node] = (lazy1[node] == 1 ? P[r + 1] - P[l] : 0); if (l < r) { lazy1[left] = lazy1[right] = lazy1[node]; lazy2[left] = lazy2[right] = 0; } lazy1[node] = 0; } if (lazy2[node]) { tree[node] += (r - l + 1) * lazy2[node]; if (l < r) { lazy2[left] += lazy2[node]; lazy2[right] += lazy2[node]; } lazy2[node] = 0; } } void update1(int i, int j, int x, int node = 1, int l = 0, int r = n - 1) { push_lazy(node, l, r); if (i > j || l > r) { return; } if (j < l || r < i) { return; } if (i <= l && r <= j) { lazy1[node] = x; lazy2[node] = 0; return push_lazy(node, l, r); } update1(i, j, x, left, l, mid); update1(i, j, x, right, mid + 1, r); Merge(node); } void update2(int i, int j, ll x, int node = 1, int l = 0, int r = n - 1) { push_lazy(node, l, r); if (i > j || l > r) { return; } if (j < l || r < i) { return; } if (i <= l && r <= j) { lazy2[node] = x; return push_lazy(node, l, r); } update2(i, j, x, left, l, mid); update2(i, j, x, right, mid + 1, r); Merge(node); } int query(int i, int node = 1, int l = 0, int r = n - 1) { push_lazy(node, l, r); if (i < l || r < i) { return -1; } if (l == r) { return tree[node]; } return i <= mid ? query(i, left, l, mid) : query(i, right, mid + 1, r); } void build(int node = 1, int l = 0, int r = n - 1) { push_lazy(node, l, r); if (l == r) { return void(A[l] = tree[node]); } build(left, l, mid); build(right, mid + 1, r); } #undef mid #undef left #undef right int sign(int x) { return x > 0 ? +1 : x < 0 ? -1 : 0; } vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) { n = c.size(); int q = l.size(); vector<int> b(n); iota(b.begin(), b.end(), 0); sort(b.begin(), b.end(), [&c](int i, int j) { return c[i] < c[j]; }); for (int i = 0; i < n; i++) { C[i] = c[i]; } sort(C, C + n); for (int i = 0; i < n; i++) { P[i + 1] = P[i] + C[i]; } for (int j = 0; j < q; j++) { int can = 0; int low = 1, high = n; while (low <= high) { int mid = (low + high) / 2; int x = query(mid - 1); if (v[j] > 0) { if (x + v[j] < C[mid - 1]) { high = mid - 1; } else { can = mid; low = mid + 1; } } else { if (x + v[j] > 0) { high = mid - 1; } else { can = mid; low = mid + 1; } } } update1(0, can - 1, sign(v[j])); update2(can, n - 1, v[j]); } vector<int> a(n); build(); for (int i = 0; i < n; i++) { a[b[i]] = A[i]; } return a; }
#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...