传送门
文章目录
- 题意:
- 思路:
题意:
思路:
本来是不想写数据结构了,因为明天打蓝桥了,想放松一下,但是看到这个题感觉写起来挺简单的,就试了试,结果…
首先看一下他的操作,1,2,3,51,2,3,51,2,3,5就是裸的平衡树操作,444需要用一个数组判断是否删,没有删的话再查排名即可,注意查之前需要清空所有父亲的lazylazylazy标记!!!这个非常重要。对于操作555我们状压一下,只有262626个,所以一个intintint就能存下来,直接把区间拿出来,让后查询有多少个111就好了。
坑点:
(1)(1)(1)细节上的失误,比如tr[u].rtr[u].rtr[u].r写成了tr[u].ltr[u].ltr[u].l,比如应该分裂yyy我给写成了分裂rootrootroot,以上两点分别wawawa了两页嘤嘤嘤。
(2)(2)(2)在findpfindpfindp函数里面没有提前updownupdownupdown,即清空父亲的懒标记,导致错误。注意这个题一定是要先清空再跳父亲,因为他当前的父亲可能有懒标记导致当前这个点从左儿子变成右儿子,或者右儿子变成左儿子!
蒟蒻的代码:
// Problem: P5217 贫穷
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5217
// Memory Limit: 125 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<random>
#include<sstream>
#include<chrono>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=400010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m,root,tot,x,y,z;
char s[N];
bool st[N];
struct Node {int l,r;int rank,size,state,fa;int lazy,id;
}tr[N];int newnode(int v) {int u=++tot;tr[u].l=tr[u].r=0;tr[u].lazy=0; tr[u].fa=0;tr[u].rank=rand();tr[u].id=v;tr[u].size=1;tr[u].state=1<<v;return u;
}void pushup(int u) {tr[u].size=tr[tr[u].l].size+tr[tr[u].r].size+1;tr[u].state=1<<tr[u].id;tr[u].state|=tr[tr[u].l].state,tr[tr[u].l].fa=u;tr[u].state|=tr[tr[u].r].state,tr[tr[u].r].fa=u;
}void pushdown(int u) {if(tr[u].lazy) {tr[u].lazy=0;swap(tr[u].l,tr[u].r);tr[tr[u].l].lazy^=1;tr[tr[u].r].lazy^=1;}
}void split(int u,int k,int &x,int &y) {if(!u) { x=y=0; return; }pushdown(u);if(k<=tr[tr[u].l].size) y=u,split(tr[u].l,k,x,tr[u].l),tr[x].fa=0;else x=u,split(tr[u].r,k-tr[tr[u].l].size-1,tr[u].r,y),tr[y].fa=0;pushup(u);
}int merge(int u,int v) {if(!u||!v) return u+v;if(tr[u].rank<tr[v].rank) {pushdown(u);tr[u].r=merge(tr[u].r,v);pushup(u);return u;} else {pushdown(v);tr[v].l=merge(u,tr[v].l);pushup(v);return v;}
}void updown(int u) {if(tr[u].fa) updown(tr[u].fa);pushdown(u);
} int findp(int u) {updown(u);int ans=tr[u].size-tr[tr[u].r].size;while(tr[u].fa) {if(u==tr[tr[u].fa].r) ans+=tr[tr[u].fa].size-tr[u].size;u=tr[u].fa;}return ans;
}void dfs(int u) {if(!u) return;pushdown(u);dfs(tr[u].l);//printf("%c",tr[u].id+'a');dfs(tr[u].r);
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0); scanf("%d%d%s",&n,&m,s+1);for(int i=1;i<=n;i++) {root=merge(root,newnode(s[i]-'a'));}while(m--) {char op[2]; int a; scanf("%s%d",op,&a);if(op[0]=='I') {char c[2]; scanf("%s",c);split(root,a,x,y); root=merge(merge(x,newnode(c[0]-'a')),y);} else if(op[0]=='D') {split(root,a,x,z);split(x,a-1,x,y);root=merge(x,z); st[y]=1;} else if(op[0]=='R') {int b; scanf("%d",&b);split(root,b,x,z);split(x,a-1,x,y);tr[y].lazy^=1;root=merge(merge(x,y),z);} else if(op[0]=='P') {if(st[a]) puts("0");else printf("%d\n",findp(a));} else if(op[0]=='T') {split(root,a,x,z);split(x,a-1,x,y);printf("%c\n",tr[y].id+'a');root=merge(merge(x,y),z); } else {int b; scanf("%d",&b);split(root,b,x,z);split(x,a-1,x,y);printf("%d\n",__builtin_popcount(tr[y].state));root=merge(merge(x,y),z);}}return 0;
}
/**/