답안 #1046460

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046460 2024-08-06T15:08:06 Z ymm 사탕 분배 (IOI21_candies) C++17
100 / 100
1387 ms 9052 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("avx")

#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);
      |  ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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
# 결과 실행 시간 메모리 Grader output
1 Correct 292 ms 8964 KB Output is correct
2 Correct 296 ms 9044 KB Output is correct
3 Correct 313 ms 9044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 56 ms 5112 KB Output is correct
3 Correct 31 ms 5300 KB Output is correct
4 Correct 662 ms 8960 KB Output is correct
5 Correct 669 ms 9044 KB Output is correct
6 Correct 665 ms 9040 KB Output is correct
7 Correct 697 ms 8860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 38 ms 4956 KB Output is correct
4 Correct 37 ms 4420 KB Output is correct
5 Correct 1347 ms 9044 KB Output is correct
6 Correct 1362 ms 8808 KB Output is correct
7 Correct 1374 ms 9040 KB Output is correct
8 Correct 1387 ms 8900 KB Output is correct
9 Correct 167 ms 9048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 292 ms 8964 KB Output is correct
7 Correct 296 ms 9044 KB Output is correct
8 Correct 313 ms 9044 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 56 ms 5112 KB Output is correct
11 Correct 31 ms 5300 KB Output is correct
12 Correct 662 ms 8960 KB Output is correct
13 Correct 669 ms 9044 KB Output is correct
14 Correct 665 ms 9040 KB Output is correct
15 Correct 697 ms 8860 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 38 ms 4956 KB Output is correct
19 Correct 37 ms 4420 KB Output is correct
20 Correct 1347 ms 9044 KB Output is correct
21 Correct 1362 ms 8808 KB Output is correct
22 Correct 1374 ms 9040 KB Output is correct
23 Correct 1387 ms 8900 KB Output is correct
24 Correct 167 ms 9048 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 32 ms 4320 KB Output is correct
27 Correct 43 ms 5112 KB Output is correct
28 Correct 661 ms 9044 KB Output is correct
29 Correct 657 ms 9044 KB Output is correct
30 Correct 665 ms 8964 KB Output is correct
31 Correct 676 ms 9052 KB Output is correct
32 Correct 661 ms 9040 KB Output is correct