답안 #1046457

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046457 2024-08-06T15:06:01 Z ymm 사탕 분배 (IOI21_candies) C++17
100 / 100
1321 ms 15700 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 = 4;

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;
	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 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 324 ms 11580 KB Output is correct
2 Correct 307 ms 11348 KB Output is correct
3 Correct 313 ms 11348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 48 ms 6964 KB Output is correct
3 Correct 32 ms 6724 KB Output is correct
4 Correct 675 ms 12156 KB Output is correct
5 Correct 693 ms 15188 KB Output is correct
6 Correct 673 ms 15700 KB Output is correct
7 Correct 720 ms 15080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 44 ms 6652 KB Output is correct
4 Correct 36 ms 5264 KB Output is correct
5 Correct 1321 ms 11032 KB Output is correct
6 Correct 1301 ms 13236 KB Output is correct
7 Correct 1286 ms 13756 KB Output is correct
8 Correct 1288 ms 12628 KB Output is correct
9 Correct 171 ms 13704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 324 ms 11580 KB Output is correct
7 Correct 307 ms 11348 KB Output is correct
8 Correct 313 ms 11348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 48 ms 6964 KB Output is correct
11 Correct 32 ms 6724 KB Output is correct
12 Correct 675 ms 12156 KB Output is correct
13 Correct 693 ms 15188 KB Output is correct
14 Correct 673 ms 15700 KB Output is correct
15 Correct 720 ms 15080 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 44 ms 6652 KB Output is correct
19 Correct 36 ms 5264 KB Output is correct
20 Correct 1321 ms 11032 KB Output is correct
21 Correct 1301 ms 13236 KB Output is correct
22 Correct 1286 ms 13756 KB Output is correct
23 Correct 1288 ms 12628 KB Output is correct
24 Correct 171 ms 13704 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 35 ms 5584 KB Output is correct
27 Correct 55 ms 7760 KB Output is correct
28 Correct 661 ms 13396 KB Output is correct
29 Correct 660 ms 13908 KB Output is correct
30 Correct 711 ms 14160 KB Output is correct
31 Correct 668 ms 14216 KB Output is correct
32 Correct 671 ms 14516 KB Output is correct