P12894 [蓝桥杯 2025 国 Java B] 智能交通信号灯

Problem\] \\color{blue}{\\texttt{\[Problem\]}} \[Problem

给定一个长度为 n n n 的数组 a 1 ... n a_{1\dots n} a1...n,进行 m m m 次一下操作:

  1. 给定 l , r l,r l,r,求出 ∑ l ≤ i < j ≤ r mex { a i , a j } \sum\limits_{l \leq i < j\leq r}\text{mex}\{a_{i},a_{j}\} l≤i<j≤r∑mex{ai,aj}。其中 mex { x 1 , x 2 , ... , x n } \text{mex}\{ x_{1},x_{2},\dots,x_{n} \} mex{x1,x2,...,xn} 表示大于等于 1 1 1 的最小的未在 x 1 ... n x_{1 \dots n} x1...n 中出现的整数。
  2. 给定 k , x k,x k,x,修改 a k = x a_{k}=x ak=x。

1 ≤ n , m ≤ 1 × 10 5 , 1 ≤ l < r ≤ n , 1 ≤ k ≤ n , 1 ≤ a i , x ≤ 1 × 10 9 1 \leq n,m \leq 1\times 10^{5}, 1 \leq l < r \leq n, 1 \leq k \leq n, 1\leq a_{i},x \leq 1 \times 10^{9} 1≤n,m≤1×105,1≤l<r≤n,1≤k≤n,1≤ai,x≤1×109。

Analysis\] \\color{blue}{\\texttt{\[Analysis\]}} \[Analysis

其实 mex { x , y } ( x < y ) \text{mex}\{x,y\}(x<y) mex{x,y}(x<y) 的取值无非就是 1 , 2 , 3 1,2,3 1,2,3。

  • 当 x = 1 , y = 2 x=1,y=2 x=1,y=2 时, mex = 3 \text{mex}=3 mex=3。
  • 当 x = 1 , y ≠ 2 x=1,y \not = 2 x=1,y=2 时, mex = 2 \text{mex}=2 mex=2。
  • 否则 mex = 1 \text{mex}=1 mex=1。

这题到这里也就做完了。

开一个树状数组分别统计区间内 1 , 2 1,2 1,2 出现的次数就可以了。

记得开 long long。

Code \color{blue}{\text{Code}} Code

cpp 复制代码
const int N=1e5+100;

class Fenwick_Tree{
	public:
		void set_size(int n){
			this->n=n;
			for(int i=1;i<=n;i++)
				c[i]=0;
		}
		void modify(int x,int val){
			for(int i=x;i<=n;i+=lowbit(i))
				c[i]+=val;
		}
		int query(int x){
			int ans=0;
			for(int i=x;i;i-=lowbit(i))
				ans+=c[i];
			
			return ans;
		}
	private:
		int c[N],n;
		int lowbit(int x){
			return x&(-x);
		}
}cnt[2];

int query(int num,int l,int r){
	if (num<1||num>2) return -1;
	
	--num;
	return cnt[num].query(r)-cnt[num].query(l-1);
}

typedef long long ll;
#define sum(n) (1ll*(n)*((n)-1)/2)
ll query(int l,int r){
	int n=r-l+1;
	int x=query(1,l,r),y=query(2,l,r),z=n-x-y;
	
	return 3ll*x*y+2ll*sum(x)+2ll*x*z+1ll*(sum(n)-1ll*x*y-sum(x)-1ll*x*z);
}

int n,m,a[N];

int main(){
	n=read();m=read();
	
	cnt[0].set_size(n);
	cnt[1].set_size(n);
	
	for(int i=1;i<=n;i++){
		a[i]=read();
		if (a[i]<=2)
			cnt[a[i]-1].modify(i,1);
	}
	
	for(int i=1;i<=m;i++){
		int opt=read(),l=read(),r=read();
		
		if (opt==1) printf("%lld\n",query(l,r));
		else{
			if (a[l]<=2)
				cnt[a[l]-1].modify(l,-1);
			
			a[l]=r;
			if (a[l]<=2)
				cnt[a[l]-1].modify(l,1);
		}
	}
	
	return 0;
}

顺便一说,这题的数据好水啊。犯了一个及其低级的错误,但是还能拿 75 75 75 分。

不过可以理解,毕竟修改操作 a i , x a_{i},x ai,x 都大于等于 3 3 3 时等于没改,如果数据纯随机的话,大部分修改都是没用的。

cpp 复制代码
	for(int i=1;i<=m;i++){
		int opt=read(),l=read(),r=read();
		
		if (opt==1) printf("%lld\n",query(l,r));
		else{
			if (a[l]<=2)
				cnt[a[l]-1].modify(i,-1);
			
			a[l]=r;
			if (a[l]<=2)
				cnt[a[l]-1].modify(i,1);
		}
	}

考验一下大家,能不能超出错误在哪。

相关推荐
郝学胜_神的一滴1 小时前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨18 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4565 天前
C++进阶(1)——前景提要
c++
夜悊5 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴5 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0016 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾6 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you6 天前
constexpr函数
c++
凡人叶枫6 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫6 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++