蒟蒻求助,为1e5的数据毛线段树会TLE呀
查看原帖
蒟蒻求助,为1e5的数据毛线段树会TLE呀
1122916
BLACK11楼主2024/8/29 18:49
#include<bits/stdc++.h>
#define FASTIO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define lson ((p)<<1)
#define rson ((p)<<1|1)
#define ft first
#define sd second
#define pb emplace_back
#define dbg cout << "^^^\n";
#define int long long
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int INF=0x3f3f3f3f;
const int MAX=1e5+20;
int vis[35];
int ans=0;
//struct segment_tree{
	struct{
		int l,r;
		int color=1;   //color-1表杂色
		int lztag=0;  //lztag0表未涂色
	}tr[MAX*4];
	void build(int p,int lo,int ro){
		tr[p].l=lo,tr[p].r=ro,tr[p].color=1,tr[p].lztag=0;
		if(tr[p].l==tr[p].r) return;
		int mid=(lo+ro)>>1;
		build(lson,lo,mid);
		build(rson,mid+1,ro);
	}
	void push_down(int p){
		if(tr[p].lztag==0) return;
		int tag=tr[p].lztag;
		tr[lson].color=tag,tr[lson].lztag=tag;
		tr[rson].color=tag,tr[rson].lztag=tag;
		tr[p].lztag=0;
	}
	void modify(int p,int lo,int ro,int k){
		if(tr[p].l>=lo && tr[p].r<=ro){tr[p].color=k;tr[p].lztag=k;return;}
		push_down(p);
		int ndmid=(tr[p].l+tr[p].r)>>1;
		if(lo<=ndmid) modify(lson,lo,ro,k);
		if(ro>ndmid) modify(rson,lo,ro,k);
		if(tr[lson].color==tr[rson].color) tr[p].color=tr[lson].color;
		else tr[p].color=-1;
	}
	void query(int p,int lo,int ro){
		if(tr[p].color!=-1 && tr[p].l>=lo && tr[p].r<=ro){
			if(!vis[tr[p].color]) ++ans,vis[tr[p].color]=true;
			return;
		}
		push_down(p);
		int ndmid=(tr[p].l+tr[p].r)>>1;
		if(lo<=ndmid) query(lson,lo,ro);
		if(ro>ndmid) query(rson,lo,ro);
	}
//};
//segment_tree sgt;
void solve(){
	int l,t,o;
	cin >> l >> t >> o;
	//sgt.
	build(1,0,l+10);
	while(o--){
		char ch;
		cin >> ch;
		if(ch=='C'){
			int a,b,c;
			cin >> a >> b >> c;
			if(a>b) swap(a,b);
			//sgt.
			modify(1,a,b,c);
		}
		else{
			int a,b;
			cin >> a >> b;
			if(a>b) swap(a,b);
			memset(vis,0,sizeof vis);
			ans=0;
			//sgt.
			query(1,a,b);
			cout << ans << '\n';
		}
		
	}
	return;
}
signed main(){
	FASTIO;
//	int _t;
//	cin >> _t;
//	while(_t--)
		solve();
	return 0;
}

2024/8/29 18:49
加载中...