Submission #708482

# Submission time Handle Problem Language Result Execution time Memory
708482 2023-03-11T20:29:10 Z rainboy Dungeon 3 (JOI21_ho_t5) C
0 / 100
4000 ms 172412 KB
#include <stdio.h>
#include <string.h>

#define N	200000
#define LN	18	/* LN = ceil(log2(N)) */
#define N_	(1 << LN)
#define N1	(N * 2 * (LN + 1) + 1)
#define M	200000
#define M_	(N + M)
#define INF	0x3f3f3f3f3f3f3f3fLL

typedef unsigned long long ull;

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

unsigned int X = 12345;

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

int xx[N], cc[N], pp[N], qq[N], n; long long xx_[N + 1]; ull xx1[N + 1];
int ll_[M], rr_[M], tt_[M], m; ull ans[M];
long long ww[M_]; int hh[M_], m_;

int st[N_ * 2], n_;

void build1(int *xx, int n) {
	int i;

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

int query1(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 zz[N1], ll[N1], rr[N1], yy[N1], _, u_, l_, r_; ull aa[N1], aa_[N1], bb[N1], bb_[N1];

int node(int y, ull a, ull b) {
	zz[_] = rand_();
	ll[_] = rr[_] = 0;
	yy[_] = y;
	aa_[_] = aa[_] = a;
	bb_[_] = bb[_] = b;
	return _++;
}

void pul(int u) {
	int l = ll[u], r = rr[u];

	aa_[u] = aa_[l] + aa[u] + aa_[r];
	bb_[u] = bb_[l] + bb[u] + bb_[r];
}

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

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), pul(u);
		return u;
	} else {
		ll[v] = merge(u, ll[v]), pul(v);
		return v;
	}
}

int tr_update(int u, int y, ull a, ull b) {
	split(u, y);
	if (u_ == 0)
		u_ = node(y, a, b);
	else
		aa_[u_] = aa[u_] += a, bb_[u_] = bb[u_] += b;
	u = merge(merge(l_, u_), r_);
	return u;
}

ull tr_query(int u, int y, ull x) {
	ull z;

	z = 0;
	while (u)
		if (yy[u] <= y)
			z += (aa_[u] - aa_[rr[u]]) * x + (bb_[u] - bb_[rr[u]]), u = rr[u];
		else
			u = ll[u];
	return z;
}

int tt[N_ * 2];

void build2() {
	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	memset(tt, 0, n_ * 2 * sizeof *tt), _ = 1;
}

void update2(int i, int y, ull a, ull b) {
	for (i += n_; i > 0; i >>= 1)
		tt[i] = tr_update(tt[i], y, a, b);
}

ull query2(int l, int r, int y, ull x) {
	ull z;

	z = 0;
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			z += tr_query(tt[l++], y, x);
		if ((r & 1) == 0)
			z += tr_query(tt[r--], y, x);
	}
	return z;
}

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 = ww[hh[j]] != ww[h] ? ww[hh[j]] - ww[h] : hh[j] - 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 solvel() {
	int h, h_, i;

	for (h = 0; h < m; h++)
		ww[h] = tt_[h];
	for (i = 0; i < n; i++)
		ww[m + i] = pp[i] == -1 ? INF : xx_[i] - xx_[pp[i]];
	for (h = 0; h < m_; h++)
		hh[h] = h;
	sort(hh, 0, m_);
	build2();
	for (h = 0; h < m_; h++) {
		h_ = hh[h];
		if (h_ >= m) {
			i = h_ - m;
			if (pp[i] != -1)
				update2(pp[i], i, cc[i], (ull) (xx_[pp[i]] - xx_[i]) * cc[i]);
		} else
			ans[h_] -= xx1[rr_[h_]] - xx1[ll_[h_]] + query2(ll_[h_], rr_[h_] - 1, rr_[h_] - 1, tt_[h_]);
	}
}

