社区讨论

RE求助,非法内存引用

P9561 [SDCPC 2023] Colorful Segments参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m1zntfzr
此快照首次捕获于
2024/10/08 07:45
去年
此快照最后确认于
2024/10/08 16:20
去年
查看原帖
上面是线段树板子,主程序很短。#2 RE了
CPP
#include <bits/stdc++.h>//RuntimeError
template <typename T>
inline void read(T& aim){
	T num=0,f=1;
	int ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())num=num*10+ch-'0';
	aim=num*f;
}
using namespace std;
typedef long long LL;
const int N=1e5+9,mod=998244353;
const int Sz=1e5*36;
struct Seg{
	int lc[Sz],rc[Sz],tot;
	LL val[Sz],tag[Sz];
	void clear(){
		tot=0;build();
	}
	int build(){
		tot++;
		lc[tot]=rc[tot]=val[tot]=0;
		tag[tot]=1;
		return tot;
	}
	void spread(int t){
		if(tag[t]!=1){
			if(lc[t]){
				tag[lc[t]]=tag[lc[t]]*tag[t]%mod;
				val[lc[t]]=val[lc[t]]*tag[t]%mod;
			}
			if(rc[t]){
				tag[rc[t]]=tag[rc[t]]*tag[t]%mod;
				val[rc[t]]=val[rc[t]]*tag[t]%mod;
			}
			tag[t]=1;
		}
	}
	void add(int t,int a,int b,int p,LL v){
		if(a==b){
			val[t]+=v;val[v]%=mod;
			return;
		}
		spread(t);
		int mid=(a+b)>>1;
		if(p<=mid){
			if(!lc[t])lc[t]=build();
			add(lc[t],a,mid,p,v);
		}else{
			if(!rc[t])rc[t]=build();
			add(rc[t],mid+1,b,p,v);
		}
		val[t]=(val[lc[t]]+val[rc[t]])%mod;
	}
	void mul(int t,int a,int b,int l,int r,LL v){
		if(l<=1&&r>=b){
			val[t]=val[t]*v%mod;
			tag[t]=tag[t]*v%mod;
			return;
		}
		spread(t);
		int mid=(a+b)>>1;
		if(l<=mid){
			if(!lc[t])lc[t]=build();
			mul(lc[t],a,mid,l,r,v);
		}
		if(r>mid){
			if(!rc[t])rc[t]=build();
			mul(rc[t],mid+1,b,l,r,v);
		}
		val[t]=(val[lc[t]]+val[rc[t]])%mod;
	}
	LL query(int t,int a,int b,int l,int r){
		if(l<=a&&r>=b){
			return val[t];
		}
		spread(t);
		int mid=(a+b)>>1;LL res=0;
		if(l<=mid&&lc[t]){
			res+=query(lc[t],a,mid,l,r);
		}
		if(r>mid&&rc[t]){
			res+=query(rc[t],mid+1,b,l,r);
		}
		return res%mod;
	}
}seg0,seg1;
struct Itv{
	int l,r,c;
	friend bool operator<(Itv ea,Itv eb){
		return ea.r<eb.r;
	}
}a[N];

int n;
int main(){
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].c);
		}
		sort(a+1,a+1+n);
		seg0.clear();seg1.clear();
		seg0.add(1,0,1e9,0,1);
		seg1.add(1,0,1e9,0,1);
		LL ans=1;
		for(int i=1;i<=n;i++){
			if(!a[i].c){
				LL res=seg1.query(1,0,1e9,0,a[i].l-1);
				ans=(ans+res)%mod;
				seg0.add(1,0,1e9,a[i].r,res);
				seg1.mul(1,0,1e9,0,a[i].l-1,2);
			}else{
				LL res=seg0.query(1,0,1e9,0,a[i].l-1);
				ans=(ans+res)%mod;
				seg1.add(1,0,1e9,a[i].r,res);
				seg0.mul(1,0,1e9,0,a[i].l-1,2);
			}
		}
		printf("%lld\n",ans);
	}
	
	
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...