跳到正文

个人记录

单侧递归线段树模板

c2105cxy约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年5月7日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

1.前缀最小值类型P4198 楼房重建

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ls (p*2)
#define rs (p*2+1)
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
inline void write(int x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9)write(x/10);
	putchar((x%10)+'0');
}

const int N=1e5+10,inf=1e8+10;
const double eps=1e-10;


int n,m;

struct Segment_Tree{

	struct tree{
		int l,r;
		double k_max;
		int sum;
	}tr[4*N];

	int calc(int p,double Max){
		//cout<<"MAX "<<Max<<endl;
		//cout<<"cccdddddddddddddddddd "<<tr[p].l<<' '<<tr[p].r<<' '<<tr[p].k_max<<' '<<Max<<endl;
		if(tr[p].l==tr[p].r){
			//cout<<"check "<<tr[p].k_max<<' '<<Max<<endl;
			if((tr[p].k_max-Max)>eps)return 1;
			//cout<<tr[p].k_max-Max<<"error\n";
			return 0;
		}
		if((tr[p].k_max-Max)>eps);
		else return 0;
		tree L=tr[ls],R=tr[rs];
		int res=0;
		//cout<<"check "<<L.l<<' '<<L.r<<' '<<tr[ls].k_max<<' '<<Max<<endl;
		if(L.k_max-Max>eps)res=tr[p].sum-L.sum+calc(ls,Max);//////////////////////////////////////////////////////////////////////注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		else res=calc(rs,Max);
		if(res==2){
			//cout<<R.sum<<' '<<calc(ls,Max)<<endl;
		}
		return res;
	}

	void pushup(int p){
		//cout<<"open the new one!!!!!!!\n";
		tr[p].k_max=max(tr[ls].k_max,tr[rs].k_max);
		tr[p].sum=tr[ls].sum+calc(rs,tr[ls].k_max);
		//cout<<tr[ls].l<<' '<<tr[ls].r<<' '<<tr[ls].k_max<<"sadsffffffsafwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n";
		//cout<<"check "<<tr[p].l<<' '<<tr[p].r<<' '<<tr[ls].sum<<' '<<tr[p].sum<<endl;
	}

	void build(int p,int l,int r){
		tr[p].l=l,tr[p].r=r;
		tr[p].k_max=0;
		tr[p].sum=0;
		if(l==r)return;
		int mid=(l+r)>>1;
		build(ls,l,mid);
		build(rs,mid+1,r);
		//pushup(p);
	}

	void modify_add(int p,int x,double k){
		if(tr[p].l==tr[p].r){

			tr[p].k_max=k;
			tr[p].sum=1;
			//cout<<"dfdfdf "<<tr[p].l<<' '<<tr[p].r<<' '<<tr[p].k_max<<endl;
			//cout<<"dasasasasasasasasasasasasasasasasasasasasasasasasasasasas "<<k<<' '<<tr[p].l<<' '<<tr[p].r<<endl;
			return;
		}
		int mid=(tr[p].l+tr[p].r)>>1;
		if(mid>=x)modify_add(ls,x,k);
		else modify_add(rs,x,k);
		pushup(p);
	}

	int query(int p,int ll,int rr){
		if(tr[p].l>=ll&&tr[p].r<=rr)return tr[p].sum;
		int mid=(tr[p].l+tr[p].r)>>1;
		int res=0;
		if(mid>=ll)res+=query(ls,ll,rr);
		if(mid+1<=rr)res+=query(rs,ll,rr);
		return res;
	}


}Tree;

signed main(){

	n=read(),m=read();
	Tree.build(1,1,n);

	for(int i=1;i<=m;i++){
		int x=read(),y=read();
		double k=y*1.0/x;
		//printf("kkk %.8lf\n",k);
		Tree.modify_add(1,x,k);
		//cout<<"ans= "<<i<<' ';
		write(Tree.query(1,1,n));
		putchar('\n');
	}


	return 0;
}
/*
3 4
2 3
3 5
1 1
2 4
*/

注意事项:1.pushup函数中,一种情况(代码有标注)右端点的贡献不能直接R.sumR.sum而是要tr[p].sum−L.sumtr[p].sum-L.sum 2.注意精度

2.括号匹配溢出影响类型 P9130 [USACO23FEB] Hungry Cow P

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
inline void write(int x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9)write(x/10);
	putchar((x%10)+'0');
}

const int N=1e5+10,mod=1e9+7,M=2e14,ni2=(mod+1)/2;

int n;
int tot;
int get(int l,int r){
	//cout<<"get "<<l<<' '<<r<<' '<<((r+l)%mod*(r-l+1)%mod)*ni2%mod<<endl;
	return (r+l)%mod*((r-l+1)%mod)%mod*ni2%mod;
}

int Sum;

struct Segment_Tree{

