답안 #832491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832491 2023-08-21T10:52:47 Z rainboy ZIGZAG (INOI20_zigzag) C
100 / 100
1276 ms 52016 KB
#include <stdio.h>

#define N	300000
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N))) */

int sgn(long long a) { return a == 0 ? 0 : (a < 0 ? -1 : 1); }

int h_, n_;

int aa_[N_ * 2]; long long bb_[N_ * 2];

void put(int i, int a, long long b) {
	aa_[i] *= a, bb_[i] = bb_[i] * a + b;
}

void pus(int i) {
	if (aa_[i] != 1 || bb_[i] != 0)
		put(i << 1 | 0, aa_[i], bb_[i]), put(i << 1 | 1, aa_[i], bb_[i]), aa_[i] = 1, bb_[i] = 0;
}

void push(int i) {
	int h;

	for (h = h_; h > 0; h--)
		pus(i >> h);
}

int sz[N_ * 2], pppos[N_ * 2], ppneg[N_ * 2], qqpos[N_ * 2], qqneg[N_ * 2]; long long st[N_ * 2]; char lz[N_];

void put_(int i) {
	int tmp;

	tmp = pppos[i], pppos[i] = ppneg[i], ppneg[i] = tmp;
	tmp = qqpos[i], qqpos[i] = qqneg[i], qqneg[i] = tmp;
	if (i < n_)
		lz[i] ^= 1;
}

void pus_(int i) {
	if (lz[i])
		put_(i << 1 | 0), put_(i << 1 | 1), lz[i] = 0;
}

void pul_(int i) {
	if (!lz[i]) {
		int l = i << 1, r = l | 1;

		pppos[i] = pppos[l] < sz[l] ? pppos[l] : sz[l] + pppos[r];
		ppneg[i] = ppneg[l] < sz[l] ? ppneg[l] : sz[l] + ppneg[r];
		qqpos[i] = qqpos[r] < sz[r] ? qqpos[r] : qqpos[l] + sz[r];
		qqneg[i] = qqneg[r] < sz[r] ? qqneg[r] : qqneg[l] + sz[r];
		st[i] = st[l] + st[r] + (long long) qqpos[l] * pppos[r] + (long long) qqneg[l] * ppneg[r];
	}
}

void push_(int i) {
	int h;

	for (h = h_; h > 0; h--)
		pus_(i >> h);
}

void pull_(int i) {
	while (i > 1)
		pul_(i >>= 1);
}

void build(int *aa, int n) {
	int i, x;

	h_ = 0;
	while (1 << h_ < n)
		h_++;
	n_ = 1 << h_;
	for (i = 0; i < n_; i++)
		aa_[i] = 1, bb_[i] = 0;
	for (i = 0; i < n_; i++)
		aa_[n_ + i] = 0, bb_[n_ + i] = i < n ? aa[i] : 0;
	for (i = 0; i < n_; i++) {
		sz[n_ + i] = 1;
		x = i + 1 < n ? sgn(aa[i + 1] - aa[i]) * (i % 2 == 0 ? 1 : -1) : 0;
		if (x == 1)
			pppos[n_ + i] = 1, ppneg[n_ + i] = 0, qqpos[n_ + i] = 1, qqneg[n_ + i] = 0, st[n_ + i] = 1;
		else if (x == -1)
			pppos[n_ + i] = 0, ppneg[n_ + i] = 1, qqpos[n_ + i] = 0, qqneg[n_ + i] = 1, st[n_ + i] = 1;
		else
			pppos[n_ + i] = 0, ppneg[n_ + i] = 0, qqpos[n_ + i] = 0, qqneg[n_ + i] = 0, st[n_ + i] = 0;
	}
	for (i = n_ - 1; i > 0; i--)
		sz[i] = sz[i << 1 | 0] + sz[i << 1 | 1], pul_(i);
}

void update(int l, int r, int a, long long b) {
	push(l += n_), push(r += n_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			put(l++, a, b);
		if ((r & 1) == 0)
			put(r--, a, b);
	}
}

long long query(int i) {
	push(i += n_);
	return bb_[i];
}

void upd_(int i, int x) {
	i += n_;
	push_(i);
	if (x == 1)
		pppos[i] = 1, ppneg[i] = 0, qqpos[i] = 1, qqneg[i] = 0, st[i] = 1;
	else if (x == -1)
		pppos[i] = 0, ppneg[i] = 1, qqpos[i] = 0, qqneg[i] = 1, st[i] = 1;
	else
		pppos[i] = 0, ppneg[i] = 0, qqpos[i] = 0, qqneg[i] = 0, st[i] = 0;
	pull_(i);
}

void update_(int l, int r) {
	int l_, r_;

	if (l > r)
		return;
	l_ = l += n_, r_ = r += n_;
	push_(l_), push_(r_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			put_(l++);
		if ((r & 1) == 0)
			put_(r--);
	}
	pull_(l_), pull_(r_);
}

long long query_(int l, int r) {
	int qpos, qneg, ppos, pneg;
	long long x;

	if (l > r)
		return 0;
	push_(l += n_), push_(r += n_);
	qpos = 0, qneg = 0, ppos = 0, pneg = 0, x = 0;
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1) {
			x += st[l] + (long long) qpos * pppos[l] + (long long) qneg * ppneg[l];
			qpos = qqpos[l] < sz[l] ? qqpos[l] : qpos + sz[l];
			qneg = qqneg[l] < sz[l] ? qqneg[l] : qneg + sz[l];
			l++;
		}
		if ((r & 1) == 0) {
			x += st[r] + (long long) qqpos[r] * ppos + (long long) qqneg[r] * pneg;
			ppos = pppos[r] < sz[r] ? pppos[r] : sz[r] + ppos;
			pneg = ppneg[r] < sz[r] ? ppneg[r] : sz[r] + pneg;
			r--;
		}
	}
	x += (long long) qpos * ppos + (long long) qneg * pneg;
	return x;
}

