제출 #544766

#제출 시각아이디문제언어결과실행 시간메모리
544766rainboyQueue (CEOI06_queue)C11
100 / 100
75 ms5460 KiB
#include <stdio.h>

#define N	50000
#define N_	(N * 3 + 1)

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int aa[N_];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (aa[ii[j]] == aa[i_])
				j++;
			else if (aa[ii[j]] < aa[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int main() {
	static int ii[N_], aa_[N_], ll[N_], rr[N_], pp[N_], qu[N_];
	int n, n_, q, i, p, cnt;

	scanf("%d", &n);
	aa[0] = 1;
	for (i = 0; i < n; i++) {
		int a, b;

		scanf("%d%d", &a, &b);
		aa[i * 3 + 1] = a, aa[i * 3 + 2] = a + 1, aa[i * 3 + 3] = b;
	}
	for (i = 0; i <= n * 3; i++)
		ii[i] = i;
	sort(ii, 0, n * 3 + 1);
	for (i = 0, n_ = 0; i <= n * 3; i++) {
		int a = aa[ii[i]];

		aa[ii[i]] = n_;
		if (i + 1 == n * 3 || aa[ii[i + 1]] != a)
			aa_[n_++] = a;
	}
	for (i = 0; i < n_; i++)
		ll[i] = i - 1, rr[i] = i + 1;
	for (i = 0; i < n; i++) {
		int i_ = aa[i * 3 + 1], j_ = aa[i * 3 + 3], l = ll[i_], r = rr[i_];

		if (l >= 0)
			rr[l] = r;
		if (r < n_)
			ll[r] = l;
		l = ll[j_];
		rr[i_] = j_, ll[j_] = i_;
		if (l >= 0)
			rr[l] = i_;
		ll[i_] = l;
	}
	i = 0;
	while (ll[i] >= 0)
		i++;
	p = 1, cnt = 0;
	while (i < n_) {
		qu[cnt++] = i;
		pp[i] = p;
		p += aa_[i + 1] - aa_[i];
		i = rr[i];
	}
	scanf("%d", &q);
	while (q--) {
		static char s[2];
		int x, lower, upper;

		scanf("%s%d", s, &x);
		if (s[0] == 'P') {
			lower = 0, upper = n_;
			while (upper - lower > 1) {
				i = (lower + upper) / 2;
				if (aa_[i] <= x)
					lower = i;
				else
					upper = i;
			}
			i = lower;
			printf("%d\n", pp[i] + x - aa_[i]);
		} else {
			lower = 0, upper = n_;
			while (upper - lower > 1) {
				i = (lower + upper) / 2;
				if (pp[qu[i]] <= x)
					lower = i;
				else
					upper = i;
			}
			i = lower;
			printf("%d\n", aa_[qu[i]] + x - pp[qu[i]]);
		}
	}
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

queue.c: In function 'main':
queue.c:37:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
queue.c:42:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |   scanf("%d%d", &a, &b);
      |   ^~~~~~~~~~~~~~~~~~~~~
queue.c:80:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
queue.c:85:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |   scanf("%s%d", s, &x);
      |   ^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...