线段树求条qwq
查看原帖
线段树求条qwq
126972
Kevinx楼主2024/10/31 11:37

rt

#include<bits/stdc++.h>
#define ll long long
#define Mmid ll mid =rc[ls(p)]
#define Mid ll mid =(l+r)>>1
using namespace std;
const int N = 1e6 + 20, M = N<<2, Mod = 998244353;
ll T, n, ans = 0, maxx = 0;
ll x[N<<1], len = 0;//x存端点离散化 
ll f[2][N];
inline ll ls(ll x) {return x<<1;}
inline ll rs(ll x) {return x<<1|1;}

struct px{
	ll l, r, c;
}a[N];
struct node{//线段树使用端点来确定范围而不是数组长度 
	ll lc[M], rc[M], val[M], tag[M];
	//val存dp值,tag存*2的标记 
	
	void build(ll p, ll l, ll r){
		if(l == r) {
			lc[p] = rc[p] = x[l];
			val[p] = 0;
			tag[p] = 1;
			if(x[l] == 0) val[p] = 1;
			return ;
		}
		Mid;
		build(ls(p), l, mid);
		build(rs(p), mid+1, r);
		lc[p] = lc[ls(p)], rc[p] = rc[rs(p)];//!!
		val[p] = (val[ls(p)]+val[rs(p)])%Mod;
		tag[p] = 1;
	}
	void up(ll p) {
		val[p] = (val[ls(p)]+val[rs(p)]) % Mod;
	}
//	void downmul(ll p) {
//		tim[ls(p)] = (tim[ls(p)]*tag[p])%Mod;
//		tim[rs(p)] = (tim[rs(p)]*tag[p])%Mod;
//		tag[ls(p)] = (tag[ls(p)]*tag[p])%Mod;
//		tag[rs(p)] = (tag[rs(p)]*tag[p])%Mod;
//		tag[p] = 1;
//	}
	void down(ll p) {
		val[ls(p)] = (val[ls(p)]*tag[p])%Mod;
		val[rs(p)] = (val[rs(p)]*tag[p])%Mod;
		tag[ls(p)] = (tag[ls(p)]*tag[p])%Mod;
		tag[rs(p)] = (tag[rs(p)]*tag[p])%Mod;
		tag[p] = 1;
	}
	
	
	void mul(ll p, ll x, ll y) {
		ll l = lc[p], r = rc[p];//!!
		if(x <= l && r <= y) {
			val[p] = (val[p]*2) % Mod;
			tag[p] = (tag[p]*2) % Mod;
			return ;
		}
		down(p);
		Mmid;
		if(x <= mid) mul(ls(p), x, y);
		if(y >  mid) mul(rs(p), x, y);
		up(p);
	}
	
	
	void add(ll p, ll x, ll k) {
		ll l = lc[p], r = rc[p];//!!
		if(l == r) {
			val[p] = (val[p]+k)%Mod;
			return ;
		}
		down(p);
		Mmid;
		if(x <= mid) add(ls(p), x, k);
		if(x >  mid) add(rs(p), x, k);
		up(p);
	}
	
	ll query(ll p, ll x, ll y) {
		ll l = lc[p], r = rc[p];//!! 
		if(x <= l && r <= y) {
			return val[p];
		}
		down(p);
		Mmid;
		ll res = 0;
		if(x <= mid) res = (res+query(ls(p), x, y))%Mod;
		if(y >  mid) res = (res+query(rs(p), x, y))%Mod;
		up(p);
		return res;
	}
	
}tree0, tree1;

bool cmp(px a, px b) {
	return a.r < b.r;
}
int main(){
	scanf("%lld", &T);
	while(T--) {
		ll ans = 0;
		scanf("%lld", &n);
		for(int i = 1; i <= n; i++) {
			cin >> a[i].l >> a[i].r >> a[i].c;
			x[i] = a[i].l, x[i+n] = a[i].r;
		}
		ll cnt = 2*n+1;
		x[2*n+1] = 0;
		sort(x+1, x+cnt+1);
		len = unique(x+1, x+cnt+1)-x-1;
		
		tree0.build(1, 1, len);
		tree1.build(1, 1, len);
		
		sort(a+1, a+n+1, cmp);
		for(int i = 1; i <= n; i++) {
			ll sum = 0;
			if(a[i].c == 1) sum = tree0.query(1, 0, a[i].l-1), tree1.add(1, a[i].r, sum), tree0.mul(1, 0, a[i].l-1);
			if(a[i].c == 0) sum = tree1.query(1, 0, a[i].l-1), tree0.add(1, a[i].r, sum), tree1.mul(1, 0, a[i].l-1);
			ans = (ans+sum)%Mod;
		}
		ans = (ans+1)%Mod;
		printf("%lld\n", ans);
	}
	
	return 0;
}  

2024/10/31 11:37
加载中...