	struct tree{
		int ls,rs,pl,pr,sum;
		//sum是指pl的权值和
		//pl是没吃到草的天数前缀
		//pr是剩余草向后溢出的后缀
	}tr[40000010];

	int new_node(int p,int l,int r){
		tr[++tot]=tr[p];
		tr[tot].pl=r-l+1;
		tr[tot].sum=get(l,r)%mod;
		tr[tot].pr=0;
		//cout<<"new node!!!!!!!! "<<tot<<' '<<l<<' '<<r<<' '<<tr[tot].pl<<' '<<tr[tot].pr<<' '<<tr[tot].sum<<endl;
		return tot;
	}

	int calc_sum(int p,int l,int r,int x){
		if(!p){
			//cout<<"sdsdsd\n";
			return get(l+x,r);
		}
		if(!x)return tr[p].sum%mod;
		if(tr[p].pl<=x)return 0;
		if(l==r&&!x)return tr[p].sum%mod;

		int res=0;
		tree L=tr[tr[p].ls],R=tr[tr[p].rs];
		int mid=(l+r)>>1;
		if(!tr[p].ls)L.pl=mid-l+1,L.pr=0,L.sum=get(l,mid);
		if(!tr[p].rs)R.pl=r-mid,R.pr=0,R.sum=get(mid+1,r);

		/*if(!tr[p].ls)tr[p].ls=new_node(0,l,mid);
		if(!tr[p].rs)tr[p].rs=new_node(0,mid+1,r);*/


		if(L.pl<=x)res=calc_sum(tr[p].rs,mid+1,r,x+L.pr-L.pl);
		else res=tr[p].sum-L.sum+calc_sum(tr[p].ls,l,mid,x);
		return res%mod;
	}


	void pushup(int p,int l,int r){
		int mid=(l+r)>>1;
		if(!tr[p].ls)tr[p].ls=new_node(0,l,mid);
		if(!tr[p].rs)tr[p].rs=new_node(0,mid+1,r);
		int ls=tr[p].ls,rs=tr[p].rs;
		tr[p].sum=tr[ls].sum+calc_sum(rs,mid+1,r,tr[ls].pr);
		tr[p].pl=tr[ls].pl+max(0ll,tr[rs].pl-tr[ls].pr);
		tr[p].pr=tr[rs].pr+max(0ll,tr[ls].pr-tr[rs].pl);
		tr[p].sum%=mod;
		//cout<<"pushup "<<p<<' '<<l<<' '<<r<<' '<<tr[p].pl<<' '<<tr[p].pr<<' '<<tr[p].sum<<endl;
	}

	int modify_add(int p,int l,int r,int x,int k){
		if(!p)p=new_node(0,l,r);
		if(l==r){
			if(k==0){
				tr[p].sum=l%mod;
				tr[p].pl=1;
				tr[p].pr=0;
				//cout<<"check check check modify  "<<p<<' '<<l<<' '<<r<<' '<<tr[p].pl<<' '<<tr[p].pr<<' '<<tr[p].sum<<endl;
				return p;
			}
			tr[p].sum=0;
			tr[p].pl=0;
			tr[p].pr=k-1;
			//cout<<"check check check modify  "<<p<<' '<<l<<' '<<r<<' '<<tr[p].pl<<' '<<tr[p].pr<<' '<<tr[p].sum<<endl;
			return p;
		}
		int mid=(l+r)>>1;
		if(mid>=x)tr[p].ls=modify_add(tr[p].ls,l,mid,x,k);
		else tr[p].rs=modify_add(tr[p].rs,mid+1,r,x,k);
		pushup(p,l,r);
		//cout<<"check check check modify  "<<p<<' '<<l<<' '<<r<<' '<<tr[p].pl<<' '<<tr[p].pr<<' '<<tr[p].sum<<endl;
		return p;
	}

	int query(){
		//cout<<"query "<<Sum<<' '<<tr[1].sum<<endl;
		return tr[1].sum%mod;
	}

}Tree;

signed main(){


	n=read();
	Tree.new_node(0,1,M);
	Sum=get(1,M);

	for(int i=1;i<=n;i++){
		//cout<<"totototototototot "<<tot<<endl;
		int d,b;
		d=read(),b=read();
		Tree.modify_add(1,1,M,d,b);
		int ans=(Sum-Tree.query())%mod;
		write((ans+mod)%mod);
		putchar('\n');
	}

	return 0;
}
/*

*/

注意事项:1.取模 2.动态开点时,根节点要特别注意 3.关于动态开点,正常情况下没开出来的点是空的,对答案没影响。但是本题的“空区间”是有影响的,这就要求遇到空区间我们要特判或者把它开出来。值得注意的是,如果在pushup函数中开点,空间复杂度是对的(nlogn);但是不能在calc函数中开点

原文