結果

問題 No.953 席
ユーザー ysuzuki5321ysuzuki5321
提出日時 2019-12-18 10:30:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 16,350 bytes
コンパイル時間 1,997 ms
コンパイル使用メモリ 150,384 KB
実行使用メモリ 26,960 KB
最終ジャッジ日時 2024-07-05 23:38:22
合計ジャッジ時間 9,979 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,600 KB
testcase_01 AC 4 ms
9,468 KB
testcase_02 AC 252 ms
13,524 KB
testcase_03 AC 249 ms
12,584 KB
testcase_04 AC 252 ms
12,724 KB
testcase_05 AC 247 ms
15,628 KB
testcase_06 AC 253 ms
13,524 KB
testcase_07 AC 184 ms
26,960 KB
testcase_08 AC 272 ms
21,316 KB
testcase_09 AC 145 ms
20,360 KB
testcase_10 AC 61 ms
14,212 KB
testcase_11 AC 203 ms
18,840 KB
testcase_12 AC 273 ms
17,400 KB
testcase_13 AC 283 ms
19,404 KB
testcase_14 AC 266 ms
16,680 KB
testcase_15 AC 258 ms
13,992 KB
testcase_16 AC 269 ms
16,112 KB
testcase_17 AC 271 ms
16,992 KB
testcase_18 AC 277 ms
15,960 KB
testcase_19 AC 278 ms
15,384 KB
testcase_20 AC 269 ms
18,032 KB
testcase_21 AC 255 ms
13,328 KB
testcase_22 AC 275 ms
15,140 KB
testcase_23 AC 261 ms
13,956 KB
testcase_24 AC 257 ms
13,824 KB
testcase_25 AC 276 ms
19,484 KB
testcase_26 AC 236 ms
12,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
#include <ctype.h>
#include <unordered_map>
#include <random>
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, char> pic;
typedef priority_queue<pll, vector<pll>, greater<pll>> pllgreaterq;
typedef priority_queue<pair<ll,pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> plpllgreaterq;
#define bit(x,v) ((ll)x << v)

const ll INF = 1000000007;
const int MAX = 310000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[200010];
int lank[200010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

int same(int x, int y) {
	return parent(x) == parent(y);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (lank[px] < lank[py]) {
		pr[py] = px;
		lank[px] += lank[py] + 1;
	}
	else {
		pr[px] = py;
		lank[py] += lank[px] + 1;
	}
	return true;
}

ll bit[200010];
int max_n = 200000;
int pm = 0;
void add(int x) {

	while (max_n >= x)
	{
		bit[x]++;
		x += x & -x;
	}
}
void sub(int x) {
	while (max_n >= x)
	{
		bit[x]--;
		x += x & -x;
	}
}
ll merge(ll* a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<int> L(n1 + 1);
	vector<int> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = INF;
	R[n2] = INF;
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i] <= R[j]) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll merge2(pair<int, char>* a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<pair<int, char>> L(n1 + 1);
	vector<pair<int, char>> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = make_pair(INF, ' ');
	R[n2] = make_pair(INF, ' ');
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i].first <= R[j].first) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll mergeSort2(pair<int, char>* a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort2(a, left, mid);
		res += mergeSort2(a, mid, right);
		res += merge2(a, left, mid, right);
	}
	return res;
}
ll mergeSort(ll* a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort(a, left, mid);
		res += mergeSort(a, mid, right);
		res += merge(a, left, mid, right);
	}
	return res;
}
int partition(pair<int, char>* a, int p, int r) {
	pair<int, char> x = a[r];
	int i = p - 1;
	for (size_t j = p; j < r; j++)
	{
		if (a[j].first <= x.first) {
			i++;
			swap(a[i], a[j]);
		}
	}
	swap(a[i + 1], a[r]);
	return i + 1;
}
void quick(pair<int, char>* a, int p, int r) {
	if (p < r) {
		int q = partition(a, p, r);
		quick(a, p, q - 1);
		quick(a, q + 1, r);
	}
}