int main() {
	static int aa[N];
	int n, q, i;

	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	build(aa, n);
	while (q--) {
		static char s[2];
		int l, r, x;

		scanf("%s%d%d", s, &l, &r), l--, r--;
		if (s[0] == '*') {
			update(l, r, -1, 0);
			if (l > 0)
				upd_(l - 1, sgn(query(l) - query(l - 1)) * ((l - 1) % 2 == 0 ? 1 : -1));
			update_(l, r - 1);
			if (r + 1 < n)
				upd_(r, sgn(query(r + 1) - query(r)) * (r % 2 == 0 ? 1 : -1));
		} else if (s[0] == '+') {
			scanf("%d", &x);
			update(l, r, 1, x);
			if (l > 0)
				upd_(l - 1, sgn(query(l) - query(l - 1)) * ((l - 1) % 2 == 0 ? 1 : -1));
			if (r + 1 < n)
				upd_(r, sgn(query(r + 1) - query(r)) * (r % 2 == 0 ? 1 : -1));
		} else
			printf("%lld\n", query_(l, r - 1) + r - l + 1);
	}
	return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:166:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:168:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:174:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |   scanf("%s%d%d", s, &l, &r), l--, r--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:183:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |    scanf("%d", &x);
      |    ^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 980 KB Output is correct
2 Correct 5 ms 980 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 6 ms 980 KB Output is correct
5 Correct 7 ms 980 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 7 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 619 ms 47532 KB Output is correct
2 Correct 73 ms 2560 KB Output is correct
3 Correct 516 ms 51720 KB Output is correct
4 Correct 631 ms 51708 KB Output is correct
5 Correct 575 ms 51844 KB Output is correct
6 Correct 596 ms 51708 KB Output is correct
7 Correct 610 ms 48580 KB Output is correct
8 Correct 641 ms 51660 KB Output is correct
9 Correct 566 ms 51616 KB Output is correct
10 Correct 470 ms 51700 KB Output is correct
11 Correct 552 ms 51660 KB Output is correct
12 Correct 646 ms 51680 KB Output is correct
13 Correct 320 ms 51456 KB Output is correct
14 Correct 324 ms 51300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 980 KB Output is correct
2 Correct 5 ms 980 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 6 ms 980 KB Output is correct
5 Correct 7 ms 980 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 7 ms 980 KB Output is correct
8 Correct 180 ms 11248 KB Output is correct
9 Correct 168 ms 11296 KB Output is correct
10 Correct 188 ms 11292 KB Output is correct
11 Correct 392 ms 11236 KB Output is correct
12 Correct 193 ms 11212 KB Output is correct
13 Correct 164 ms 11276 KB Output is correct
14 Correct 143 ms 11212 KB Output is correct
15 Correct 149 ms 11264 KB Output is correct
16 Correct 196 ms 11280 KB Output is correct
17 Correct 158 ms 11264 KB Output is correct
18 Correct 82 ms 11432 KB Output is correct
19 Correct 100 ms 11444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 980 KB Output is correct
2 Correct 5 ms 980 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 6 ms 980 KB Output is correct
5 Correct 7 ms 980 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 7 ms 980 KB Output is correct
8 Correct 619 ms 47532 KB Output is correct
9 Correct 73 ms 2560 KB Output is correct
10 Correct 516 ms 51720 KB Output is correct
11 Correct 631 ms 51708 KB Output is correct
12 Correct 575 ms 51844 KB Output is correct
13 Correct 596 ms 51708 KB Output is correct
14 Correct 610 ms 48580 KB Output is correct
15 Correct 641 ms 51660 KB Output is correct
16 Correct 566 ms 51616 KB Output is correct
17 Correct 470 ms 51700 KB Output is correct
18 Correct 552 ms 51660 KB Output is correct
19 Correct 646 ms 51680 KB Output is correct
20 Correct 320 ms 51456 KB Output is correct
21 Correct 324 ms 51300 KB Output is correct
22 Correct 180 ms 11248 KB Output is correct
23 Correct 168 ms 11296 KB Output is correct
24 Correct 188 ms 11292 KB Output is correct
25 Correct 392 ms 11236 KB Output is correct
26 Correct 193 ms 11212 KB Output is correct
27 Correct 164 ms 11276 KB Output is correct
28 Correct 143 ms 11212 KB Output is correct
29 Correct 149 ms 11264 KB Output is correct
30 Correct 196 ms 11280 KB Output is correct
31 Correct 158 ms 11264 KB Output is correct
32 Correct 82 ms 11432 KB Output is correct
33 Correct 100 ms 11444 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 292 KB Output is correct
36 Correct 769 ms 48896 KB Output is correct
37 Correct 724 ms 51928 KB Output is correct
38 Correct 1276 ms 50648 KB Output is correct
39 Correct 717 ms 51928 KB Output is correct
40 Correct 721 ms 48884 KB Output is correct
41 Correct 616 ms 51972 KB Output is correct
42 Correct 669 ms 48980 KB Output is correct
43 Correct 725 ms 51928 KB Output is correct
44 Correct 727 ms 51920 KB Output is correct
45 Correct 738 ms 52016 KB Output is correct
46 Correct 695 ms 51948 KB Output is correct
47 Correct 388 ms 51728 KB Output is correct