답안 #551584

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551584 2022-04-21T05:49:36 Z skittles1412 Ants and Sugar (JOI22_sugar) C++17
22 / 100
3421 ms 1048576 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

struct Node {
	array<array<array<long, 2>, 2>, 3> v;

	friend Node operator+(const Node& x, const Node& y) {
		Node ans = inf();
		for (int a = 0; a < 3; a++) {
			for (int b = 0; b < 2; b++) {
				for (int c = 0; c < 2; c++) {
					for (int d = 0; d < 3; d++) {
						for (int e = 0; e < 2; e++) {
							auto& cur = ans.v[min(a + d, 2)][b][e];
							cur = max(cur, x.v[a][b][c] + y.v[d][c][e]);
						}
					}
				}
			}
		}
		return ans;
	}

	static Node inf() {
		Node ans {};
		for (auto& a : ans.v) {
			for (auto& b : a) {
				for (auto& c : b) {
					c = -1e18;
				}
			}
		}
		return ans;
	}

	static Node def() {
		Node ans = inf();
		ans.v[0][0][0] = ans.v[0][1][1] = ans.v[0][0][1] = ans.v[1][1][0] = 0;
		return ans;
	}
};

struct Lazy {
	long ax, bx;

	Lazy& operator+=(const Lazy& x) {
		ax += x.ax;
		bx += x.bx;
		return *this;
	}

	friend Lazy operator+(Lazy a, const Lazy& b) {
		return a += b;
	}

	Node apply(Node n) const {
		for (int i = 0; i < 3; i++) {
			for (auto& a : n.v[i]) {
				for (auto& b : a) {
					b += bx * i;
				}
			}
			n.v[i][0][1] -= ax;
			n.v[i][1][0] += ax;
		}
		return n;
	}

	static Lazy identity() {
		return {0, 0};
	}
};

const int maxn = 1 << 19, tmaxn = maxn << 1;

Node v[tmaxn];
Lazy lazy[tmaxn];

void update(int o, int l, int r, int ql, int qr, const Lazy& x) {
	int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
	if (ql <= l && r <= qr) {
		lazy[o] += x;
	} else {
		if (ql <= mid) {
			update(lc, l, mid, ql, qr, x);
		}
		if (mid < qr) {
			update(rc, mid + 1, r, ql, qr, x);
		}
	}
	if (l == r) {
		v[o] = lazy[o].apply(Node::def());
	} else {
		v[o] = lazy[o].apply(v[lc] + v[rc]);
	}
}

void update(int l, int r, const Lazy& x) {
	dbg(l, r, x.ax, x.bx);
	update(1, 0, maxn - 1, l, r, x);
}

