제출 #645697

#제출 시각아이디문제언어결과실행 시간메모리
645697rainboyFish 2 (JOI22_fish2)C11
13 / 100
4062 ms6008 KiB
#include <stdio.h>
#include <string.h>

#define N	100000
#define N_	(1 << 17)	/* N_ = pow2(ceil(log2(N + 1))) */
#define INF	0x3f3f3f3f

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

long long st1[N_ * 2]; int st2[N_ * 2], kk2[N_ * 2], lz2[N_], h_, n_;

void pul1(int i) {
	int l = i << 1, r = l | 1;

	st1[i] = st1[l] + st1[r];
}

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

	h_ = 0;
	while (1 << h_ <= n)
		h_++;
	n_ = 1 << h_;
	for (i = 0; i < n; i++)
		st1[n_ + i] = aa[i];
	for (i = n_ - 1; i > 0; i--)
		pul1(i);
}

void update1(int i, int a) {
	i += n_;
	st1[i] = a;
	while (i > 1)
		pul1(i >>= 1);
}

int queryl1(int r, long long s) {
	int l = 0;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((r & 1) == 0) {
			if (s > st1[r])
				s -= st1[r--];
			else {
				while (r < n_)
					if (s <= st1[r << 1 | 1])
						r = r << 1 | 1;
					else
						s -= st1[r << 1 | 1], r = r << 1 | 0;
				return r - n_;
			}
		}
	return -1;
}

int queryr1(int l, long long s) {
	int r = n_ - 1;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((l & 1) == 1) {
			if (s > st1[l])
				s -= st1[l++];
			else {
				while (l < n_)
					if (s <= st1[l << 1 | 0])
						l = l << 1 | 0;
					else
						s -= st1[l << 1 | 0], l = l << 1 | 1;
				return l - n_;
			}
		}
	return n_;
}

void put2(int i, int x) {
	st2[i] += x;
	if (i < n_)
		lz2[i] += x;
}

void pus2(int i) {
	if (lz2[i])
		put2(i << 1 | 0, lz2[i]), put2(i << 1 | 1, lz2[i]), lz2[i] = 0;
}

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

		if (st2[l] < st2[r])
			st2[i] = st2[l], kk2[i] = kk2[l];
		else if (st2[l] > st2[r])
			st2[i] = st2[r], kk2[i] = kk2[r];
		else
			st2[i] = st2[l], kk2[i] = kk2[l] + kk2[r];
	}
}

void push2(int i) {
	int h;

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

void pull2(int i) {
	while (i > 1)
		pul2(i >>= 1);
}

void update2(int l, int r, int x) {
	int l_ = l += n_, r_ = r += n_;

	push2(l_), push2(r_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			put2(l++, x);
		if ((r & 1) == 0)
			put2(r--, x);
	}
	pull2(l_), pull2(r_);
}

int query2(int l, int r) {
	int x = INF, k = 0;

	push2(l += n_), push2(r += n_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1) {
			if (x > st2[l])
				x = st2[l], k = 0;
			if (x == st2[l])
				k += kk2[l];
			l++;
		}
		if ((r & 1) == 0) {
			if (x > st2[r])
				x = st2[r], k = 0;
			if (x == st2[r])
				k += kk2[r];
			r--;
		}
	}
	return x == 0 ? k : 0;
}

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

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	build(aa, n);
	scanf("%d", &q);
	while (q--) {
		int t, a, l, r;

		scanf("%d", &t);
		if (t == 1) {
			scanf("%d%d", &i, &a), i--;
			aa[i] = a;
			update1(i, a);
		} else {
			int j;

			scanf("%d%d", &l, &r), l--, r--;
			memset(st2, 0, n_ * 2 * sizeof *st2);
			memset(kk2, 0, n_ * 2 * sizeof *kk2);
			memset(lz2, 0, n_ * sizeof *lz2);
			for (i = 0; i < n_; i++)
				kk2[n_ + i] = 1;
			for (i = n_ - 1; i > 0; i--)
				pul2(i);
			for (i = l; i <= r; i++) {
				j = max(queryl1(i - 1, aa[i]), l - 1);
				if (j < l || aa[j] > aa[i])
					update2(j + 1, i - 1, 1);
				j = min(queryr1(i + 1, aa[i]), r + 1);
				if (j > r || aa[j] >= aa[i])
					update2(i + 1, j - 1, 1);
			}
			printf("%d\n", query2(l, r));
		}
	}
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

fish2.c: In function 'main':
fish2.c:153:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
fish2.c:155:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
fish2.c:157:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
fish2.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |   scanf("%d", &t);
      |   ^~~~~~~~~~~~~~~
fish2.c:163:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |    scanf("%d%d", &i, &a), i--;
      |    ^~~~~~~~~~~~~~~~~~~~~
fish2.c:169:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |    scanf("%d%d", &l, &r), l--, r--;
      |    ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...