답안 #543940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
543940 2022-03-31T16:42:05 Z rainboy Radio (COCI22_radio) C
110 / 110
826 ms 179184 KB
#include <stdio.h>
#include <string.h>

#define N	1000000
#define K	7	/* 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 > N */
#define N_	(1 << 24)	/* N_ = pow2(ceil(log2(N))) */
#define Q	200000
#define N1	(2 + Q * K)
#define INF	0x3f3f3f3f

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

unsigned int X = 12345;

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

int pp[N + 1][K], kk_[N + 1], ll_[N + 2];

void init() {
	int a, b;

	for (a = 2; a <= N; a++) {
		if (kk_[a])
			continue;
		for (b = a; b <= N; b += a)
			pp[b][kk_[b]++] = a;
	}
	for (a = 1; a <= N + 1; a++)
		ll_[a] = ll_[a - 1] + kk_[a - 1];
}

int zz[1 + N1], ll[1 + N1], rr[1 + N1], kk[1 + N1], u_, l_, r_;

int node(int k) {
	static int _ = 1;

	zz[_] = rand_();
	kk[_] = k;
	return _++;
}

void split(int u, int k) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	if (kk[u] < k) {
		split(rr[u], k);
		rr[u] = l_, l_ = u;
	} else if (kk[u] > k) {
		split(ll[u], k);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

int first(int u) { return ll[u] == 0 ? u : first(ll[u]); }
int last(int u) { return rr[u] == 0 ? u : last(rr[u]); }

int st[N_ * 2];

void pul(int i) {
	st[i] = min(st[i << 1 | 0], st[i << 1 | 1]);
}

void update(int i, int x) {
	st[i += N_] = x;
	while (i > 1)
		pul(i >>= 1);
}

void update_(int i, int p, int x) {
	int h;

	for (h = 0; h < kk_[i]; h++)
		if (pp[i][h] == p) {
			update(ll_[i] + h, x);
			break;
		}
}

int query(int l) {
	int r = N_ - 1, x = INF;

	for (l += N_, r += N_; l <= r; l >>= 1, r >>= 1)
		if ((l & 1) == 1)
			x = min(x, st[l++]);
	return x;
}

int main() {
	static char on[N + 1];
	static int tt[N + 1];
	int n, q;

	init();
	scanf("%d%d", &n, &q);
	memset(st, 0x3f, N_ * 2 * sizeof *st);
	while (q--) {
		static char cc[N + 1];
		int h, i, p, l, r;

		scanf("%s", cc);
		if (cc[0] == 'S') {
			scanf("%d", &i);
			if (!on[i]) {
				on[i] = 1;
				for (h = 0; h < kk_[i]; h++) {
					p = pp[i][h];
					split(tt[p], i);
					if (l_)
						update_(kk[last(l_)], p, i);
					if (r_)
						update_(i, p, kk[first(r_)]);
					tt[p] = merge(merge(l_, node(i)), r_);
				}
			} else {
				on[i] = 0;
				for (h = 0; h < kk_[i]; h++) {
					p = pp[i][h];
					split(tt[p], i);
					update_(i, p, INF);
					if (l_)
						update_(kk[last(l_)], p, r_ ? kk[first(r_)] : INF);
					tt[p] = merge(l_, r_);
				}
			}
		} else {
			scanf("%d%d", &l, &r);
			printf(query(ll_[l]) <= r ? "DA\n" : "NE\n");
		}
	}
	return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:115:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:121:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |   scanf("%s", cc);
      |   ^~~~~~~~~~~~~~~
Main.c:123:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |    scanf("%d", &i);
      |    ^~~~~~~~~~~~~~~
Main.c:147:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |    scanf("%d%d", &l, &r);
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 166764 KB Output is correct
2 Correct 96 ms 166880 KB Output is correct
3 Correct 99 ms 166768 KB Output is correct
4 Correct 95 ms 166784 KB Output is correct
5 Correct 97 ms 166756 KB Output is correct
6 Correct 117 ms 166808 KB Output is correct
7 Correct 106 ms 166752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 170672 KB Output is correct
2 Correct 572 ms 174924 KB Output is correct
3 Correct 690 ms 178260 KB Output is correct
4 Correct 114 ms 167524 KB Output is correct
5 Correct 163 ms 169944 KB Output is correct
6 Correct 185 ms 173036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 166764 KB Output is correct
2 Correct 96 ms 166880 KB Output is correct
3 Correct 99 ms 166768 KB Output is correct
4 Correct 95 ms 166784 KB Output is correct
5 Correct 97 ms 166756 KB Output is correct
6 Correct 117 ms 166808 KB Output is correct
7 Correct 106 ms 166752 KB Output is correct
8 Correct 353 ms 170672 KB Output is correct
9 Correct 572 ms 174924 KB Output is correct
10 Correct 690 ms 178260 KB Output is correct
11 Correct 114 ms 167524 KB Output is correct
12 Correct 163 ms 169944 KB Output is correct
13 Correct 185 ms 173036 KB Output is correct
14 Correct 286 ms 170060 KB Output is correct
15 Correct 521 ms 171324 KB Output is correct
16 Correct 826 ms 179184 KB Output is correct
17 Correct 617 ms 177720 KB Output is correct
18 Correct 794 ms 178564 KB Output is correct
19 Correct 710 ms 178520 KB Output is correct
20 Correct 186 ms 173092 KB Output is correct
21 Correct 186 ms 173036 KB Output is correct
22 Correct 204 ms 173028 KB Output is correct
23 Correct 183 ms 173028 KB Output is correct