답안 #382662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
382662 2021-03-28T01:57:50 Z rainboy 가로등 (APIO19_street_lamps) C
100 / 100
969 ms 26252 KB
#include <stdio.h>
#include <string.h>
 
#define N	300000
#define Q	300000
#define M	(Q * 3)
 
unsigned int X = 12345;

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

int ft[N], n;
 
void update(int i, int x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}
 
int query(int i) {
	int x = 0;
 
	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}
 
int type[Q], ll1[Q], rr1[Q], ans[Q];
int ll_[M], rr_[M], xx[M], hh[M], hh_[M];
 
void merge_(int l, int m, int r) {
	int i, j, k;
 
	i = l, j = m, k = l;
	while (i < m && j < r)
		hh_[k++] = rr_[hh[i]] < rr_[hh[j]] ? hh[i++] : hh[j++];
	while (i < m)
		hh_[k++] = hh[i++];
	while (j < r)
		hh_[k++] = hh[j++];
	memcpy(hh + l, hh_ + l, (r - l) * sizeof *hh_);
}
 
void solve(int l, int r) {
	int m, l_, m_, r_, i, j;
 
	if (r - l == 1)
		return;
	m = (l + r) / 2;
	solve(l, m), solve(m, r);
	l_ = ll1[l], m_ = ll1[m], r_ = rr1[r - 1];
	for (j = r_ - 1, i = m_ - 1; j >= m_; j--) {
		int hi, hj = hh[j];
 
		while (i >= l_ && rr_[hi = hh[i]] >= rr_[hj]) {
			if (xx[hi] < 0 || type[xx[hi] - 1] == 1)
				update(ll_[hi], xx[hi]);
			i--;
		}
		if (xx[hj] > 0 && type[xx[hj] - 1] == 2)
			ans[xx[hj] - 1] += query(ll_[hj]);
	}
	while (++i < m_) {
		int hi = hh[i];
 
		if (xx[hi] < 0 || type[xx[hi] - 1] == 1)
			update(ll_[hi], -xx[hi]);
	}
	merge_(l_, m_, r_);
}
 
int zz[1 + N + Q], ll[1 + N + Q], rr[1 + N + Q], ii[1 + N + Q], u_, l_, r_;

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

	zz[_] = rand_();
	ii[_] = i;
	return _++;
}

