Submission #768482

#TimeUsernameProblemLanguageResultExecution timeMemory
768482SanguineChameleonSeats (IOI18_seats)C++17
100 / 100
2147 ms103232 KiB
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e6 + 20;
pair<int, int> tree[maxN * 4];
int lazy[maxN * 4];
int R[maxN];
int C[maxN];
vector<vector<int>> seats;
int N;

void build_tree(int id, int lt, int rt) {
	if (lt == rt) {
		tree[id] = make_pair(0, 1);
		return;
	}
	int mt = (lt + rt) >> 1;
	build_tree(id << 1, lt, mt);
	build_tree(id << 1 | 1, mt + 1, rt);
	tree[id] = make_pair(0, rt - lt + 1);
}

void update_tree(int id, int lt, int rt, int ql, int qr, int add) {
	if (lt == ql && rt == qr) {
		tree[id].first += add;
		lazy[id] += add;
		return;
	}
	tree[id << 1].first += lazy[id];
	lazy[id << 1] += lazy[id];
	tree[id << 1 | 1].first += lazy[id];
	lazy[id << 1 | 1] += lazy[id];
	lazy[id] = 0;
	int mt = (lt + rt) >> 1;
	if (qr <= mt) {
		update_tree(id << 1, lt, mt, ql, qr, add);
	}
	else if (ql >= mt + 1) {
		update_tree(id << 1 | 1, mt + 1, rt, ql, qr, add);
	}
	else {
		update_tree(id << 1, lt, mt, ql, mt, add);
		update_tree(id << 1 | 1, mt + 1, rt, mt + 1, qr, add);
	}
	tree[id].first = min(tree[id << 1].first, tree[id << 1 | 1].first);
	tree[id].second = (tree[id << 1].first == tree[id].first ? tree[id << 1].second : 0) + (tree[id << 1 | 1].first == tree[id].first ? tree[id << 1 | 1].second : 0);
}

void update_square(int i, int j, int add) {
	vector<int> vals = {seats[i][j], seats[i][j + 1], seats[i + 1][j], seats[i + 1][j + 1]};
	sort(vals.begin(), vals.end());
	if (vals[0] < vals[1]) {
		update_tree(1, 1, N, vals[0], vals[1] - 1, add);
	}
	if (vals[2] < vals[3]) {
		update_tree(1, 1, N, vals[2], vals[3] - 1, add);
	}
}

void update_pos(int i, int j, int val) {
	update_square(i - 1, j - 1, -1);
	update_square(i - 1, j, -1);
	update_square(i, j - 1, -1);
	update_square(i, j, -1);
	seats[i][j] = val;
	update_square(i - 1, j - 1, 1);
	update_square(i - 1, j, 1);
	update_square(i, j - 1, 1);
	update_square(i, j, 1);
}

void give_initial_chart(int H, int W, vector<int> _R, vector<int> _C) {
	H = H;
	W = W;
	N = H * W;
	seats.resize(H + 2, vector<int>(W + 2, N + 1));
	build_tree(1, 1, N);
	for (int i = 1; i <= N; i++) {
		R[i] = _R[i - 1] + 1;
		C[i] = _C[i - 1] + 1;
		seats[R[i]][C[i]] = i;
	}
	for (int i = 0; i <= H; i++) {
		for (int j = 0; j <= W; j++) {
			update_square(i, j, 1);
		}
	}
}

int swap_seats(int a, int b) {
	a++;
	b++;
	update_pos(R[a], C[a], b);
	update_pos(R[b], C[b], a);
	swap(R[a], R[b]);
	swap(C[a], C[b]);
	return tree[1].second;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...