例题1:营业额统计
题意:寻找数列中每个数与前面数的最小差(绝对值)
解法:Splay树寻找前驱和后继
注意:
1.Splay树第一个元素需要手动插入
2.Splay树在插入每一个数后都需要进行伸展操作
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#define eps 1e-8
#define zero(a) fabs(a)<eps
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000000+10;
int n,num,ans;
int ch[maxn][2],tot,pre[maxn],key[maxn],root;
void Rotate(int r,int kind){//down->up->middle也可,只要middle在最后即可
int x=pre[r],y=pre[x];
if(y) ch[y][ch[y][1]==x]=r;//up
pre[r]=pre[x];
ch[x][!kind]=ch[r][kind],pre[ch[r][kind]]=x;//down
ch[r][kind]=x,pre[x]=r;//middle
}
void splay(int r,int goal){
while(pre[r]!=goal){
int x=pre[r];
if(pre[x]==goal) Rotate(r,ch[x][0]==r);
else{
int y=pre[x];
int kind=ch[y][1]==x;
if(ch[x][kind]==r) Rotate(x,!kind),Rotate(r,!kind);//一字型两次旋转先转父节点,再转子节点速度快10倍!
else Rotate(r,kind),Rotate(r,!kind);
}
}
if(!goal) root=r;
}
bool Insert(int x){//去重
int r=root;
if(key[r]==x) return 1;//是根节点
while(ch[r][key[r]<x]){//所处子树有节点存在:往下查询
r=ch[r][key[r]<x];
if(key[r]==x){//找到相等元素,不必插入
splay(r,0);
return 1;
}
}
int kind=key[r]<x;
ch[r][kind]=++tot;
pre[tot]=r,key[tot]=x,ch[tot][0]=ch[tot][1]=0;
splay(ch[r][kind],0);
return 0;
}
int getpre(int r){
r=ch[r][0];
if(!r) return -INF;//不存在前驱
while(ch[r][1]) r=ch[r][1];
return key[r];
}
int getnxt(int r){
r=ch[r][1];
if(!r) return INF;
while(ch[r][0]) r=ch[r][0];
return key[r];
}
int main(){
//freopen("a.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
ans=0;
tot=0,root=0;
for(int i=1;i<=n;i++){
if(scanf("%d",&num)==EOF) num=0;
if(i==1){//树中必须有顶点时才能使用Insert函数
root=++tot,pre[tot]=0,key[tot]=num,ch[tot][0]=ch[tot][1]=0;
ans=num;
continue;
}
if(Insert(num)) continue;//已有相同元素
int a=getpre(root),b=getnxt(root);
//printf("%d %d %d\n",num,a,b);
ans+=(min(num-a,b-num));
}
printf("%d\n",ans);
}
return 0;
}
例题2:维修数列
6种操作:
1)在pos后插入tot个数字
2)在pos后删除tot个数字
3)从pos开始翻转tot个数字
4)从pos开始tot个数字均改为c
5)从pos开始询问tot个数字和
6)询问最大连续区间和
解法:每个节点存储一些信息,并在修改和查询时进行update、pushup和pushdown操作
splay区间操作参考资料:http://wenku.baidu.com/view/db0f77661ed9ad51f01df23f.html
#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
#include<stdlib.h>
#include<set>
#define ll long long
#define Key_value ch[ch[root][1]][0]
using namespace std;
const int maxn = 500010;
const int INF = 0x3f3f3f3f;
int pre[maxn],ch[maxn][2],key[maxn];//节点信息
int size[maxn],sum[maxn];//子树信息
int lx[maxn],rx[maxn],mx[maxn];//最大连续区间信息
int same[maxn],rev[maxn];//标记信息
int root,tot1;
int s[maxn],tot2;
int a[maxn],n,q;
void Update_Same(int r,int v){//更新该节点和标记
if(!r) return;
key[r]=v;
sum[r]=v*size[r];
lx[r]=rx[r]=mx[r]=max(v,v*size[r]);//对于相等元素区间,最大值要么全选,要么取其一
same[r]=1;
}
void Update_Rev(int r){//更新该节点和标记
if(!r) return;
swap(ch[r][0],ch[r][1]),swap(lx[r],rx[r]);
rev[r]^=1;
}
void Push_Up(int r){//自底向上更新“子树信息"和"最大连续区间信息"
int lson=ch[r][0],rson=ch[r][1];
//1更新子树信息
size[r]=size[lson]+size[rson]+1;
sum[r]=sum[lson]+sum[rson]+key[r];
//2更新最大连续区间信息
lx[r]=max(lx[lson],sum[lson]+key[r]+max(0,lx[rson]));
rx[r]=max(rx[rson],sum[rson]+key[r]+max(0,rx[lson]));
mx[r]=max(0,rx[lson])+key[r]+max(0,lx[rson]);
mx[r]=max(mx[r],max(mx[lson],mx[rson]));
}
void Push_Down(int r){//自上向下传递lazy标记并更新下一层
if(same[r]){
Update_Same(ch[r][0],key[r]);
Update_Same(ch[r][1],key[r]);
same[r]=0;
}
if(rev[r]){
Update_Rev(ch[r][0]);
Update_Rev(ch[r][1]);
rev[r]=0;
}
}
void Newnode(int &r,int father,int k){//在father的r处插入key为k的节点
if(tot2) r=s[tot2--];//从节点池中取点
else r=++tot1;
pre[r]=father;
ch[r][0]=ch[r][1]=rev[r]=same[r]=0;
key[r]=sum[r]=lx[r]=rx[r]=mx[r]=k;
size[r]=1;
}
void Build(int &x,int l,int r,int father){//在father的x处建立[l,r]节点区间
if(l>r) return;
int m=(l+r)>>1;
Newnode(x,father,a[m]);
Build(ch[x][0],l,m-1,x);
Build(ch[x][1],m+1,r,x);
Push_Up(x);
}
void Add(int num){//将区间a[0,num-1]建立在Key_value处
Build(Key_value,0,num-1,ch[root][1]);
}
void Push_end(){
Push_Up(ch[root][1]),Push_Up(root);
}
void Init(){//初始化0节点和两个虚拟节点st和en:root和ch[root][1]; 添加区间a[0,1...n-1]
tot2=0,tot1=-1;
Newnode(root,0,0);
size[root]=0,lx[root]=rx[root]=mx[root]=-INF;//?
Newnode(root,0,-1),Newnode(ch[root][1],root,-1);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
Add(n),Push_end();
}
void Rotate(int r,int kind){
int x=pre[r],y=pre[x];
//1.传递标记
Push_Down(x),Push_Down(r);
//2.旋转
if(y) ch[y][ch[y][1]==x]=r;
pre[r]=y;
ch[x][!kind]=ch[r][kind],pre[ch[r][kind]]=x;
ch[r][kind]=x,pre[x]=r;
//3.更新父节点
Push_Up(x);
}
void Splay(int r,int goal){
for(Push_Down(r);pre[r]!=goal;){
int x=pre[r];
if(pre[x]==goal) Rotate(r,ch[pre[r]][0]==r);
else{
int y=pre[x];
int kind=ch[y][1]==x;
if(ch[x][kind]==r) Rotate(x,!kind),Rotate(r,!kind);
else Rotate(r,kind),Rotate(r,!kind);
}
}
Push_Up(r);
if(goal==0) root=r;
}
int Get_Kth(int r,int k){
Push_Down(r);
int t=size[ch[r][0]]+1;
if(t==k) return r;
if(t>k) return Get_Kth(ch[r][0],k);
else return Get_Kth(ch[r][1],k-t);
}
void Interval(int l,int r){
Splay(Get_Kth(root,l),0),Splay(Get_Kth(root,r),root);
}
void Insert(int pos,int tot){
for(int i=0;i<tot;i++) scanf("%d",&a[i]);
//1.区间伸展
Interval(pos+1,pos+2);
//2.添加子树
Add(tot);
//3.端点更新
Push_end();
}
void erase(int r){//保存删除节点编号,可再生
if(!r) return;
s[++tot2]=r;
erase(ch[r][0]),erase(ch[r][1]);
}
void Delete(int pos,int tot){
//1区间伸展
Interval(pos,pos+tot+1);
//2保存编号
erase(Key_value);
//3删除子树
pre[Key_value]=0,Key_value=0;
//4端点更新
Push_end();
}
void Make_Same(int pos,int tot,int c){
//1.区间伸展
Interval(pos,pos+tot+1);
//2.更新新标记
Update_Same(Key_value,c);
//3.端点更新
Push_end();
}
void Reverse(int pos,int tot){
//1.区间伸展
Interval(pos,pos+tot+1);
//2.填加标记
Update_Rev(Key_value);
//3.端点更新
Push_end();
}
int Get_Sum(int pos,int tot){
//1.区间伸展
Interval(pos,pos+tot+1);
//2.区间求和
return sum[Key_value];
}
int Get_MaxSum(int pos,int tot){
//1.区间伸展
Interval(pos,pos+tot+1);
//2.Get_MaxSum
return mx[Key_value];
}
int main(){
freopen("a.txt","r",stdin);
while(scanf("%d%d",&n,&q)==2){
Init();
char op[20];
int x,y,z;
while(q--){
scanf("%s",op);
if(op[0]=='I'){
scanf("%d%d",&x,&y);
Insert(x,y);
}
else if(op[0]=='D'){
scanf("%d%d",&x,&y);
Delete(x,y);
}
else if(op[0]=='M'&&op[2]=='K'){
scanf("%d%d%d",&x,&y,&z);
Make_Same(x,y,z);
}
else if(op[0]=='R'){
scanf("%d%d",&x,&y);
Reverse(x,y);
}
else if(op[0]=='G'){
scanf("%d%d",&x,&y);
printf("%d\n",Get_Sum(x,y));
}
else printf("%d\n",Get_MaxSum(1,size[root]-2));
}
}
return 0;
}