ll n;
int ci = 0;
ll P[1000010];
struct Node {
	int key;
	int priority;
	Node* parent, * left, * right;
	Node(int key, int priority);
	Node() {}
};
Node NIL;
Node::Node(int key, int priority) : key(key), priority(priority) {
	left = &NIL;
	right = &NIL;
}
Node* root = new Node();
void cenrec(Node* k) {
	if (k->key == NIL.key) return;
	cenrec(k->left);
	cout << " " << k->key;
	cenrec(k->right);
}
void fastrec(Node* k)
{
	if (k->key == NIL.key) return;
	cout << " " << k->key;
	fastrec(k->left);
	fastrec(k->right);
}
void insert(Node* v) {
	Node* y = &NIL;
	Node* x = root;
	while (x->key != NIL.key)
	{
		y = x;
		if (v->key < x->key) {
			x = x->left;
		}
		else {
			x = x->right;
		}
	}
	v->parent = y;
	if (y->key == NIL.key) {
		root = v;
	}
	else if (v->key < y->key) {
		y->left = v;
	}
	else {
		y->right = v;
	}

}

Node* find(Node* k, ll v)
{
	if (k->key == NIL.key) return &NIL;
	if (k->key == v) return k;
	if (v < k->key) return find(k->left, v);
	return find(k->right, v);
}
void delp12(Node* x) {
	if (x->key == NIL.key)  return;
	Node* l = x->left;
	Node* r = x->right;
	Node* pr = x->parent;

	if (l->key == NIL.key
		&& r->key == NIL.key) {
		if (pr->left == x) {
			pr->left = &NIL;
		}
		else pr->right = &NIL;
	}
	else if (l->key != NIL.key) {
		if (pr->left == x) {
			pr->left = l;
		}
		else pr->right = l;
		l->parent = pr;
	}
	else if (r->key != NIL.key) {
		if (pr->left == x) {
			pr->left = r;
		}
		else pr->right = r;
		r->parent = pr;
	}
}
Node* get_next(Node* k) {
	if (k->key == NIL.key) return &NIL;
	Node* res = get_next(k->left);
	if (res->key != NIL.key) return res;
	return k;
}
void del(Node* x) {

	if (x->key == NIL.key) return;
	Node* l = x->left;
	Node* r = x->right;
	Node* pr = x->parent;

	if (l->key != NIL.key && r->key != NIL.key) {
		Node* nex = get_next(r);
		x->key = nex->key;
		delp12(nex);
	}
	else {
		delp12(x);
	}
}
Node* rightRotate(Node* t) {
	Node* s = t->left;
	t->left = s->right;
	s->right = t;
	return s;
}
Node* leftRotate(Node* t) {
	Node* s = t->right;
	t->right = s->left;
	s->left = t;
	return s;
}
Node* _insert(Node* t, int key, int priority) {
	if (t->key == NIL.key) {
		return new Node(key, priority);
	}
	if (key == t->key) {
		return t;
	}

	if (key < t->key) {
		t->left = _insert(t->left, key, priority);
		if (t->priority < t->left->priority) {
			t = rightRotate(t);
		}
	}
	else {
		t->right = _insert(t->right, key, priority);
		if (t->priority < t->right->priority) {
			t = leftRotate(t);
		}
	}
	return t;
}
Node* delete1(Node* t, int key);
Node* _delete(Node* t, int key) {
	if (t->left->key == NIL.key && t->right->key == NIL.key) {
		return &NIL;
	}
	else if (t->left->key == NIL.key) {
		t = leftRotate(t);
	}
	else if (t->right->key == NIL.key) {
		t = rightRotate(t);
	}
	else
	{
		if (t->left->priority > t->right->priority) {
			t = rightRotate(t);
		}
		else
			t = leftRotate(t);
	}
	return delete1(t, key);
}
Node* delete1(Node* t, int key) {
	if (t->key == NIL.key) {
		return &NIL;
	}
	if (key < t->key) {
		t->left = delete1(t->left, key);
	}
	else if (key > t->key) {
		t->right = delete1(t->right, key);
	}
	else return _delete(t, key);
	return t;
}
int H;
int left(int i) {
	return i * 2 + 1;
}
int right(int i) {
	return i * 2 + 2;
}

