跳到正文

题解

题解:P12194 [NOISG 2025 Prelim] Snacks

I_LOVE_FG约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月11日
篇幅
约 7 分钟
题目
P12194

版本

  1. v1当前

solution

诶这题怎么是黄?果然我只会数据结构。赛时写了个线段树。那就展示一下这个丑陋的做法吧。

看完题一眼权值线段树。所谓权值线段树并没有这么高级,其实就是把线段树当桶用,然后可以查询区间 [l,r][l,r] 里面总共有几个数。

于是想到维护一棵有区间值的和以及区间内数的总数,支持区间赋 00,单点修改的权值线段树就好了。对于操作 11 直接调用区间赋 00 操作,对于操作 22 直接单点修改即可。

注意区间赋 00 时应该记录下被覆盖掉的数有多少个,以得到在单点修改的时候应加的权值(实际上就是记录吃掉的小吃的个数)。

数据范围有点大那就离散化一下。离散化之后要记录离散化后的数对应的原数是多少,否则线段树内记录的区间和都是乘了离散化后的权值,而这样是不对的。

然后赛时 #112 出锅了导致直接卡了 30min。

code

C++
#include<bits/stdc++.h>

#define int long long

namespace IO {
	inline int read() {
		int ret = 0, f = 1;char ch = getchar();
		while (ch < '0' || ch > '9') {
			if (ch == '-') f = -f;
			ch = getchar();
		} 
		while (ch >= '0' && ch <= '9') {
			ret = (ret << 1) + (ret << 3) + (ch ^ 48);
			ch = getchar();
		}
		return ret * f;
	}
	void write(int x) {
		if (x < 0) putchar('-'), x = -x;
		if (x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}

using namespace IO;
using namespace std;
typedef long long ll;

const int maxn = 5e5 + 5;

int a[maxn << 2], sum[maxn * 4], L[maxn * 4], R[maxn * 4], lazy[maxn * 4], num[maxn << 2];
int n, m;

int X[maxn << 2], Y[maxn << 2], Val[maxn << 2], u[maxn << 2], tot, t[maxn << 2], b[maxn << 2];

int len(int x) {
	return R[x] - L[x] + 1;
}

void pushup(int x) {
	sum[x] = sum[x << 1] + sum[x << 1 | 1];
	num[x] = num[x << 1] + num[x << 1 | 1];
}

void pushdown(int x) {
	if (lazy[x]) {
		lazy[x << 1] = lazy[x << 1 | 1] = 1;
		sum[x << 1] = sum[x << 1 | 1] = 0;
		num[x << 1] = num[x << 1 | 1] = 0;
		lazy[x] = 0;
	}
}

void build(int l, int r, int x) {
	L[x] = l, R[x] = r;
	if (l == r) {
		sum[x] = b[l] * t[l];
		num[x] = t[l];
//		cout << l << ' ' << sum[x] << ' ' << num[x] << '\n';
		return;
	}
	int mid = l + ((r - l) >> 1);
	build(l, mid, x << 1);
	build(mid + 1, r, x << 1 | 1);
	pushup(x);
}

void update(int pos, int d, int x) {
	if (L[x] == R[x] && R[x] == pos) {
		sum[x] += b[pos] * d;num[x] += d;
		return;
	}
	pushdown(x);
	int mid = L[x] + ((R[x] - L[x]) >> 1);
	if (pos <= mid) update(pos, d, x << 1);
	else update(pos, d, x << 1 | 1);
	pushup(x);
}

int update_ze(int l, int r, int x) {
	int ans = 0;
	if (L[x] >= l && R[x] <= r) {
		lazy[x] = 1;ans = num[x];
//		cout << ans << "nailong\n";
		sum[x] = 0;num[x] = 0;
		return ans;
	}
	pushdown(x);
	int mid = L[x] + R[x] >> 1;
	if (l <= mid) ans += update_ze(l, r, x << 1);
	if (r > mid) ans += update_ze(l, r, x << 1 | 1);
	pushup(x); 
	return ans;
}

signed main() {
	n = read(), m = read();
	for (int i = 1;i <= n;i++) a[i] = read(), u[++tot] = a[i];
	
	for (int i = 1;i <= m;i++) X[i] = read(), Y[i] = read(), Val[i] = read(), u[++tot] = X[i], u[++tot] = Y[i], u[++tot] = Val[i];
	
	sort(u + 1, u + 1 + tot);
	tot = unique(u + 1, u + 1 + tot) - u - 1;
	
	int mi;
	for (int i = 1;i <= n;i++) {
		mi = a[i];
		a[i] = lower_bound(u + 1, u + tot + 1, a[i]) - u;b[a[i]] = mi;
		t[a[i]]++;
	}
	for (int i = 1;i <= m;i++) {
		mi = X[i];
		X[i] = lower_bound(u + 1, u + tot + 1, X[i]) - u;b[X[i]] = mi;mi = Y[i];
		Y[i] = lower_bound(u + 1, u + tot + 1, Y[i]) - u;b[Y[i]] = mi;mi = Val[i];
		Val[i] = lower_bound(u + 1, u + tot + 1, Val[i]) - u;b[Val[i]] = mi;
	}
	
//	for (int i = 1;i <= n;i++) cout << a[i] << ' ';
//	putchar(10);
//	for (int i = 1;i <= m;i++) cout << X[i] << ' ' << Y[i] << ' ' << Val[i] << '\n';
//	
//	for (int i = 1;i <= n;i++) cout << b[a[i]] << ' ';
//	putchar(10);
//	for (int i = 1;i <= m;i++) cout << b[X[i]] << ' ' << b[Y[i]] << ' ' << b[Val[i]] << '\n'; 
	
	build(1, tot, 1);
	write(sum[1]), putchar(10);
	
	for (int i = 1;i <= m;i++) {
		int x = X[i], y = Y[i], k = Val[i], Num = update_ze(x, y, 1);//cout << Num << "nailong\n";
		update(k, Num, 1);
		write(sum[1]), putchar(10);
	}
	return 0;
} 
原文