跳到正文

个人记录

P3391 【模板】文艺平衡树

queue_prompt约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年11月20日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 7 分钟
获赞
1

版本

  1. v2当前
  2. v1

前言

很明显某入机将将会背模板但完全不会用。

所以连这道【模板】都不会。

再无话说*2

开始吧

正文

关于此题其实不难想到按size拆(类似数列分块入门6)成[1,l−1][1,l-1],[l,r][l,r],[r+1,n][r+1,n]三个区间翻转[l,r][l,r]后再合起来。

那么怎么翻呢?再思考片刻就会发现:直接左右子树递归反转就可以了,这样中序遍历的结果就是原来的回文。

但是明显这样复杂度O(n2logn)O(n^2 logn),比暴力还差。

好了我不会了。

遂查看题解,发现问题所在:我还真就没怎么学线段树和树状数组就直接冲fhq了。

考虑懒标记,因为翻转两次等于没翻,可以用异或下传。

最后全部传下去,解决了还没解决,最后一个问题:何时下传?

线段树显然操作完再传,但是这里不一样。

分裂操作容易导致合并后的左右子树并非元节点的左右子树,所以分裂之前要pushdown一次。

合并操作同理,合并之前把x和y子树都下传。

解决了。

代码:

C++
//#define debug

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
#include<map>
#include<stack>
#include<cstring>
#include<random>

using std::max;
using std::min;
using std::cin;
using std::cout;
using std::sort;
using std::string;
using std::swap;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define lwb lower_bound
#define upb upper_bound

#ifdef debug
#define gc() getchar()
#define pc(x) putchar(x)
#else
char buf[1<<20],*p1=buf,*p2=buf;
char pbuf[1<<20],*pp=pbuf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)? EOF:*p1++)
#define pc(x) (*pp++=x,pp-pbuf==(1<<20)? (fwrite(pbuf,1,1<<20,stdout),pp=pbuf,"mutsumi"):"mortis")
#endif
namespace IO{
	template<class T>
	inline void in(T &x){
		x=0;
		char ch=gc();
		bool f=0;
		for(;!isdigit(ch);ch=gc()) f=ch=='-';
		for(;isdigit(ch);ch=gc()) x=(x<<3)+(x<<1)+ (T)(f? 48-ch:ch-48);
	}
	template<class T>
	inline void out(T x){
		if(!x){pc('0');return;}
		bool f=x<0;
		f? pc('-'),"Soyo0&Anon":"Sakiko&Uika";
		short sta[40],top=0;
		while(x) sta[top++]=x%10,x/=10;
		while(top--) pc(f? (48-sta[top]):(48+sta[top]));
	}
	inline void sin(string &str){
		str.clear();
		char ch;
		while((ch=gc())!=EOF&&isspace(ch));
		for(;ch!=EOF&&!isspace(ch);ch=gc()) str.pb(ch);
	}
	inline void sout(const string &str){for(char ch:str) pc(ch);}
	#ifndef debug
	class Flusher{public: ~Flusher(){fwrite(pbuf,1,pp-pbuf,stdout);}}flusher;
	#endif
}


template<typename T,int SIZE>
class BT{
	public:
		int root=0,cnt=0;
	protected:
		T v[SIZE];
		int lc[SIZE],rc[SIZE],size[SIZE],key[SIZE];
		bool lz[SIZE];
		inline void upd(int now){size[now]=size[lc[now]]+size[rc[now]]+1;}
		inline void pd(int now){
			if(lz[now]) swap(lc[now],rc[now]);
			lz[lc[now]]^=lz[now];
			lz[rc[now]]^=lz[now];
			lz[now]=0;
		}
		int newnode(T val){
			v[++cnt]=val;
			size[cnt]=1;
			key[cnt]=rand();
			return cnt;
		}
		inline void split(int now,int rank,int &x,int &y){
			if(!now){x=y=0;return;}
			pd(now);
			if(size[lc[now]]+1<=rank) x=now,split(rc[now],rank-size[lc[now]]-1,rc[now],y);
			else y=now,split(lc[now],rank,x,lc[now]);
			upd(now);
		}
		inline int merge(int x,int y){
			if(!x||!y) return x+y;
			pd(x),pd(y);
			if(key[x]<key[y]){rc[x]=merge(rc[x],y),upd(x);return x;}
			lc[y]=merge(x,lc[y]),upd(y);return y;
		}
	
	public:
		inline void ins(T val){
			root=merge(root,newnode(val));
		}
		inline void flip(int l,int r){
			int x,y,z;
			split(root,r,x,z);
			split(x,l-1,x,y);
			lz[y]^=1;
			root=merge(merge(x,y),z); 
		}
		inline void dfs(int now){
			if(!now) return;
			if(lz[now]) swap(lc[now],rc[now]);
			lz[lc[now]]^=lz[now];
			lz[rc[now]]^=lz[now];
			lz[now]=1;
			dfs(lc[now]);
			IO::out(v[now]),pc(' ');
			dfs(rc[now]);
		}
		
};

const int N=2e6+5;

BT<int,N>tree;
int n,m,l,r;

class Slove{public:Slove(){
	
//    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	
//	freopen("1.in","r",stdin);
//	freopen("1.out","w",stdout);
	
	srand(0x0d000721);
	
	IO::in(n),IO::in(m);
	for(int i=1;i<=n;i++) tree.ins(i);
	for(int i=1;i<=m;i++) IO::in(l),IO::in(r),tree.flip(l,r);
	tree.dfs(tree.root);
	
}}SLOVE;

int main(){}
嗯怎么说

显然,经过一段时间的锻炼,我的代码水平得到了0到0的提升,可读性得到了-INT_MAX到-LLONG_MAX的下降。

原文