ll heap[2000010];
void maxHeapify(int i) {
	int l = left(i);
	int r = right(i);
	int largest = 0;
	if (l < H && heap[l] > heap[i])
		largest = l;
	else
		largest = i;
	if (r < H && heap[r] > heap[largest])
		largest = r;

	if (largest != i) {
		swap(heap[i], heap[largest]);
		maxHeapify(largest);
	}
}
int pare(int i) {
	return (i - 1) / 2;
}
void raise(int i) {
	int l = pare(i);
	if (l < 0) return;

	if (heap[l] < heap[i]) {
		swap(heap[i], heap[l]);
		raise(l);
	}
}
void minHeapify(int i) {
	int l = left(i);
	int r = right(i);
	int minimam = 0;
	if (l < H && heap[l] < heap[i])
		minimam = l;
	else
		minimam = i;
	if (r < H && heap[r] < heap[minimam])
		minimam = r;

	if (minimam != i) {
		swap(heap[i], heap[minimam]);
		minHeapify(minimam);
	}
}
void buildMaxHeap() {
	for (int i = H / 2; i >= 0; i--)
	{
		maxHeapify(i);
	}
}

int dx[] = { -1,0,1,0 };
int dy[] = { 0,-1,0,1 };
std::vector<int> find_all(const std::string str, const std::string subStr) {
	std::vector<int> result;

	int subStrSize = subStr.size();
	int pos = str.find(subStr);

	while (pos != std::string::npos) {
		result.push_back(pos);
		pos = str.find(subStr, pos + 1);
	}

	return result;
}
//ll memo[100010];
//ll next[100010];
//ll dm[100010];
//int f[100010];
//ll rec(int x) {
//
//	if (~memo[x]) return memo[x];
//	if (x == n) { 
//		dm[n] = 1;
//		return 1; 
//	}
//	ll *res = &memo[x];
//	*res = 0;
//	set<int> st;
//	st.insert(f[x]);
//	for (int i = x + 1; i <= n; i++)
//	{
//		if (~memo[i]) {
//			*res += memo[i] + 1;
//			*res %= INF;
//			break;
//		}
//
//		*res += rec(i);
//		*res %= INF;
//		if (st.find(f[i]) != st.end()) {break; }
//		st.insert(f[i]);
//	}
//
//	return *res;
//}
#define bit(x,v) ((ll)x << v)

class BIT {

