답안 #707734

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
707734 2023-03-09T23:13:03 Z rainboy Fire (JOI20_ho_t5) C
100 / 100
446 ms 73672 KB
#include <stdio.h>

#define N	200000
#define Q	200000
#define N_	(1 << 18)	/* N_ = pow2(ceil(log2(N))) */
#define M	(N * 8 + Q)

typedef unsigned long long ull;

int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

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

void ft_update(ull *ft, int n, int i, ull x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

ull ft_query(ull *ft, int i) {
	ull x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int st[N_ * 2], n_;

void st_build(int *aa, int n) {
	int i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	for (i = 0; i < n_; i++)
		st[n_ + i] = i < n ? aa[i] : 0;
	for (i = n_ - 1; i > 0; i--)
		st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}

int st_query(int l, int r) {
	int x = 0;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			x = max(x, st[l++]);
		if ((r & 1) == 0)
			x = max(x, st[r--]);
	}
	return x;
}

int type[M], tt[M], ll[M], rr[M], ii[M], m; ull xx[M], yy[M];

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

		while (j < k) {
			int c = tt[hh[j]] != tt[h] ? tt[hh[j]] - tt[h] : type[hh[j]] - type[h];

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
			}
		}
		sort(hh, l, i);
		l = k;
	}
}

void add_upd0(int t, int i, ull x) {
	type[m] = 0, tt[m] = t, ii[m] = i, xx[m] = x, m++;
}

void add_upd1(int t, int i, ull x, ull y) {
	type[m] = 1, tt[m] = t, ii[m] = i, xx[m] = x, yy[m] = y, m++;
}

void add_query(int t, int l, int r) {
	type[m] = 2, tt[m] = t, ll[m] = l, rr[m] = r, m++;
}

int main() {
	static int aa[N], qu[N], pp[N], qq[N], hh[M];
	static ull ft1[N], ft2[N], ft3[N], ans[Q];
	int n, q, cnt, h, h_, i, t, l, r;

	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	st_build(aa, n);
	cnt = 0;
	for (i = 0; i < n; i++) {
		while (cnt && aa[qu[cnt - 1]] <= aa[i])
			cnt--;
		pp[i] = cnt == 0 ? -1 : qu[cnt - 1];
		qu[cnt++] = i;
	}
	cnt = 0;
	for (i = n - 1; i >= 0; i--) {
		while (cnt && aa[qu[cnt - 1]] < aa[i])
			cnt--;
		qq[i] = cnt == 0 ? n : qu[cnt - 1];
		qu[cnt++] = i;
	}
	for (h = 0; h < q; h++) {
		scanf("%d%d%d", &t, &l, &r), l--, r--;
		add_query(t, l, r);
	}
	for (i = 0; i < n; i++)
		if (pp[i] == -1) {
			add_upd0(0, i, (ull) -i * aa[i]);
			add_upd1(0, i + 1, (ull) (i + 1) * aa[i], aa[i]);
			add_upd1(qq[i] - i, i + 1, (ull) -(i + 1) * aa[i], -aa[i]);
			add_upd0(qq[i] - i, qq[i], (ull) qq[i] * aa[i]);
		} else {
			add_upd0(0, i, (ull) -i * aa[i]);
			add_upd0(i - pp[i], i, (ull) i * aa[i]);
			add_upd1(i - pp[i], pp[i] + 1, (ull) -(pp[i] + 1) * aa[i], -aa[i]);
			add_upd1(qq[i] - pp[i], pp[i] + 1, (ull) (pp[i] + 1) * aa[i], aa[i]);
			add_upd1(0, i + 1, (ull) (i + 1) * aa[i], aa[i]);
			add_upd1(qq[i] - i, i + 1, (ull) -(i + 1) * aa[i], -aa[i]);
			add_upd0(qq[i] - i, qq[i], (ull) qq[i] * aa[i]);
			add_upd0(qq[i] - pp[i], qq[i], (ull) -qq[i] * aa[i]);
		}
	for (h = 0; h < m; h++)
		hh[h] = h;
	sort(hh, 0, m);
	for (h = 0; h < m; h++) {
		h_ = hh[h];
		if (type[h_] == 0)
			ft_update(ft1, n, ii[h_], xx[h_]);
		else if (type[h_] == 1) {
			ft_update(ft2, n, ii[h_], xx[h_]);
			ft_update(ft3, n, ii[h_], yy[h_]);
		} else {
			ans[h_] += ft_query(ft1, rr[h_]) - ft_query(ft1, ll[h_]);
			ans[h_] += ft_query(ft2, rr[h_] - tt[h_]) - ft_query(ft2, ll[h_] - tt[h_]);
			ans[h_] += (ft_query(ft3, rr[h_] - tt[h_]) - ft_query(ft3, ll[h_] - tt[h_])) * tt[h_];
			ans[h_] += (ull) st_query(max(rr[h_] - tt[h_], 0), rr[h_]) * (rr[h_] + 1);
			ans[h_] -= (ull) st_query(max(ll[h_] - tt[h_], 0), ll[h_]) * ll[h_];
		}
	}
	for (h = 0; h < q; h++)
		printf("%llu\n", ans[h]);
	return 0;
}

