Submission #1046458

# Submission time Handle Problem Language Result Execution time Memory
1046458 2024-08-06T15:07:34 Z ymm Distributing Candies (IOI21_candies) C++17
100 / 100
1100 ms 9048 KB
#include "candies.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
typedef long long ll;
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")

#define INLINE __attribute__((always_inline)) constexpr
#define ASSUME(expr) __attribute__((assume(expr)))
#define MAX(x, y) ((x)>(y)?(x):(y))
#define MIN(x, y) ((x)<(y)?(x):(y))

INLINE int uppos(int x, int u) { return x; }
INLINE int upneg(int x, int u) { return x; }
template<class... T> INLINE int uppos(int x, int u, int v, T... vs);
template<class... T> INLINE int upneg(int x, int u, int v, T... vs);

template<class... T>
INLINE int uppos(int x, int u, int v, T... vs)
{
	ASSUME(0 <= x && x <= u && v > 0);
	return upneg(MIN(x + v, u), u, vs...);
}

template<class... T>
INLINE int upneg(int x, int u, int v, T... vs)
{
	ASSUME(0 <= x && x <= u && v > 0);
	return uppos(MAX(x - v, 0), u, vs...);
}

const int N = 200'010;
const int S = 1024;
int a[N], b[N];

template<bool start_neg, class... T>
void uprange(int l, int r, T... v)
{
	if constexpr(start_neg) {
		Loop (i,l,r)
			a[i] = upneg(a[i], b[i], v...);
	} else {
		Loop (i,l,r)
			a[i] = uppos(a[i], b[i], v...);
	}
}

const int M = 8;

template<bool start_neg>
void up(int l, int r, vector<int> vec)
{
	switch (vec.size()) {
	case 0: break;
	case 1: uprange<start_neg>(l, r, vec[0]); break;
	case 2: uprange<start_neg>(l, r, vec[0], vec[1]); break;
	case 3: uprange<start_neg>(l, r, vec[0], vec[1], vec[2]); break;
	case 4: uprange<start_neg>(l, r, vec[0], vec[1], vec[2], vec[3]); break;
	case 5: uprange<start_neg>(l, r, vec[0], vec[1], vec[2], vec[3], vec[4]); break;
	case 6: uprange<start_neg>(l, r, vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]); break;
	case 7: uprange<start_neg>(l, r, vec[0], vec[1], vec[2], vec[3], vec[4], vec[5], vec[6]); break;
	case 8: uprange<start_neg>(l, r, vec[0], vec[1], vec[2], vec[3], vec[4], vec[5], vec[6], vec[7]); break;
	default: assert(false);
	}
}

const int inf = 1e9 + 10;

std::vector<int> distribute_candies(std::vector<int> _c, std::vector<int> ql,
                                    std::vector<int> qr, std::vector<int> qv) {
	int n = _c.size();
	int q = ql.size();
	for (int &x : qr)
		++x;
	Loop (i,0,n)
		b[i] = _c[i];
	for (int L = 0; L < n; L += S) {
		int R = min(L + S, n);
		vector<int> vec;
		bool st = 0, ls = 0;
		auto flush = [&]() {
			if (vec.empty())
				return;
			(st? up<true>: up<false>)(L, R, vec);
			vec.clear();
		};
		Loop (i,0,q) {
			int l = MAX(ql[i], L);
			int r = MIN(qr[i], R);
			if (l >= r)
				continue;
			if (L == l && R == r) {
				if (!vec.size()) {
					vec.push_back(abs(qv[i]));
					st = ls = qv[i] < 0;
				} else if (ls == (qv[i] < 0)) {
					vec.back() = min(inf, vec.back() + abs(qv[i]));
				} else {
					vec.push_back(abs(qv[i]));
					ls = !ls;
				}
			} else {
				flush();
				(qv[i] < 0? uprange<true, int>: uprange<false, int>)(l, r, abs(qv[i]));
			}
			if (vec.size() == M)
				flush();
		}
		flush();
	}
	return vector<int>(a, a+n);
}

Compilation message

candies.cpp: In function 'constexpr int uppos(int, int, int, T ...)':
candies.cpp:11:22: warning: attributes at the beginning of statement are ignored [-Wattributes]
   11 | #define ASSUME(expr) __attribute__((assume(expr)))
      |                      ^~~~~~~~~~~~~
candies.cpp:23:2: note: in expansion of macro 'ASSUME'
   23 |  ASSUME(0 <= x && x <= u && v > 0);
      |  ^~~~~~
candies.cpp: In function 'constexpr int upneg(int, int, int, T ...)':
candies.cpp:11:22: warning: attributes at the beginning of statement are ignored [-Wattributes]
   11 | #define ASSUME(expr) __attribute__((assume(expr)))
      |                      ^~~~~~~~~~~~~
candies.cpp:30:2: note: in expansion of macro 'ASSUME'
   30 |  ASSUME(0 <= x && x <= u && v > 0);
      |  ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 318 ms 8892 KB Output is correct
2 Correct 309 ms 9044 KB Output is correct
3 Correct 357 ms 8948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 49 ms 5172 KB Output is correct
3 Correct 31 ms 5452 KB Output is correct
4 Correct 640 ms 8912 KB Output is correct
5 Correct 587 ms 9044 KB Output is correct
6 Correct 585 ms 9044 KB Output is correct
7 Correct 589 ms 8872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 37 ms 4956 KB Output is correct
4 Correct 33 ms 4496 KB Output is correct
5 Correct 1089 ms 8936 KB Output is correct
6 Correct 1056 ms 9044 KB Output is correct
7 Correct 1083 ms 9040 KB Output is correct
8 Correct 1100 ms 8920 KB Output is correct
9 Correct 176 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 318 ms 8892 KB Output is correct
7 Correct 309 ms 9044 KB Output is correct
8 Correct 357 ms 8948 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 49 ms 5172 KB Output is correct
11 Correct 31 ms 5452 KB Output is correct
12 Correct 640 ms 8912 KB Output is correct
13 Correct 587 ms 9044 KB Output is correct
14 Correct 585 ms 9044 KB Output is correct
15 Correct 589 ms 8872 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 37 ms 4956 KB Output is correct
19 Correct 33 ms 4496 KB Output is correct
20 Correct 1089 ms 8936 KB Output is correct
21 Correct 1056 ms 9044 KB Output is correct
22 Correct 1083 ms 9040 KB Output is correct
23 Correct 1100 ms 8920 KB Output is correct
24 Correct 176 ms 9048 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 29 ms 4212 KB Output is correct
27 Correct 47 ms 4952 KB Output is correct
28 Correct 611 ms 8968 KB Output is correct
29 Correct 603 ms 8828 KB Output is correct
30 Correct 621 ms 8984 KB Output is correct
31 Correct 620 ms 8920 KB Output is correct
32 Correct 585 ms 8832 KB Output is correct