Submission #521568

# Submission time Handle Problem Language Result Execution time Memory
521568 2022-02-02T12:03:30 Z eecs Distributing Candies (IOI21_candies) C++17
100 / 100
379 ms 38424 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 200010;
int C;
ll mn[maxn << 2], mx[maxn << 2], tag[maxn << 2];
vector<pair<int, int>> Q[maxn];

#define mid (l + r >> 1)
#define ls (k << 1)
#define rs (k << 1 | 1)
void apply(int k, ll v) { mn[k] += v, mx[k] += v, tag[k] += v; }
void pushdown(int k) { apply(ls, tag[k]), apply(rs, tag[k]), tag[k] = 0; }

void add(int k, int l, int r, int ql, int qr, ll v) {
	if (l >= ql && r <= qr) { apply(k, v); return; }
	pushdown(k);
	if (mid >= ql) add(ls, l, mid, ql, qr, v);
	if (mid < qr) add(rs, mid + 1, r, ql, qr, v);
	mn[k] = min(mn[ls], mn[rs]), mx[k] = max(mx[ls], mx[rs]);
}

int find(int k, int l, int r, ll &cmn, ll &cmx) {
	if (l < r && max(cmx, mx[k]) - min(cmn, mn[k]) >= C) {
		pushdown(k);
		int t = find(rs, mid + 1, r, cmn, cmx);
		return ~t ? t : find(ls, l, mid, cmn, cmx);
	} else {
		cmn = min(cmn, mn[k]), cmx = max(cmx, mx[k]);
	}
	return cmx - cmn >= C ? l : -1;
}

ll query(int k, int l, int r, int p) {
	if (l == r) return mx[k];
	pushdown(k);
	return mid >= p ? query(ls, l, mid, p) : query(rs, mid + 1, r, p);
}

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
	int n = c.size(), q = l.size();
	for (int i = 0; i < q; i++) {
		Q[l[i]].emplace_back(i + 1, v[i]);
		Q[r[i] + 1].emplace_back(i + 1, -v[i]);
	}
	vector<int> ans;
	ll cur = 0;
	for (int i = 0; i < n; i++) {
		for (auto p : Q[i]) add(1, 0, q, p.first, q, p.second), cur += p.second;
		C = c[i];
		if (mx[1] - mn[1] < C) { ans.push_back(cur - mn[1]); continue; }
		ll cmn = LLONG_MAX, cmx = LLONG_MIN;
		int pos = find(1, 0, q, cmn, cmx);
		ll v = query(1, 0, q, pos);
		if (v == cmn) ans.push_back(cur + C - cmx);
		else ans.push_back(cur - cmn);
	}
	return ans;
}

Compilation message

candies.cpp: In function 'void add(int, int, int, int, int, ll)':
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:20:6: note: in expansion of macro 'mid'
   20 |  if (mid >= ql) add(ls, l, mid, ql, qr, v);
      |      ^~~
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:20:28: note: in expansion of macro 'mid'
   20 |  if (mid >= ql) add(ls, l, mid, ql, qr, v);
      |                            ^~~
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:21:6: note: in expansion of macro 'mid'
   21 |  if (mid < qr) add(rs, mid + 1, r, ql, qr, v);
      |      ^~~
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:21:24: note: in expansion of macro 'mid'
   21 |  if (mid < qr) add(rs, mid + 1, r, ql, qr, v);
      |                        ^~~
candies.cpp: In function 'int find(int, int, int, ll&, ll&)':
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:28:20: note: in expansion of macro 'mid'
   28 |   int t = find(rs, mid + 1, r, cmn, cmx);
      |                    ^~~
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:29:31: note: in expansion of macro 'mid'
   29 |   return ~t ? t : find(ls, l, mid, cmn, cmx);
      |                               ^~~
candies.cpp: In function 'll query(int, int, int, int)':
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:39:9: note: in expansion of macro 'mid'
   39 |  return mid >= p ? query(ls, l, mid, p) : query(rs, mid + 1, r, p);
      |         ^~~
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:39:33: note: in expansion of macro 'mid'
   39 |  return mid >= p ? query(ls, l, mid, p) : query(rs, mid + 1, r, p);
      |                                 ^~~
candies.cpp:11:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 | #define mid (l + r >> 1)
      |              ~~^~~
candies.cpp:39:53: note: in expansion of macro 'mid'
   39 |  return mid >= p ? query(ls, l, mid, p) : query(rs, mid + 1, r, p);
      |                                                     ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5136 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 4 ms 5188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 362 ms 31908 KB Output is correct
2 Correct 379 ms 35744 KB Output is correct
3 Correct 352 ms 35612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 186 ms 26908 KB Output is correct
3 Correct 72 ms 9048 KB Output is correct
4 Correct 374 ms 32192 KB Output is correct
5 Correct 352 ms 38056 KB Output is correct
6 Correct 355 ms 38424 KB Output is correct
7 Correct 352 ms 37812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 106 ms 25492 KB Output is correct
4 Correct 64 ms 8116 KB Output is correct
5 Correct 180 ms 28712 KB Output is correct
6 Correct 190 ms 32740 KB Output is correct
7 Correct 207 ms 33276 KB Output is correct
8 Correct 173 ms 31864 KB Output is correct
9 Correct 191 ms 33452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5136 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 4 ms 5188 KB Output is correct
6 Correct 362 ms 31908 KB Output is correct
7 Correct 379 ms 35744 KB Output is correct
8 Correct 352 ms 35612 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 186 ms 26908 KB Output is correct
11 Correct 72 ms 9048 KB Output is correct
12 Correct 374 ms 32192 KB Output is correct
13 Correct 352 ms 38056 KB Output is correct
14 Correct 355 ms 38424 KB Output is correct
15 Correct 352 ms 37812 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 106 ms 25492 KB Output is correct
19 Correct 64 ms 8116 KB Output is correct
20 Correct 180 ms 28712 KB Output is correct
21 Correct 190 ms 32740 KB Output is correct
22 Correct 207 ms 33276 KB Output is correct
23 Correct 173 ms 31864 KB Output is correct
24 Correct 191 ms 33452 KB Output is correct
25 Correct 3 ms 4940 KB Output is correct
26 Correct 61 ms 9208 KB Output is correct
27 Correct 192 ms 29228 KB Output is correct
28 Correct 328 ms 36224 KB Output is correct
29 Correct 343 ms 36720 KB Output is correct
30 Correct 363 ms 36864 KB Output is correct
31 Correct 364 ms 37068 KB Output is correct
32 Correct 354 ms 37436 KB Output is correct