void solveqr() {
	int h, h_, i;

	for (h = 0; h < m; h++)
		ww[h] = tt_[h];
	for (i = 0; i < n; i++)
		ww[m + i] = xx_[qq[i]] - xx_[i];
	for (h = 0; h < m_; h++)
		hh[h] = h;
	sort(hh, 0, m_);
	build2();
	for (i = 0; i < n; i++)
		update2(i, qq[i], cc[i], (ull) xx_[i] * cc[i]);
	for (h = 0; h < m_; h++) {
		h_ = hh[h];
		if (h_ >= m) {
			i = h_ - m;
			update2(i, qq[i], -cc[i], (ull) (xx_[qq[i]] - xx_[i]) * cc[i]);
		} else
			ans[h_] += query2(ll_[h_], rr_[h_], rr_[h_], tt_[h_]);
	}
}

void solveqr_() {
	int h, h_, i;

	for (h = 0; h < m; h++)
		ww[h] = tt_[h];
	for (i = 0; i < n; i++)
		ww[m + i] = pp[i] == -1 ? INF : xx_[qq[i]] - xx_[pp[i]];
	for (h = 0; h < m_; h++)
		hh[h] = h;
	sort(hh, 0, m_);
	build2();
	for (h = 0; h < m_; h++) {
		h_ = hh[h];
		if (h_ >= m) {
			i = h_ - m;
			if (pp[i] != -1)
				update2(pp[i], qq[i], -cc[i], (ull) (xx_[qq[i]] - xx_[pp[i]]) * cc[i]);
		} else
			ans[h_] -= query2(ll_[h_], rr_[h_], rr_[h_], tt_[h_]);
	}
}

int main() {
	static int qu[N];
	int cnt, h, i;

	scanf("%d%d", &n, &m), m_ = m + n;
	for (i = 0; i < n; i++) {
		scanf("%d", &xx[i]);
		xx_[i + 1] = xx_[i] + xx[i];
	}
	build1(xx, n);
	for (i = 0; i < n; i++)
		scanf("%d", &cc[i]);
	cnt = 0;
	for (i = 0; i < n; i++) {
		while (cnt && cc[qu[cnt - 1]] > cc[i])
			cnt--;
		pp[i] = cnt == 0 ? -1 : qu[cnt - 1];
		qu[cnt++] = i;
	}
	cnt = 0;
	for (i = n - 1; i >= 0; i--) {
		while (cnt && cc[qu[cnt - 1]] >= cc[i])
			cnt--;
		qq[i] = cnt == 0 ? n : qu[cnt - 1];
		qu[cnt++] = i;
	}
	for (h = 0; h < m; h++)
		scanf("%d%d%d", &ll_[h], &rr_[h], &tt_[h]), ll_[h]--, rr_[h]--;
#if 0
	for (h = 0; h < m; h++)
		for (i = ll_[h]; i < rr_[h]; i++) {
			long long xl = pp[i] < ll_[h] ? xx_[i] : max(xx_[i], xx_[pp[i]] + tt_[h]);
			long long xr = min(xx_[i] + tt_[h], qq[i] <= rr_[h] ? xx_[qq[i]] : xx_[rr_[h]]);

			ans[h] -= xl * cc[i];
			if (qq[i] <= rr_[h]) {
				ans[h] += xr * cc[i];
				if (xl >= xr)
					ans[h] -= (xr - xl) * cc[i];
			}
		}
#else
	xx1[0] = 0;
	for (i = 0; i < n; i++)
		xx1[i + 1] = xx1[i] + (ull) xx_[i] * cc[i];
	solvel();
	solveqr();
	solveqr_();
#endif
	for (h = 0; h < m; h++)
		if (query1(ll_[h], rr_[h] - 1) <= tt_[h])
			printf("%llu\n", ans[h]);
		else
			printf("-1\n");
	return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:245:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  245 |  scanf("%d%d", &n, &m), m_ = m + n;
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:247:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  247 |   scanf("%d", &xx[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:252:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  252 |   scanf("%d", &cc[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:268:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  268 |   scanf("%d%d%d", &ll_[h], &rr_[h], &tt_[h]), ll_[h]--, rr_[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 37 ms 2516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1298 ms 45572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4083 ms 172412 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 37 ms 2516 KB Output isn't correct
2 Halted 0 ms 0 KB -