	static const int MAX_N = 200010;
public:
	BIT() { memset(bit, 0, sizeof(bit)); }
	int bit[MAX_N + 1], n;
	int sum(int i) {
		int s = 0;
		while (i > 0)
		{
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}

	void add(int i, int x) {
		while (i <= n)
		{
			bit[i] += x;
			i += i & -i;
		}
	}
	void clear() {
		memset(bit, 0, sizeof(bit));
	}
	int a[MAX_N];
	void bable_swap_count() {
		ll ans = 0;
		for (size_t j = 0; j < n; j++)
		{
			ans += j - sum(a[j]);
			add(a[j], 1);
		}
		printf("%lld\n", ans);
	}

	int search(int s, int x) {

		ll half = (s + x) / 2;
		ll sh = sum(x);

		ll sl = sum(half);

		ll st = sum(s);
		if (sh - sl == 0) {
			return x;
		}
		if (sh - sl < x - half) {
			return search(half, x);
		}

		if (sl - st == 0) {
			return half;
		}

		if (sl - st < half - s) {
			return search(s, half);
		}
		return -1;
	}

	int lankSearch(int lank) {
		return lankSearch(lank, 0, MAX_N);
	}
	int lankSearch(int lank,int s,int t) {

		ll half = (s + t) / 2;
		
		ll v = sum(half);
		ll v1 = sum(t);
		ll v2 = sum(s);
		if (lank == 1) {
			if (s + 1 >= t) return t;
			else if (v - v2 > 0) {
				return lankSearch(lank, s, half);
			}
			else return lankSearch(lank, half, t);
		}

		if ((v - v2) < lank) {
			return lankSearch(lank - (v- v2),half,t);
		}
		if ((v - v2) >= lank) {
			return lankSearch(lank, s, half);
		}
			
		return -1;
	}
};

bool ch(vector<string> tb, int x, int y) {
	try {

	for (int i = 0; i < 8; i++)
	{
		if (tb[x][i] == 'Q') return false;
		if (tb[i][y] == 'Q') return false;
	}
	int ty = y;
	for (int i = x; i < 8; i++)
	{
		if (tb[i][ty] == 'Q') return false;
		ty++;
		if (ty == 8) break;
	}

	ty = y;
	for (int i = x; i < 8; i++)
	{
		if (tb[i][ty] == 'Q') return false;
		ty--;
		if (ty == -1) break;
	}
	ty = y;
	for (int i = x; i >= 0; i--)
	{
		if (tb[i][ty] == 'Q') return false;
		ty++;
		if (ty == 8) break;
	}
	ty = y;
	for (int i = x; i >= 0; i--)
	{
		if (tb[i][ty] == 'Q') return false;
		ty--;
		if (ty == -1) break;
	}
	}
	catch (int v) {
		cout << endl;
	}

	return true;
}
bool use[8];
vector<string> constra(vector<string> tb, vector<int> p) {

	vector<string> r(tb.begin(),tb.end());
	int pi = 0;
	for (size_t i = 0; i < 8; i++)
	{
		if (use[i]) continue;
		if (!ch(r, i, p[pi])) return {};
		r[i][p[pi]] = 'Q';
		pi++;
	}
	return r;
}
string tb[510];
int h, w;
bool fil(int x, int y) {

	queue<pii> q;
	q.push(make_pair(x, y));
	while (!q.empty())
	{
		auto p = q.front();
		q.pop();
		for (size_t i = 0; i < 4; i++)
		{
			int tx = p.first + dx[i];
			int ty = p.second + dy[i];
			if (tx < 0 || ty < 0 || tx >= h || ty >= w) {
				continue;
			}
			if (tb[tx][ty] == '0') continue;
			if (tb[tx][ty] == '1') continue;
			if (tb[tx][ty] == '#') {
				tb[tx][ty] = '1';
				continue;
			}
			tb[tx][ty] = '0';
			q.push(make_pair(tx, ty));
		}
	}
	return false;
}

vector<pii> es[110];
ll p(ll i, ll j, ll l) {
	return (i / 2) * 2 + (j / 2) *2 + (l / 2) * 2;
}
ll cal1(ll i, ll j, ll l) {
	ll res = 0;
	if (i > 0 && j > 0 && l > 0) {
		i--;
		j--;
		l--;
		res += 3;
	}
	return res + p(i,j,l);
}
ll cal2(ll i, ll j, ll l) {
	ll res = 0;
	if (i > 1 && j > 1 && l > 1) {
		i -= 2;
		j -= 2;
		l -= 2;
		res += 6;
	}
	return res + p(i, j, l);

}
ll m;
map<pair<string, string>, ll> createmp(string s) {
	map<pair<string, string>, ll> m1;
	for (size_t i = 0; i < (1 << n); i++)
	{
		int ti = i;
		int p[20];
		memset(p, 0, sizeof(p));
		int v = 0;
		while (ti > 0)
		{
			if (ti & 1) {
				p[v] = true;

			}
			ti >>= 1;
			v++;
		}
		string s1 = "";
		string s2 = "";
		for (size_t i = 0; i < n; i++)
		{
			if (p[i]) {
				s1 += s[i];
			}
			else {
				s2 += s[i];
			}
		}
		m1[make_pair(s1, s2)]++;
	}

	return m1;
}
ll sp[100010];
bool seat[100010];

bool ic(int x) {
	if (x == n) {
		if (seat[x - 1] == false) {
			return true;
		}
	}
	if (x == 1) {
		if (seat[x + 1] == false)return true;
	}
	if (!seat[x + 1] && !seat[x - 1])return true;
	return false;
}

void solv() {

	ll k1, k2;
	cin >> n >> k1 >> k2;
	ll q;
	cin >> q;
	ll a[100010], b[100010];
	for (size_t i = 0; i < q; i++)
	{
		cin >> a[i] >> b[i];
	}

	// 帰る時間のpqueue
	// 席のpqueue
	ll k1t = k1;
	ll k2t = k2;
	
	for (size_t i = 0; i < n; i++)
	{
		if (i % 2 == 0) {
			if (k1 > k2) {
				if (k1t <= n) {
					sp[k1t] = i;
					k1t++;
				}
				else {
					sp[k2t] = i;
					k2t--;
				}
			}
			else {
				if (k1t > 0) {
					sp[k1t] = i;
					k1t--;
				}
				else {
					sp[k2t] = i;
					k2t++;
				}
			}
		}
		else {
			if (k1 > k2) {
				if (k2t > 0) {
					sp[k2t] = i;
					k2t--;
				}
				else {
					sp[k1t] = i;
					k1t++;
				}
			}
			else {
				if (k2t <= n) {
					sp[k2t] = i;
					k2t++;
				}
				else {
					sp[k1t] = i;
					k1t--;
				}
			}
		}
	}

	priority_queue < pair<int, pll>, vector< pair<int, pll>>, greater< pair<int, pll>>> sq;

	for (size_t i = 1; i <= n; i++)
	{
		if (k1 % 2 == 1) {
			if (i % 2 == 1) {
				sq.push(make_pair(0, make_pair(sp[i],i)));
			}
			else {
				sq.push(make_pair(1, make_pair(sp[i], i)));
			}
		}
		else {
			if (i % 2 == 0) {
				sq.push(make_pair(0, make_pair(sp[i], i)));
			}
			else {
				sq.push(make_pair(1, make_pair(sp[i], i)));
			}
		}
	}
	// イベントのqueue,時間,席を立つか(席に座る人がいる場合は座るように処理)来店するか,誰が(iが分かっていればb[i]は拾える)
	priority_queue <pair<ll, pll>, vector< pair<ll, pll>>, greater< pair<ll, pll>>> ev;
	for (size_t i = 0; i < q; i++)
	{
		ev.push(make_pair(a[i], make_pair(1, i)));
	}

	// 席に座る処理がややこしい
	ll t = 0;
	memset(seat, 0, sizeof(seat));
	queue<int> wa; // 待ってる人のqueue
	int res[100010];
	while (!ev.empty())
	{
		auto p = ev.top();
		ll tim = p.first;
		int type = p.second.first;
		int wh = p.second.second;
		ev.pop();
		if (type == 1) {
			if (sq.empty()) // 席が空だった場合はwait
			{
				wa.push(p.second.second);
				continue;
			}
			bool ok = false;
			while (!sq.empty())
			{
				auto su = sq.top();
				sq.pop();
				int st = su.second.second;
				if (seat[st]) continue;
				//優先席じゃなくなっている
				if (su.first == 0 && !ic(st)) continue;
				seat[st] = true;
				ev.push(make_pair(tim + b[wh], make_pair(0,wh)));
				res[wh] = st;
				ok = true;
				break;
			}

			if (!ok) {
				wa.push(p.second.second);
				continue;
			}
		}
		else { // 席を立つ場合	
			seat[res[wh]] = false;
			// 座っていた席とその隣の席の状況を調べる
			int p1 = res[wh];
			if (ic(p1)) {
				sq.push(make_pair(0, make_pair(sp[p1], p1)));
				sq.push(make_pair(1, make_pair(sp[p1], p1)));
			}
			else {
				sq.push(make_pair(1, make_pair(sp[p1], p1)));
			}
			int p2 = res[wh] - 1;
			if (p2 != 0 && !seat[p2])
				if (ic(p2)) {
					sq.push(make_pair(0, make_pair(sp[p2], p2)));
					sq.push(make_pair(1, make_pair(sp[p2], p2)));
				}
				else {
					sq.push(make_pair(1, make_pair(sp[p2], p2)));
				}
			int p3 = res[wh] + 1;
			if (p3 != n + 1 && !seat[p3])
				if (ic(p3)) {
					sq.push(make_pair(0, make_pair(sp[p3], p3)));
					sq.push(make_pair(1, make_pair(sp[p3], p3)));
				}
				else {
					sq.push(make_pair(1, make_pair(sp[p3], p3)));
				}

			if (wa.empty()) continue;
			//待っている人の席取り
			int wai = wa.front();
			wa.pop();
			ev.push(make_pair(tim, make_pair(1, wai)));
		}
	}


	for (size_t i = 0; i < q; i++)
	{
		cout << res[i] << endl;
	}
}
int main() {
	//COMinit();
	solv();
	return 0;
}
0