void split(int u, int i) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	if (ii[u] < i) {
		split(rr[u], i);
		rr[u] = l_, l_ = u;
	} else if (ii[u] > i) {
		split(ll[u], i);
		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 main() {
	static char cc[N + 1];
	int q, m, h, i;
 
	scanf("%d%d%s", &n, &q, cc);
	for (i = 0; i < n; i++)
		if (cc[i] == '1')
			update(i, 1);
		else
			u_ = merge(u_, node(i));
	m = 0;
	for (h = 0; h < q; h++) {
		static char s[8];
 
		scanf("%s", s);
		ll1[h] = m;
		if (s[0] == 't') {
			int p, q;
 
			scanf("%d", &i), i--;
			split(u_, i), p = l_ ? ii[last(l_)] + 1 : 0, q = r_ ? ii[first(r_)] - 1 : n - 1;
			type[h] = 1;
			if (cc[i] == '0') {
				cc[i] = '1';
				u_ = merge(l_, r_);
				if (p < i)
					ll_[m] = p, rr_[m] = i - 1, xx[m] = h + 1, m++;
				ll_[m] = p, rr_[m] = q, xx[m] = -(h + 1), m++;
				if (i < q)
					ll_[m] = i + 1, rr_[m] = q, xx[m] = h + 1, m++;
				update(i, 1);
			} else {
				cc[i] = '0';
				u_ = merge(merge(l_, node(i)), r_);
				if (p < i)
					ll_[m] = p, rr_[m] = i - 1, xx[m] = -(h + 1), m++;
				ll_[m] = p, rr_[m] = q, xx[m] = h + 1, m++;
				if (i < q)
					ll_[m] = i + 1, rr_[m] = q, xx[m] = -(h + 1), m++;
				update(i, -1);
			}
		} else {
			int l, r;
 
			scanf("%d%d", &l, &r), l--, r -= 2;
			type[h] = 2;
			ll_[m] = l, rr_[m] = r, xx[m] = h + 1, m++;
			if (query(r) - query(l - 1) == r - l + 1)
				ans[h] += h + 1;
		}
		rr1[h] = m;
	}
	for (h = 0; h < m; h++)
		hh[h] = h;
	memset(ft, 0, n * sizeof *ft);
	solve(0, q);
	for (h = 0; h < q; h++)
		if (type[h] == 2)
			printf("%d\n", ans[h]);
	return 0;
}

Compilation message

street_lamps.c: In function 'main':
street_lamps.c:130:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  130 |  scanf("%d%d%s", &n, &q, cc);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.c:140:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  140 |   scanf("%s", s);
      |   ^~~~~~~~~~~~~~
street_lamps.c:145:4: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  145 |    scanf("%d", &i), i--;
      |    ^~~~~~~~~~~~~~~
street_lamps.c:170:4: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  170 |    scanf("%d%d", &l, &r), l--, r -= 2;
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 15980 KB Output is correct
2 Correct 436 ms 16108 KB Output is correct
3 Correct 516 ms 16136 KB Output is correct
4 Correct 748 ms 19680 KB Output is correct
5 Correct 663 ms 19180 KB Output is correct
6 Correct 748 ms 19692 KB Output is correct
7 Correct 365 ms 17644 KB Output is correct
8 Correct 344 ms 14512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 961 ms 23088 KB Output is correct
6 Correct 835 ms 21312 KB Output is correct
7 Correct 647 ms 18800 KB Output is correct
8 Correct 365 ms 14468 KB Output is correct
9 Correct 148 ms 9964 KB Output is correct
10 Correct 166 ms 10792 KB Output is correct
11 Correct 160 ms 10732 KB Output is correct
12 Correct 361 ms 17772 KB Output is correct
13 Correct 341 ms 14316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 365 ms 21356 KB Output is correct
6 Correct 560 ms 22944 KB Output is correct
7 Correct 736 ms 24300 KB Output is correct
8 Correct 969 ms 26252 KB Output is correct
9 Correct 430 ms 19692 KB Output is correct
10 Correct 417 ms 20716 KB Output is correct
11 Correct 431 ms 19692 KB Output is correct
12 Correct 421 ms 20844 KB Output is correct
13 Correct 433 ms 19564 KB Output is correct
14 Correct 423 ms 20844 KB Output is correct
15 Correct 365 ms 23660 KB Output is correct
16 Correct 338 ms 20460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 392 ms 15980 KB Output is correct
9 Correct 436 ms 16108 KB Output is correct
10 Correct 516 ms 16136 KB Output is correct
11 Correct 748 ms 19680 KB Output is correct
12 Correct 663 ms 19180 KB Output is correct
13 Correct 748 ms 19692 KB Output is correct
14 Correct 365 ms 17644 KB Output is correct
15 Correct 344 ms 14512 KB Output is correct
16 Correct 2 ms 492 KB Output is correct
17 Correct 2 ms 492 KB Output is correct
18 Correct 2 ms 492 KB Output is correct
19 Correct 2 ms 364 KB Output is correct
20 Correct 961 ms 23088 KB Output is correct
21 Correct 835 ms 21312 KB Output is correct
22 Correct 647 ms 18800 KB Output is correct
23 Correct 365 ms 14468 KB Output is correct
24 Correct 148 ms 9964 KB Output is correct
25 Correct 166 ms 10792 KB Output is correct
26 Correct 160 ms 10732 KB Output is correct
27 Correct 361 ms 17772 KB Output is correct
28 Correct 341 ms 14316 KB Output is correct
29 Correct 2 ms 492 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
31 Correct 2 ms 492 KB Output is correct
32 Correct 2 ms 492 KB Output is correct
33 Correct 365 ms 21356 KB Output is correct
34 Correct 560 ms 22944 KB Output is correct
35 Correct 736 ms 24300 KB Output is correct
36 Correct 969 ms 26252 KB Output is correct
37 Correct 430 ms 19692 KB Output is correct
38 Correct 417 ms 20716 KB Output is correct
39 Correct 431 ms 19692 KB Output is correct
40 Correct 421 ms 20844 KB Output is correct
41 Correct 433 ms 19564 KB Output is correct
42 Correct 423 ms 20844 KB Output is correct
43 Correct 365 ms 23660 KB Output is correct
44 Correct 338 ms 20460 KB Output is correct
45 Correct 203 ms 12524 KB Output is correct
46 Correct 252 ms 12800 KB Output is correct
47 Correct 408 ms 14828 KB Output is correct
48 Correct 723 ms 24556 KB Output is correct
49 Correct 188 ms 15212 KB Output is correct
50 Correct 184 ms 15084 KB Output is correct
51 Correct 186 ms 15340 KB Output is correct
52 Correct 183 ms 15468 KB Output is correct
53 Correct 180 ms 15468 KB Output is correct
54 Correct 190 ms 15596 KB Output is correct