Submission #695068

#TimeUsernameProblemLanguageResultExecution timeMemory
695068rainboySushi (JOI16_sushi)C11
15 / 100
4232 ms5940 KiB
#include <stdio.h>
#include <string.h>

#define N	400000
#define K	633	/* K = ceil(sqrt(N)) */
#define M	((N + K - 1) / K)

int min(int a, int b) { return a < b ? a : b; }

int aa[N], aa_[N], n; char upd[N];
int iq[M][K + 1], pq[N], cnt[M], h1;

int lt(int i, int j) { return aa[i] > aa[j]; }

int p2(int p) {
	return (p *= 2) > cnt[h1] ? 0 : (p < cnt[h1] && lt(iq[h1][p + 1], iq[h1][p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[h1][q]); p = q)
		iq[h1][pq[j] = p] = j;
	iq[h1][pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[h1][q], i); p = q)
		iq[h1][pq[j] = p] = j;
	iq[h1][pq[i] = p] = i;
}

int pq_first() {
	return iq[h1][1];
}

void pq_add(int i) {
	pq[i] = ++cnt[h1], pq_up(i);
}

int pq_remove_first() {
	int i = iq[h1][1], j = iq[h1][cnt[h1]--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

void build(int h) {
	int l = h * K, r = min((h + 1) * K, n) - 1, i, p;

	h1 = h;
	memset(pq + l, 0, (r - l + 1) * sizeof *pq), cnt[h1] = 0;
	for (i = l; i <= r; i++)
		iq[h1][pq[i] = ++cnt[h1]] = i;
	for (p = cnt[h1] / 2; p > 0; p--)
		pq_dn(iq[h1][p]);
}

void refresh(int h) {
	int l = h * K, r = min((h + 1) * K, n) - 1, i, j;

	h1 = h;
	memset(pq + l, 0, (r - l + 1) * sizeof *pq), cnt[h1] = 0;
	for (i = l; i <= r; i++)
		if (upd[i])
			pq_add(i);
	for (i = l; i <= r; i++) {
		if (!upd[i])
			pq_add(i);
		j = pq_remove_first();
		aa_[i] = aa[j];
	}
	memcpy(aa + l, aa_ + l, (r - l + 1) * sizeof *aa);
	memset(upd + l, 0, (r - l + 1) * sizeof *upd);
	build(h);
}

int walk(int h, int l, int r, int a) {
	int i, tmp;

	for (i = l; i <= r; i++)
		if (a < aa[i])
			tmp = a, a = aa[i], aa[i] = tmp;
	build(h);
	return a;
}

int run(int h, int a) {
	int i, tmp;

	h1 = h, i = pq_first();
	if (a < aa[i])
		tmp = a, a = aa[i], aa[i] = tmp, upd[i] = 1, pq_dn(i);
	return a;
}

int main() {
	int q, h, i;

	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (h = 0; h * K < n; h++)
		build(h);
	while (q--) {
		int s, t, hs, ht, a;

		scanf("%d%d%d", &s, &t, &a), s--, t--, hs = s / K, ht = t / K;
		if (s <= t) {
			if (hs == ht) {
				refresh(hs);
				a = walk(hs, s, t, a);
			} else {
				refresh(hs), refresh(ht);
				a = walk(hs, s, (hs + 1) * K - 1, a);
				for (h = hs + 1; h < ht; h++)
					a = run(h, a);
				a = walk(ht, ht * K, t, a);
			}
		} else {
			if (hs == ht)
				refresh(hs);
			else
				refresh(hs), refresh(ht);
			a = walk(hs, s, min((hs + 1) * K, n) - 1, a);
			for (h = hs + 1; h * K < n; h++)
				a = run(h, a);
			for (h = 0; h < ht; h++)
				a = run(h, a);
			a = walk(ht, ht * K, t, a);
		}
		printf("%d\n", a);
	}
	return 0;
}

Compilation message (stderr)

sushi.c: In function 'main':
sushi.c:104:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
sushi.c:106:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
sushi.c:112:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   scanf("%d%d%d", &s, &t, &a), s--, t--, hs = s / K, ht = t / K;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...