Compilation message

ho_t5.c: In function 'main':
ho_t5.c:102:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
ho_t5.c:104:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
ho_t5.c:121:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |   scanf("%d%d%d", &t, &l, &r), l--, r--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 416 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 424 KB Output is correct
20 Correct 1 ms 424 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 420 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 420 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 420 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 357 ms 72108 KB Output is correct
3 Correct 331 ms 71400 KB Output is correct
4 Correct 355 ms 71704 KB Output is correct
5 Correct 366 ms 72912 KB Output is correct
6 Correct 346 ms 71932 KB Output is correct
7 Correct 354 ms 72444 KB Output is correct
8 Correct 364 ms 73196 KB Output is correct
9 Correct 368 ms 72732 KB Output is correct
10 Correct 352 ms 71116 KB Output is correct
11 Correct 346 ms 72904 KB Output is correct
12 Correct 353 ms 70996 KB Output is correct
13 Correct 352 ms 72524 KB Output is correct
14 Correct 361 ms 72524 KB Output is correct
15 Correct 353 ms 72504 KB Output is correct
16 Correct 359 ms 72000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 417 ms 70852 KB Output is correct
3 Correct 388 ms 69840 KB Output is correct
4 Correct 417 ms 72348 KB Output is correct
5 Correct 385 ms 70484 KB Output is correct
6 Correct 395 ms 71100 KB Output is correct
7 Correct 399 ms 71316 KB Output is correct
8 Correct 396 ms 70608 KB Output is correct
9 Correct 413 ms 69972 KB Output is correct
10 Correct 399 ms 69384 KB Output is correct
11 Correct 409 ms 72188 KB Output is correct
12 Correct 393 ms 71540 KB Output is correct
13 Correct 422 ms 71756 KB Output is correct
14 Correct 429 ms 70216 KB Output is correct
15 Correct 404 ms 72220 KB Output is correct
16 Correct 413 ms 71464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 334 ms 56228 KB Output is correct
2 Correct 332 ms 56560 KB Output is correct
3 Correct 344 ms 58032 KB Output is correct
4 Correct 333 ms 56204 KB Output is correct
5 Correct 334 ms 56488 KB Output is correct
6 Correct 329 ms 56652 KB Output is correct
7 Correct 330 ms 57644 KB Output is correct
8 Correct 344 ms 57120 KB Output is correct
9 Correct 332 ms 56476 KB Output is correct
10 Correct 317 ms 57092 KB Output is correct
11 Correct 330 ms 56672 KB Output is correct
12 Correct 319 ms 56912 KB Output is correct
13 Correct 331 ms 56560 KB Output is correct
14 Correct 322 ms 56832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 416 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 424 KB Output is correct
20 Correct 1 ms 424 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 420 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 420 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 420 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 357 ms 72108 KB Output is correct
34 Correct 331 ms 71400 KB Output is correct
35 Correct 355 ms 71704 KB Output is correct
36 Correct 366 ms 72912 KB Output is correct
37 Correct 346 ms 71932 KB Output is correct
38 Correct 354 ms 72444 KB Output is correct
39 Correct 364 ms 73196 KB Output is correct
40 Correct 368 ms 72732 KB Output is correct
41 Correct 352 ms 71116 KB Output is correct
42 Correct 346 ms 72904 KB Output is correct
43 Correct 353 ms 70996 KB Output is correct
44 Correct 352 ms 72524 KB Output is correct
45 Correct 361 ms 72524 KB Output is correct
46 Correct 353 ms 72504 KB Output is correct
47 Correct 359 ms 72000 KB Output is correct
48 Correct 417 ms 70852 KB Output is correct
49 Correct 388 ms 69840 KB Output is correct
50 Correct 417 ms 72348 KB Output is correct
51 Correct 385 ms 70484 KB Output is correct
52 Correct 395 ms 71100 KB Output is correct
53 Correct 399 ms 71316 KB Output is correct
54 Correct 396 ms 70608 KB Output is correct
55 Correct 413 ms 69972 KB Output is correct
56 Correct 399 ms 69384 KB Output is correct
57 Correct 409 ms 72188 KB Output is correct
58 Correct 393 ms 71540 KB Output is correct
59 Correct 422 ms 71756 KB Output is correct
60 Correct 429 ms 70216 KB Output is correct
61 Correct 404 ms 72220 KB Output is correct
62 Correct 413 ms 71464 KB Output is correct
63 Correct 334 ms 56228 KB Output is correct
64 Correct 332 ms 56560 KB Output is correct
65 Correct 344 ms 58032 KB Output is correct
66 Correct 333 ms 56204 KB Output is correct
67 Correct 334 ms 56488 KB Output is correct
68 Correct 329 ms 56652 KB Output is correct
69 Correct 330 ms 57644 KB Output is correct
70 Correct 344 ms 57120 KB Output is correct
71 Correct 332 ms 56476 KB Output is correct
72 Correct 317 ms 57092 KB Output is correct
73 Correct 330 ms 56672 KB Output is correct
74 Correct 319 ms 56912 KB Output is correct
75 Correct 331 ms 56560 KB Output is correct
76 Correct 322 ms 56832 KB Output is correct
77 Correct 421 ms 71568 KB Output is correct
78 Correct 419 ms 72684 KB Output is correct
79 Correct 417 ms 72812 KB Output is correct
80 Correct 429 ms 71428 KB Output is correct
81 Correct 415 ms 71252 KB Output is correct
82 Correct 408 ms 72256 KB Output is correct
83 Correct 442 ms 71896 KB Output is correct
84 Correct 405 ms 70876 KB Output is correct
85 Correct 421 ms 72780 KB Output is correct
86 Correct 423 ms 71464 KB Output is correct
87 Correct 382 ms 73164 KB Output is correct
88 Correct 405 ms 73180 KB Output is correct
89 Correct 412 ms 70928 KB Output is correct
90 Correct 407 ms 72672 KB Output is correct
91 Correct 377 ms 71492 KB Output is correct
92 Correct 401 ms 70692 KB Output is correct
93 Correct 384 ms 71900 KB Output is correct
94 Correct 401 ms 73672 KB Output is correct
95 Correct 380 ms 73232 KB Output is correct
96 Correct 392 ms 71908 KB Output is correct
97 Correct 429 ms 71936 KB Output is correct
98 Correct 436 ms 70924 KB Output is correct
99 Correct 426 ms 71884 KB Output is correct
100 Correct 446 ms 72252 KB Output is correct
101 Correct 420 ms 71568 KB Output is correct
102 Correct 433 ms 72668 KB Output is correct