void solve() {
	fill(begin(v), end(v), Node::def());
	int q, k;
	cin >> q >> k;
	int arr[q][3];
	for (auto& [a, b, c] : arr) {
		cin >> a >> b >> c;
	}
	vector<int> comp;
	for (auto& [t, ind, _x] : arr) {
		if (t == 1) {
			comp.push_back(ind);
		} else {
			comp.push_back(ind + k);
			comp.push_back(ind - k);
			comp.push_back(ind + k - 1);
		}
	}
	sort(begin(comp), end(comp));
	comp.erase(unique(begin(comp), end(comp)), comp.end());
	auto cg = [&](int x) -> int {
		return int(lower_bound(begin(comp), end(comp), x) - comp.begin());
	};
	long ants = 0;
	for (auto& [t, ind, x] : arr) {
		if (t == 1) {
			ants += x;
			update(cg(ind), maxn - 1, {x, 0});
		} else {
			update(cg(ind + k), maxn - 1, {-x, 0});
			update(cg(ind - k), cg(ind + k - 1), {0, -x});
		}
		auto& n = v[1];
		long ans = 0;
		for (auto& a : n.v) {
			ans = max({ans, a[0][0], a[1][0]});
		}
		cout << ants - ans << endl;
	}
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 98768 KB Output is correct
2 Correct 37 ms 98764 KB Output is correct
3 Correct 37 ms 98752 KB Output is correct
4 Correct 36 ms 98788 KB Output is correct
5 Correct 36 ms 98776 KB Output is correct
6 Correct 49 ms 98900 KB Output is correct
7 Correct 50 ms 98980 KB Output is correct
8 Correct 44 ms 99016 KB Output is correct
9 Correct 43 ms 98872 KB Output is correct
10 Correct 54 ms 99144 KB Output is correct
11 Correct 56 ms 99100 KB Output is correct
12 Correct 56 ms 99124 KB Output is correct
13 Correct 56 ms 99084 KB Output is correct
14 Correct 59 ms 99256 KB Output is correct
15 Correct 60 ms 99316 KB Output is correct
16 Correct 49 ms 99064 KB Output is correct
17 Correct 51 ms 99136 KB Output is correct
18 Correct 49 ms 99020 KB Output is correct
19 Correct 54 ms 99172 KB Output is correct
20 Correct 49 ms 99028 KB Output is correct
21 Correct 59 ms 99068 KB Output is correct
22 Correct 49 ms 98952 KB Output is correct
23 Correct 57 ms 99088 KB Output is correct
24 Correct 51 ms 98908 KB Output is correct
25 Correct 59 ms 99188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 98828 KB Output is correct
2 Correct 36 ms 98812 KB Output is correct
3 Correct 35 ms 98852 KB Output is correct
4 Correct 2442 ms 120352 KB Output is correct
5 Correct 1299 ms 111024 KB Output is correct
6 Correct 2869 ms 124284 KB Output is correct
7 Correct 1286 ms 115180 KB Output is correct
8 Correct 3393 ms 136476 KB Output is correct
9 Correct 2621 ms 133620 KB Output is correct
10 Correct 3421 ms 137504 KB Output is correct
11 Correct 2623 ms 131380 KB Output is correct
12 Correct 1237 ms 112692 KB Output is correct
13 Correct 1637 ms 117432 KB Output is correct
14 Correct 2629 ms 129612 KB Output is correct
15 Correct 2652 ms 129524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 98860 KB Output is correct
2 Correct 1240 ms 108744 KB Output is correct
3 Correct 1645 ms 112456 KB Output is correct
4 Correct 2598 ms 121744 KB Output is correct
5 Correct 2580 ms 129500 KB Output is correct
6 Correct 2723 ms 130092 KB Output is correct
7 Correct 237 ms 102396 KB Output is correct
8 Correct 1415 ms 116912 KB Output is correct
9 Correct 2123 ms 123872 KB Output is correct
10 Runtime error 1743 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 98768 KB Output is correct
2 Correct 37 ms 98764 KB Output is correct
3 Correct 37 ms 98752 KB Output is correct
4 Correct 36 ms 98788 KB Output is correct
5 Correct 36 ms 98776 KB Output is correct
6 Correct 49 ms 98900 KB Output is correct
7 Correct 50 ms 98980 KB Output is correct
8 Correct 44 ms 99016 KB Output is correct
9 Correct 43 ms 98872 KB Output is correct
10 Correct 54 ms 99144 KB Output is correct
11 Correct 56 ms 99100 KB Output is correct
12 Correct 56 ms 99124 KB Output is correct
13 Correct 56 ms 99084 KB Output is correct
14 Correct 59 ms 99256 KB Output is correct
15 Correct 60 ms 99316 KB Output is correct
16 Correct 49 ms 99064 KB Output is correct
17 Correct 51 ms 99136 KB Output is correct
18 Correct 49 ms 99020 KB Output is correct
19 Correct 54 ms 99172 KB Output is correct
20 Correct 49 ms 99028 KB Output is correct
21 Correct 59 ms 99068 KB Output is correct
22 Correct 49 ms 98952 KB Output is correct
23 Correct 57 ms 99088 KB Output is correct
24 Correct 51 ms 98908 KB Output is correct
25 Correct 59 ms 99188 KB Output is correct
26 Correct 36 ms 98828 KB Output is correct
27 Correct 36 ms 98812 KB Output is correct
28 Correct 35 ms 98852 KB Output is correct
29 Correct 2442 ms 120352 KB Output is correct
30 Correct 1299 ms 111024 KB Output is correct
31 Correct 2869 ms 124284 KB Output is correct
32 Correct 1286 ms 115180 KB Output is correct
33 Correct 3393 ms 136476 KB Output is correct
34 Correct 2621 ms 133620 KB Output is correct
35 Correct 3421 ms 137504 KB Output is correct
36 Correct 2623 ms 131380 KB Output is correct
37 Correct 1237 ms 112692 KB Output is correct
38 Correct 1637 ms 117432 KB Output is correct
39 Correct 2629 ms 129612 KB Output is correct
40 Correct 2652 ms 129524 KB Output is correct
41 Correct 38 ms 98860 KB Output is correct
42 Correct 1240 ms 108744 KB Output is correct
43 Correct 1645 ms 112456 KB Output is correct
44 Correct 2598 ms 121744 KB Output is correct
45 Correct 2580 ms 129500 KB Output is correct
46 Correct 2723 ms 130092 KB Output is correct
47 Correct 237 ms 102396 KB Output is correct
48 Correct 1415 ms 116912 KB Output is correct
49 Correct 2123 ms 123872 KB Output is correct
50 Runtime error 1743 ms 1048576 KB Execution killed with signal 9
51 Halted 0 ms 0 KB -