码迷,mamicode.com
首页 > 其他好文 > 详细

博客开更!!

时间:2016-02-25 09:12:52      阅读:447      评论:0      收藏:0      [点我收藏+]

标签:

好颓啊!!  做题好慢!! 各种错误!!

1014: [JSOI2008]火星人prefix

额 splay没啥说的 字符串hash蒟蒻不会啊 搞半天才懂 好玄学啊 unsigned的自然溢出也是玄学啊 (话说我 int的自然溢出也A了smg??

技术分享
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstring>
  5 using namespace std;
  6 #define MAXN 150000+100
  7 #define MO 9875321
  8 int root,n,m,base[MAXN],hash[MAXN],num[MAXN],to[MAXN][2],fa[MAXN],size[MAXN],tot=0;
  9 char s[MAXN];
 10 void Up(int x)
 11 {
 12     int L=to[x][0],R=to[x][1];
 13     size[x]=size[L]+size[R]+1;
 14     hash[x]=hash[L]+num[x]*base[size[L]]+hash[R]*base[size[L]+1];
 15 }
 16 void Rotate(int &rt,int x)
 17 {
 18     int y=fa[x],z=fa[y];
 19     if(y!=rt) to[z][to[z][1]==y]=x;
 20     else rt=x;
 21     fa[x]=z;fa[y]=x;
 22     int c=to[y][1]==x;fa[to[x][c^1]]=y;
 23     to[y][c]=to[x][c^1];to[x][c^1]=y;
 24       
 25     Up(y);Up(x);
 26 }
 27 void Splay(int &rt,int x)
 28 {
 29     while(x!=rt)
 30     {
 31         if(fa[x]!=rt) Rotate(rt,fa[x]);
 32         Rotate(rt,x);
 33     }
 34 }
 35 int Find(int rt,int rank)
 36 {
 37     if(size[to[rt][0]]+1==rank) return rt;
 38     if(size[to[rt][0]]>=rank) return Find(to[rt][0],rank);
 39     return Find(to[rt][1],rank-size[to[rt][0]]-1);
 40 }
 41 void vist(int rt)
 42 {
 43     if(rt==0) return ;
 44     cout<<rt<< <<to[rt][0]<< <<to[rt][1]<< <<size[rt]<<endl;
 45     vist(to[rt][0]);vist(to[rt][1]);
 46 }
 47 int Do(int x,int lenth)
 48 {
 49     int L=Find(root,x-1),R=Find(root,x+lenth);
 50     Splay(root,L);Splay(to[L][1],R);
 51     return hash[to[R][0]];
 52 }
 53 int Solve(int x,int y)
 54 {
 55     int L=0,R=min(n-x,n-y)+2;
 56     while(L<R)
 57     {
 58         int mid=(L+R)/2;
 59         if(Do(x,mid+1)==Do(y,mid+1)) L=mid+1;
 60         else R=mid;
 61     }
 62     return L;
 63 }
 64 void Build(int L,int R,int last)
 65 {
 66     if(L>R) return ;
 67     if(L==R)
 68     {
 69         tot++;
 70         hash[tot]=num[tot]=s[L];
 71         fa[tot]=last;size[tot]=1;
 72         return ;
 73     }
 74     int now=++tot;
 75     int mid=(L+R)/2;
 76     if(L<mid) {to[now][0]=tot+1; Build(L,mid-1,now); }
 77     if(mid+1<=R) {to[now][1]=tot+1; Build(mid+1,R,now); }
 78     num[now]=s[mid];fa[now]=last;
 79     Up(now);
 80 }
 81 int main()
 82 {
 83     base[0]=1;for(int i=1;i<=150000+10;i++) base[i]=base[i-1]*27;
 84     scanf("%s",s+2);
 85     n=strlen(s+2);s[1]=s[n+2]=0;
 86     root=tot+1;
 87     Build(1,n+2,0);
 88     cin>>m;
 89     while(m--)
 90     {
 91         getchar();char c=getchar();
 92         if(c==Q)
 93         {
 94             int x,y;scanf("%d %d",&x,&y);
 95             printf("%d\n",Solve(x+1,y+1));
 96         }
 97         else if(c==R)
 98         {
 99             int x;char c;
100             scanf("%d",&x);getchar();c=getchar();
101             x=Find(root,x+1);
102             Splay(root,x);
103             num[x]=c;
104             Up(x);
105         }
106         else
107         {
108             int x;char c;
109             scanf("%d",&x);getchar();c=getchar();
110             x++;int tmp=Find(root,x);
111             Splay(root,tmp);
112             int tmp1=Find(root,x+1);
113             Splay(to[tmp][1],tmp1);
114             to[tmp1][0]=++tot;
115             num[tot]=c;
116             fa[tot]=tmp1;
117             Up(tot);Up(tmp1);Up(tmp);
118             n++;
119         }
120     }
121     return 0;
122 }
BZOJ 1014

3223: Tyvj 1729 文艺平衡树

A过的入门题又调试半天是什么心情~~~

技术分享
#include <cstdio>
#include <iostream>
#define maxn 100100
int n,m,root,ch[maxn][2],size[maxn],fa[maxn];
bool b[maxn];
void Up(int x){    size[x]=size[ch[x][0]]+size[ch[x][1]]+1;}
void Pushdown(int x)
{
    if(!b[x]) return ;
    std::swap(ch[x][0],ch[x][1]);
    b[ch[x][0]]^=1,b[ch[x][1]]^=1;
    b[x]=false;
}
void Rotate(int &k,int x)
{
    int y=fa[x],z=fa[y];
    if(y==k) k=x; else ch[z][ch[z][1]==y]=x;
    int c=ch[y][1]==x;
    fa[x]=z;fa[y]=x;fa[ch[x][c^1]]=y;
    ch[y][c]=ch[x][c^1];ch[x][c^1]=y;
    Up(y);Up(x);
}
void Splay(int &k,int x)
{
    while(x!=k)
    {
        if(fa[x]!=k) Rotate(k,fa[x]);
        Rotate(k,x);    
    }
}
int Find(int now,int x)
{
    Pushdown(now);
    int k=size[ch[now][0]]+1;
    if(k==x) return now;
    if(x>k) return Find(ch[now][1],x-k);
    if(x<k) return Find(ch[now][0],x);
}
void F(int x,int y)
{
    int L=Find(root,x),R=Find(root,y);
    Splay(root,L),Splay(ch[L][1],R);
    b[ch[R][0]]^=1;
}
void Dfs(int now)
{
    if(!now) return ;
    Pushdown(now);
    Dfs(ch[now][0]);
    if(now-1>=1&&now-1<=n)  printf("%d ",now-1);
    Dfs(ch[now][1]);
}
int main()
{
    scanf("%d %d",&n,&m);root=n+2;
    for(int i=1;i<=n+2;i++)
        ch[i][0]=i-1,size[i]=i,fa[i]=i+1;
    for(int x,y,i=1;i<=m;i++)
        scanf("%d %d",&x,&y),F(x,y+2);
    Dfs(root);
    return 0;
}
BZOJ 3223

3224: Tyvj 1728 普通平衡树

A过的入门题又调试半天是什么心情~~~

技术分享
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstdlib>
  4 using namespace std;
  5 #define maxn 100000*2+1000
  6 #define INF 1000000000
  7 int n,fa[maxn],ch[maxn][2],key[maxn],size[maxn],root,cnt;
  8 void Up(int x)
  9 {
 10     size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
 11 }
 12 void Rotate(int x)
 13 {
 14     int y=fa[x],z=fa[y];
 15     if(y==root) root=x;
 16     else
 17         ch[z][ ch[z][1]==y ]=x;
 18     int c=ch[y][1]==x;
 19     fa[x]=z;fa[y]=x;fa[ch[x][c^1]]=y;
 20     ch[y][c]=ch[x][c^1];ch[x][c^1]=y;
 21 
 22     Up(y),Up(x);Up(z); size[0]=0;
 23 }
 24 void Splay(int x)
 25 {
 26     while(x!=root)
 27     {
 28         if(fa[x]!=root) Rotate(fa[x]);
 29         Rotate(x);
 30     }
 31 }
 32 void Ins(int &now,int x,int last)
 33 {
 34     if(now==0){now=++cnt;key[now]=x;fa[now]=last;size[now]=1;Splay(now);}
 35     else if(key[now]<x) Ins(ch[now][1],x,now); else Ins(ch[now][0],x,now);
 36 }
 37 int loc;
 38 void Find(int now,int x)
 39 {
 40     if(now==0) return ;
 41     if(key[now]==x) loc=now;
 42     if(key[now]<=x) Find(ch[now][1],x);
 43     else Find(ch[now][0],x);
 44 }
 45 void Del(int x)
 46 {
 47     Find(root,x);
 48     Splay(loc);
 49     if(ch[loc][0]*ch[loc][1]==0) root=ch[loc][0]+ch[loc][1];
 50     else 
 51     {
 52         root=ch[loc][0];
 53         int tmp=ch[loc][0];
 54         while(ch[tmp][1]!=0)tmp=ch[tmp][1];
 55         ch[tmp][1]=ch[loc][1];fa[ch[loc][1]]=tmp;
 56     }
 57 }
 58 int ans_3;
 59 void F_3(int now,int x)
 60 {
 61     if(now==0) return ;
 62     if(x<=key[now]) F_3(ch[now][0],x);
 63     else ans_3+=size[ch[now][0]]+1,F_3(ch[now][1],x);
 64 }
 65 int ans_4;
 66 void F_4(int now,int k,int sum)
 67 {
 68     if(now==0) return ;
 69     sum+=size[ch[now][0]]+1;
 70     if(sum==k){ ans_4=key[now];return;}
 71     if(k<=sum) F_4(ch[now][0],k,sum-size[ch[now][0]]-1);
 72     else F_4(ch[now][1],k,sum);
 73 }
 74 int ans_5;// qianqu
 75 void F_5(int now,int x)
 76 {
 77     if(now==0) return ;
 78     if(key[now]<x) ans_5=key[now];
 79     if(key[now]<x) F_5(ch[now][1],x);
 80     else F_5(ch[now][0],x);
 81 }
 82 int ans_6;//houji
 83 void F_6(int now,int x)
 84 {
 85     if(now==0) return ;
 86     if(key[now]>x) ans_6=key[now];
 87     if(key[now]>x) F_6(ch[now][0],x);
 88     else F_6(ch[now][1],x);
 89 }
 90 int main()
 91 {
 92     scanf("%d",&n);
 93     while(n--)
 94     {
 95         int x,y;
 96         scanf("%d %d",&x,&y);
 97         switch(x)
 98         {
 99             case 1:Ins(root,y,root);break;
100             case 2:Del(y);break;
101             case 3:ans_3=1; 
102                    F_3(root,y);
103                    printf("%d\n",ans_3);
104                    break;
105             case 4:ans_4=0;
106                    F_4(root,y,0);
107                    printf("%d\n",ans_4);
108                    break;
109             case 5:F_5(root,y);
110                    printf("%d\n",ans_5);
111                    break;
112             case 6:F_6(root,y);
113                    printf("%d\n",ans_6);
114                    break;      
115         }
116     }
117     return 0;
118 }
BZOJ 3224

1251: 序列终结者

区间操作烦死人啊!!! 注意各种标记的下传时间!! 

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define MAXN 100000
 7 int tot,root,n,m,fa[MAXN],size[MAXN],num[MAXN],to[MAXN][2],add[MAXN],mmax[MAXN];
 8 bool mark[MAXN];
 9 void Up(int x)
10 {
11     int l=to[x][0],r=to[x][1];
12     size[x]=size[to[x][0]]+size[to[x][1]]+1;
13     mmax[x]=max(num[x],max(mmax[l],mmax[r]));
14 }
15 void P(int x) //Pushdown()
16 {
17     if(x==0) return ;
18     int l=to[x][0],r=to[x][1],tmp=add[x];
19     if(mark[x]) swap(to[x][0],to[x][1]),mark[l]^=1,mark[r]^=1,mark[x]=false;
20     add[l]+=tmp;add[r]+=tmp;
21     num[l]+=tmp;num[r]+=tmp;
22     mmax[l]+=tmp;mmax[r]+=tmp;
23     add[x]=0,mmax[0]=-100000000;
24 }
25 void Rotate(int x,int &rt)
26 {
27     int y=fa[x],z=fa[y];
28     P(y);P(x);
29     if(y==rt) rt=x;
30     else to[z][to[z][1]==y]=x;
31     int c=to[y][1]==x;
32     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
33     to[y][c]=to[x][c^1];to[x][c^1]=y;
34     Up(y);Up(x);
35 }
36 void Splay(int x,int &rt){while(x!=rt){if(fa[x]!=rt)Rotate(fa[x],rt);
37 Rotate(x,rt);}}
38 int Find(int rank,int rt)
39 {
40     P(rt);
41     int tmp=size[to[rt][0]]+1;
42     if(tmp==rank)return rt;
43     if(tmp>rank) return Find(rank,to[rt][0]);
44     return Find(rank-tmp,to[rt][1]);
45 }
46 int F(int l,int r)
47 {   
48     l=Find(l,root),r=Find(r,root);
49     Splay(l,root);Splay(r,to[l][1]);
50     return to[r][0];
51 }
52 void Build(int lenth)
53 {
54     tot++;size[tot]=1;
55     for(int i=2;i<=lenth;i++)
56     {
57         tot++;
58         size[tot]=size[tot-1]+1;
59         fa[tot-1]=tot;
60         to[tot][0]=tot-1;
61     }
62     root=tot;
63 }
64 int main()
65 {
66     scanf("%d %d",&n,&m);
67     Build(n+2);
68     int opt,l,r,x,tmp;
69     while(m--)
70     {
71         scanf("%d %d %d",&opt,&l,&r);
72         switch (opt)
73         {
74             case 1:scanf("%d",&x);tmp=F(l,r+2);num[tmp]+=x;add[tmp]+=x;mmax[tmp]+=x;break;
75             case 2:mark[F(l,r+2)]^=1;break;
76             case 3:printf("%d\n",mmax[F(l,r+2)]);break;
77         }
78     }
79     return 0;
80 }
BZOJ 1251

1503: [NOI2004]郁闷的出纳员

直接搞就行

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 #define MAXN 100000+10
 5 int tot,ans,nn,n,m,mmin,root,fa[MAXN],size[MAXN],to[MAXN][2],num[MAXN],add[MAXN];
 6 void Up(int x){size[x]=size[to[x][0]]+size[to[x][1]]+1;}
 7 void Rotate(int &rt,int x)
 8 {
 9     int y=fa[x],z=fa[y];
10     if(y==rt) rt=x;
11     else to[z][to[z][1]==y]=x;
12     int c=to[y][1]==x;
13     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
14     to[y][c]=to[x][c^1];to[x][c^1]=y;
15     Up(x);Up(y);
16 }
17 void Splay(int &rt,int x){while(rt!=x){if(fa[x]!=rt)Rotate(rt,fa[x]);Rotate(rt,x);}}
18 void Pushdown(int x)
19 {
20     if(add[x]==0) return ;
21     int l=to[x][0],r=to[x][1],tmp=add[x];
22     num[l]+=tmp;num[r]+=tmp;
23     add[l]+=tmp;add[r]+=tmp;
24     add[x]=0;add[0]=0;num[0]=0;
25 }
26 int Rank(int rt,int rk)
27 {
28     Pushdown(rt);
29     int tmp=size[to[rt][0]]+1;
30     if(tmp==rk)return num[rt];
31     if(tmp<rk)return Rank(to[rt][1],rk-tmp);
32     return Rank(to[rt][0],rk);
33 }
34 void Find(int rt)
35 {
36     if(rt==0) return ;
37     Pushdown(rt);
38     if(num[rt]<0) ans=rt,Find(to[rt][1]);
39     else Find(to[rt][0]);
40 }
41 void Delate()
42 {
43     ans=0;Find(root);
44     if(ans==0)return ;
45     Splay(root,ans);n-=size[to[root][0]]+1;nn+=size[to[root][0]]+1;root=to[root][1];
46 }
47 void Ins(int &rt,int x,int last)
48 {
49     if(rt==0){n++;tot++;rt=tot;fa[rt]=last;size[rt]=1;num[rt]=x;Splay(root,rt);return ;}
50     Pushdown(rt);
51     if(num[rt]<x) Ins(to[rt][1],x,rt);
52     else Ins(to[rt][0],x,rt);
53 }
54 int main()
55 {
56     scanf("%d %d",&m,&mmin);
57     char c;int x;
58     while(m--)
59     {
60         getchar();c=getchar();scanf("%d",&x);
61         switch(c)
62         {
63             case I:if(mmin<=x) Ins(root,x-mmin,root);break;
64             case A:num[root]+=x;add[root]+=x;break;
65             case S:num[root]-=x;add[root]-=x;Delate();break;
66             case F:if(x>n) ans=-1;else ans=Rank(root,n-x+1)+mmin;printf("%d\n",ans);break;
67         }
68     }
69     printf("%d\n",nn);
70     return 0;
71 }
BZOJ 1503

3506: [Cqoi2014]排序机械臂

这个比较水 可以自己想的 而且双倍经验很爽(hah 另一个PE是很爽)啊!!

技术分享
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 #define MAXN 100000+10
 5 int n,root,tot,fa[MAXN],size[MAXN],to[MAXN][2];
 6 bool mark[MAXN];
 7 struct H{int id,num;}a[MAXN];
 8 bool cmp(H a,H b){if(a.num==b.num)return a.id<b.id;return a.num<b.num;}
 9 void Up(int x){size[x]=size[to[x][0]]+size[to[x][1]]+1;}
10 void Pushdown(int x)
11 {
12     if(!mark[x]) return ;
13     int l=to[x][0],r=to[x][1];
14     swap(to[x][0],to[x][1]);
15     mark[l]^=1;mark[r]^=1;
16     mark[x]=false;
17 }
18 void Rotate(int &rt,int x)
19 {
20     int y=fa[x],z=fa[y];
21     Pushdown(y);Pushdown(x);
22     if(y==rt) rt=x;
23     else to[z][to[z][1]==y]=x;
24     int c=to[y][1]==x;
25     fa[y]=x;fa[x]=z;fa[to[x][c^1]]=y;
26     to[y][c]=to[x][c^1];to[x][c^1]=y;
27     Up(y);Up(x);
28 }
29 void Splay(int &rt,int x){while(x!=rt){if(fa[x]!=rt)Rotate(rt,fa[x]);Rotate(rt,x);}}
30 void Build()
31 {
32     tot++;size[tot]=1;
33     for(int i=1;i<=n+1;i++)
34     {
35         tot++;fa[tot-1]=tot;
36         to[tot][0]=tot-1;
37         size[tot]=tot;
38     }
39     root=tot;
40 }
41 int main()
42 {
43     scanf("%d",&n);Build();
44     for(int i=1;i<=n;i++) scanf("%d",&a[i].num),a[i].id=i+1;
45     sort(a+1,a+n+1,cmp);
46     a[0].id=1;
47     for(int i=1;i<=n;i++)
48     {
49         Splay(root,a[i].id);
50         printf("%d ",size[to[root][0]]);
51         Pushdown(root);int tmp=to[root][1];Pushdown(tmp);
52         while(to[tmp][0]!=0) tmp=to[tmp][0],Pushdown(tmp);
53         Splay(root,tmp);
54         Splay(to[root][0],a[i-1].id);
55         mark[to[a[i-1].id][1]]^=1;
56     }
57     return 0;
58 }
BZOJ 3506

1500: [NOI2005]维修数列

嗯~ 码农题

不错 写完过样例 交上去试试吧 WA WA WA . . . . . .

vijos终于AC 嗯 交到BZOJ去吧

TLE ? wco 卡内存!! 你什么心态啊!! 64MB怎么耍??

一晚没调出来 早上又改了 内存回收 hah 终于可以了吧

TLE ? woc 卡时间!! 题目明明3s BZOJ限制1s 什么心态啊!!

于是 读入优化+把splay链的情况改成树 终于AC

 技术分享第一个Ac是hzwer的 后面的AC是被卡时卡内存的

A了这个题整个人都不好了!! 平衡树就这样吧!!

技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 using namespace std;
  6 #define N 1000000+100
  7 #define L(x) to[x][0]
  8 #define R(x) to[x][1]
  9 #define I inline 
 10 int n,m,init[N],fa[N],to[N][2],root,size[N],mx[N],ml[N],mr[N],sum[N],num[N],top,q[N];
 11 bool mark[N],ch[N];
 12 I int Read()
 13 {
 14     char c=getchar();
 15     int tmp=0,t=1;
 16     while(c!=-&&(c<0||c>9)) c=getchar();
 17     if(c==-) t=-1,c=getchar();
 18     while(0<=c&&c<=9) tmp=tmp*10+c-0,c=getchar();
 19     return tmp*t;
 20 } 
 21 I void Up(int x)
 22 {
 23     int l=L(x),r=R(x);
 24     size[x]=size[l]+size[r]+1;
 25     sum[x]=sum[l]+sum[r]+num[x];
 26     mx[x]=max(max(mx[l],mx[r]),ml[l]+num[x]+mr[r]);
 27     ml[x]=max(ml[r],sum[r]+num[x]+ml[l]);
 28     mr[x]=max(mr[l],sum[l]+num[x]+mr[r]);
 29 }
 30 I void Pushdown(int x)
 31 {
 32     int l=L(x),r=R(x);
 33     if(ch[x]==true)
 34     {
 35         num[l]=num[r]=num[x];
 36         sum[l]=size[l]*num[x];
 37         sum[r]=size[r]*num[x];
 38         if(num[x]<0) ml[l]=ml[r]=mr[l]=mr[r]=0,mx[l]=mx[r]=num[x];
 39         else mx[l]=ml[l]=mr[l]=sum[l],mx[r]=ml[r]=mr[r]=sum[r];
 40         ch[l]=ch[r]=true;
 41         sum[0]=ml[0]=mr[0]=0;
 42         mx[0]=-100000000;
 43         ch[x]=mark[x]=false;
 44     }
 45     if(mark[x])
 46     {
 47         mark[x]=false;
 48         mark[l]^=1;mark[r]^=1;
 49         swap(L(l),R(l));swap(L(r),R(r));
 50         swap(ml[l],mr[l]);swap(ml[r],mr[r]);
 51     }
 52 }
 53 I void Rotate(int &rt,int x)
 54 {
 55     int y=fa[x],z=fa[y]; 
 56     if(y==rt) rt=x;
 57     else to[z][to[z][1]==y]=x;
 58     int c=to[y][1]==x;
 59     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
 60     to[y][c]=to[x][c^1];to[x][c^1]=y;
 61     Up(y);Up(x);
 62 }
 63 I void Splay(int &rt,int x)
 64 {
 65     while(x!=rt)
 66     {
 67         if(fa[x]!=rt) Rotate(rt,fa[x]);
 68         Rotate (rt,x);
 69     }
 70 }
 71 I int Find(int rt,int x)
 72 {
 73     Pushdown(rt);
 74     int tmp=size[L(rt)]+1;
 75     if(tmp==x)return rt;
 76     if(tmp<x)return Find(R(rt),x-tmp);
 77     else return Find(L(rt),x);
 78 }
 79 I void Build(int l,int r)
 80 {
 81 
 82     int mid=(l+r)/2;
 83     int t=q[top--];
 84     num[t]=init[mid];
 85     if(l<mid) to[t][0]=q[top],fa[q[top]]=t,Build(l,mid-1);
 86     if(mid<r) to[t][1]=q[top],fa[q[top]]=t,Build(mid+1,r);
 87     Up(t);
 88 }
 89 I void Insert(int x,int lenth)
 90 {
 91     int y=Find(root,x+1);x=Find(root,x);
 92     Splay(root,x);Splay(R(x),y);
 93     for(int i=1;i<=lenth;i++)
 94         init[i]=Read();
 95     L(y)=q[top];fa[q[top]]=y;
 96     Build(1,lenth);
 97     Up(y);Up(x);
 98 }
 99 I void Dfs(int x)
100 {
101     if(x==0)return ;
102     Dfs(L(x));Dfs(R(x));
103     size[x]=num[x]=fa[x]=L(x)=R(x)=sum[x]=ml[x]=mr[x]=mx[x]=0;
104     mark[x]=ch[x]=false;
105     q[++top]=x;
106 }
107 I void Delate(int x,int lenth)
108 {
109     int y=Find(root,x+lenth);x=Find(root,x-1);
110     Splay(root,x);Splay(R(x),y);
111     Dfs(L(y));L(y)=0;Up(y);Up(x);
112 }
113 I void Change(int x,int lenth,int t)
114 {
115     int y=Find(root,x+lenth);x=Find(root,x-1);
116     Splay(root,x);Splay(R(x),y);
117     int tmp=L(y);num[tmp]=t;ch[tmp]=true;sum[tmp]=size[tmp]*t;
118     if(t<0) ml[tmp]=mr[tmp]=0,mx[tmp]=t;
119     else ml[tmp]=mr[tmp]=mx[tmp]=sum[tmp];
120     Up(y);Up(x);
121 }
122 I void Evert(int x,int lenth)
123 {
124     int y=Find(root,x+lenth);x=Find(root,x-1);
125     Splay(root,x);Splay(R(x),y);
126     int tmp=L(y);
127     mark[tmp]^=1;swap(L(tmp),R(tmp)),swap(ml[tmp],mr[tmp]);
128     Up(y),Up(x); 
129 }
130 I int Sum(int x,int lenth)
131 {
132     int y=Find(root,x+lenth);x=Find(root,x-1);
133     Splay(root,x);Splay(R(x),y);
134     return sum[L(y)];
135 }
136 int main()
137 {
138     scanf("%d %d",&n,&m);mx[0]=-100000000;
139     for(int i=1;i<=1000000;i++) q[i]=1000000-i+1;top=1000000;
140     top-=2;fa[1]=2;to[2][0]=1;size[1]=1;size[2]=2;root=2;num[1]=num[2]=-100000000;Up(1);Up(2);
141     Insert(1,n);
142     char s[200];int x,y,z;
143     while(m--)
144     {
145         scanf("%s",s+1);
146         switch(s[3])
147         {
148             case S:x=Read();y=Read();Insert(x+1,y);break;
149             case L:x=Read();y=Read();Delate(x+1,y);break;
150             case K:x=Read();y=Read();z=Read();Change(x+1,y,z);break;
151             case V:x=Read();y=Read();Evert(x+1,y);break;
152             case T:x=Read();y=Read();printf("%d\n",Sum(x+1,y));break;
153             case X:printf("%d\n",mx[root]);break;
154         }
155     }
156     return 0;
157 }
BZOJ 1500

1507: [NOI2003]Editor

由于1500留下的阴影 这个题弃疗~

2002: [Hnoi2010]Bounce 弹飞绵羊

分块水过 但LCT都好玄学啊 网上的题解都换根了(虽然又换回来 但不能理解啊) 于是蒟蒻写了个 没有换根的 少个Up 调试半天 ~

技术分享
 1 #include <cstdio>
 2 #include <cmath>
 3 #define MAXN 400000+10
 4 int n,m,num[MAXN],pos[MAXN],to[MAXN],ans[MAXN];
 5 void Updata(int L,int R)
 6 {
 7     for(int i=R;i>=L;i--)
 8     {
 9         if(num[i]+i<=n&&pos[num[i]+i]==pos[i]) 
10             to[i]=to[i+num[i]],ans[i]=ans[i+num[i]]+1;
11         else
12             to[i]=i,ans[i]=0;
13     }
14 }
15 int Q(int x)
16 {
17     int r=0;
18     while(x+num[x]<=n)
19     {
20         if(to[x]==x) r+=1,x=x+num[x];
21         else  r+=ans[x],x=to[x];
22     }
23     return r;
24 }
25 int main()
26 {
27     scanf("%d",&n);int L=sqrt(n);
28     for(int i=1;i<=n;i++) scanf("%d",&num[i]),pos[i]=i/L+1;
29     for(int i=L-1;i<=n;i+=L) Updata(i/L*L,i);Updata(n/L*L,n);
30     scanf("%d",&m); int x,y,z;
31     while(m--)
32     {
33         scanf("%d",&x);
34         if(x==1) scanf("%d",&y),printf("%d\n",Q(y+1)+1);
35         else scanf("%d %d",&y,&z),y++,num[y]=z,Updata(y/L*L,y);
36     }
37     return 0;
38 }
BZOJ 2002 分块
技术分享
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 #define N 200000+10
 7 int n,m,num[N],fa[N],to[N][2],ans[N],size[N];
 8 bool mark[N];
 9 void Up(int x)
10 {
11     size[x]=size[to[x][0]]+size[to[x][1]]+1;
12 }
13 bool Isroot(int x)
14 {
15     return to[fa[x]][0]!=x&&to[fa[x]][1]!=x;
16 }
17 void Rotate(int x)
18 {
19     int y=fa[x],z=fa[y];
20     if(!Isroot(y)) to[z][to[z][1]==y]=x;
21     int c=to[y][1]==x;
22     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
23     to[y][c]=to[x][c^1];to[x][c^1]=y;
24     Up(y);Up(x);
25 }
26 void Splay(int x)
27 {
28     if(!x)return ;
29     while(!Isroot(x))
30     {
31         if(!Isroot(fa[x])) Rotate(fa[x]);
32         Rotate(x);
33     }
34 }
35 void Access(int x)
36 {
37     int t=0;
38     while(x)
39     {
40         Splay(x);
41         to[x][1]=t;
42         t=x;Up(x);
43         x=fa[x];
44     }
45 }
46 int main()
47 {
48     cin>>n;
49     for(int i=1;i<=n;i++) 
50     {
51         int x; 
52         scanf("%d",&x);
53         if(x+i<=n) fa[i]=x+i;
54     }
55     cin>>m;
56     while(m--)
57     {
58         int opt,x,y;
59         scanf("%d",&opt);
60         switch(opt)
61         {
62             case 1:scanf("%d",&x);x++;Access(x);Splay(x);printf("%d\n",size[x]);break;
63             case 2:scanf("%d %d",&x,&y);x++;
64                    Access(x);Splay(x);fa[to[x][0]]=0;
65                    to[x][0]=0; 
66                    if(x+y<=n)fa[x]=x+y;
67                    else fa[x]=0;
68                     
69                    break;
70         }
71     }
72     return 0;
73 } 
BZOJ 2002 LCT

2049: [Sdoi2008]Cave 洞穴勘测

读题不认真 不然早A了 居然没有看见是树的条件

技术分享
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 #define N 10000+10
 7 int fa[N],to[N][2],n,m;
 8 bool mark[N];
 9 bool Is(int x)
10 {
11     return to[fa[x]][0]!=x&&to[fa[x]][1]!=x;
12 }
13 void Pushdown(int x)
14 {
15     if(!mark[x]) return ;
16     mark[x]=false;
17     swap(to[x][0],to[x][1]);
18     mark[to[x][0]]^=1;
19     mark[to[x][1]]^=1;
20 }
21 void Rotate(int x)
22 {
23     int y=fa[x],z=fa[y];
24     Pushdown(y);Pushdown(x);
25     if(!Is(y)) to[z][to[z][1]==y]=x;
26     int c=to[y][1]==x;
27     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
28     to[y][c]=to[x][c^1];to[x][c^1]=y;
29 }
30 void Splay(int x)
31 {
32     Pushdown(x);
33     while(!Is(x))
34     {
35         if(!Is(fa[x])) Rotate(fa[x]);
36         Rotate(x);
37     }
38 }
39 void Access(int x)
40 {
41     int t=0;
42     while(x)
43     {
44         Splay(x);
45         to[x][1]=t;
46         t=x;
47         x=fa[x];
48     }
49 }
50 void Markroot(int x)
51 {
52     Access(x);Splay(x);mark[x]^=1;
53 }
54 int Find(int x)
55 {
56     Access(x);Splay(x);
57     while(to[x][0]!=0) x=to[x][0];
58     return x;
59 }
60 void Cut(int x,int y)
61 {
62     Markroot(x);Access(y);Splay(y);
63     to[y][0]=fa[x]=0;
64 }
65 void Link(int x,int y)
66 {
67     Markroot(x);fa[x]=y;
68 }
69 int main()
70 {
71     scanf("%d %d",&n,&m);
72     char s[10];int x,y;
73     while(m--)
74     {
75         scanf("%s %d %d",s,&x,&y);
76         switch(s[0])
77         {
78             case Q:if(Find(x)==Find(y)) printf("Yes\n");else printf("No\n");break;
79             case C:Link(x,y);break;
80             case D:Cut(x,y);break;
81         }
82     }
83     return 0;
84 }
BZOJ 2049

3669: [Noi2014]魔法森林

好神啊!!  

技术分享
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <cstdio>
  5 using namespace std;
  6 #define N 50000+10
  7 #define M 100000+10
  8 #define INF 1000000000
  9 #define l(x) to[x][0]
 10 #define r(x) to[x][1]
 11 int num[N+M],fa[N+M],to[N+M][2],mmax[N+M];
 12 bool mark[N+M]; 
 13 int f[N];
 14 int Find(int x)
 15 {
 16     if(f[x]!=x) f[x]=Find(f[x]);
 17     return f[x]; 
 18 }
 19 int n,m;
 20 struct H{int x,y;int a,b;}init[M];
 21 bool cmp(H x,H y){return x.a<y.a;}
 22 int ans=INF;
 23 void Up(int x)
 24 {
 25     int l=to[x][0],r=to[x][1];
 26     if(num[mmax[l]]>num[mmax[r]]) mmax[x]=mmax[l];
 27     else mmax[x]=mmax[r];
 28     if(num[x]>num[mmax[x]]) mmax[x]=x;
 29 }
 30 void Pushdown(int x)
 31 {
 32     if(!mark[x]) return ;
 33     mark[x]=false;
 34     swap(l(x),r(x));
 35     mark[l(x)]^=1;
 36     mark[r(x)]^=1;
 37 }
 38 bool Is(int x)
 39 {
 40     return l(fa[x])!=x&&r(fa[x])!=x;    
 41 }
 42 void Rotate(int x)
 43 {
 44     int y=fa[x],z=fa[y];
 45     Pushdown(y);Pushdown(x);
 46     if(!Is(y)) to[z][to[z][1]==y]=x;
 47     int c=to[y][1]==x;
 48     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
 49     to[y][c]=to[x][c^1];to[x][c^1]=y;
 50     Up(y);Up(x);
 51 }
 52 void Splay(int x)
 53 {
 54     Pushdown(x);
 55     while(!Is(x))
 56     {
 57         if(!Is(fa[x])) Rotate(fa[x]);
 58         Rotate(x);
 59     }
 60 }
 61 void Access(int x)
 62 {
 63     int t=0;
 64     while(x)
 65     {
 66         Splay(x);
 67         to[x][1]=t;
 68         t=x;Up(x);
 69         x=fa[x];
 70     }
 71 }
 72 void Markroot(int x)
 73 {
 74     Access(x);Splay(x);mark[x]^=1;
 75 }
 76 void Link(int x,int y)
 77 {
 78     Markroot(x);fa[x]=y;
 79 }
 80 void Cut(int x,int y)
 81 {
 82     Markroot(x);Access(y);Splay(y);
 83     l(y)=fa[x]=0;
 84 }
 85 int Sum(int x,int y)
 86 {
 87     Markroot(x);Access(y);Splay(y);
 88     return mmax[y];
 89 }
 90 int main()
 91 {
 92     cin>>n>>m;
 93     for(int i=1;i<=n;i++) f[i]=i;
 94     for(int i=1;i<=m;i++) scanf("%d %d %d %d",&init[i].x,&init[i].y,&init[i].a,&init[i].b);
 95     sort(init+1,init+1+m,cmp);
 96     for(int i=1;i<=m;i++)
 97     {
 98         int x=init[i].x,y=init[i].y,a=init[i].a,b=init[i].b;
 99         if(Find(x)==Find(y))
100         {
101             int v=Sum(x,y);
102             if(num[v]>b)
103             {
104                 Cut(v,init[v-n].x);
105                 Cut(v,init[v-n].y);
106                 v=n+i;
107                 num[v]=b;
108                 Link(v,x);
109                 Link(v,y);
110             }    
111         }
112         else
113         {
114             f[Find(x)]=Find(y);
115             int v=n+i;
116             num[v]=b;
117             Link(v,x);
118             Link(v,y);
119         }
120         if(Find(1)==Find(n)) ans=min(ans,a+num[Sum(1,n)]);
121     }
122     if(Find(1)!=Find(n)) cout<<"-1";
123     else cout<<ans;
124     return 0;
125 } 
BZOJ 3669

2631: tree

是双倍经验吗 差不多 和另一个挺像 忘记题号了 

我的Up Pushdown 惨不忍睹!!

技术分享
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstdio>
  5 #include <cstring>
  6 using namespace std;
  7 #define N 100000+10
  8 #define MO 51061 
  9 #define LL unsigned
 10 LL fa[N],to[N][2],num[N],sum[N],jx[N],ig[N],size[N];
 11 bool mark[N];
 12 LL n,m;
 13 void Pushdown(LL x)
 14 {
 15     LL l=to[x][0],r=to[x][1];  
 16     ig[l]*=ig[x];ig[r]*=ig[x];         ig[l]%=MO;ig[r]%=MO;
 17     num[l]*=ig[x];num[r]*=ig[x];     num[l]%=MO;num[r]%=MO; 
 18     num[l]+=jx[x];num[r]+=jx[x];    num[l]%=MO;num[r]%=MO;
 19     sum[l]*=ig[x];sum[r]*=ig[x];    sum[l]%=MO; sum[r]%=MO;
 20     sum[l]+=(size[l]*jx[x])%MO;    sum[l]%=MO;
 21     sum[r]+=(size[r]*jx[x])%MO;  sum[r]%=MO;
 22     jx[l]*=ig[x];jx[r]*=ig[x];  jx[l]%=MO; jx[r]%=MO;
 23     
 24     jx[l]+=jx[x];jx[r]+=jx[x];  jx[l]%=MO; jx[r]%=MO;
 25     jx[x]=0;ig[x]=1;sum[0]=0;
 26     
 27     if(!mark[x]) return ;
 28     mark[x]=false;
 29     mark[l]^=1;mark[r]^=1;
 30     swap(to[x][1],to[x][0]);
 31 }
 32 void Up(LL x)
 33 {
 34     LL l=to[x][0],r=to[x][1];
 35     sum[x]=(sum[l]+sum[r]+num[x])%MO;
 36     size[x]=(size[l]+size[r]+1)%MO;
 37 }
 38 bool Isroot(LL x)
 39 {
 40     return to[fa[x]][0]!=x&&to[fa[x]][1]!=x;
 41 }
 42 void Rotate(LL x)
 43 {
 44     LL y=fa[x],z=fa[y];
 45     Pushdown(y);Pushdown(x);
 46     if(!Isroot(y)) to[z][to[z][1]==y]=x;
 47     LL c=to[y][1]==x;
 48     fa[x]=z;fa[y]=x;fa[to[x][c^1]]=y;
 49     to[y][c]=to[x][c^1];to[x][c^1]=y;
 50     Up(y);Up(x);
 51 }
 52 void Splay(LL x)
 53 {
 54     Pushdown(x);
 55     while(!Isroot(x))
 56     {
 57         if(!Isroot(fa[x]))  Rotate(fa[x]);
 58         Rotate(x);
 59     }
 60 } 
 61 void Access(LL x)
 62 {
 63     LL t=0;
 64     while(x!=0)
 65     {
 66         Splay(x);
 67         to[x][1]=t;
 68         t=x;Up(x);
 69         x=fa[x];
 70     }
 71 }
 72 void Markroot(LL x)
 73 {
 74     Access(x);Splay(x);mark[x]^=1;
 75 }
 76 int main()
 77 {
 78     scanf("%d %d",&n,&m);for(LL i=1;i<=n;i++) ig[i]=sum[i]=num[i]=1,size[i]=1;
 79     for(LL i=1;i<n;i++)
 80     {
 81         LL x,y;scanf("%d %d",&x,&y);
 82         Markroot(x);fa[x]=y;
 83     }
 84     LL x,y,z,x1,y1;
 85     char c;
 86     while(m--)
 87     {
 88         getchar();
 89         c=getchar();
 90         switch(c)
 91         {
 92             case +:scanf("%d %d %d",&x,&y,&z);Markroot(x);Access(y);Splay(y);z%=MO;sum[y]+=(z*size[y])%MO;sum[y]%=MO;num[y]+=z;num[y]%=MO;jx[y]+=z;jx[y]%=MO;break;
 93             case -:scanf("%d %d %d %d",&x,&y,&x1,&y1);
 94                      Markroot(x);Access(y);Splay(y);
 95                      to[y][0]=fa[x]=0;
 96                      Markroot(x1);fa[x1]=y1;
 97                      break;
 98             case *:scanf("%d %d %d",&x,&y,&z);z%=MO;Markroot(x);Access(y);Splay(y);jx[y]*=z;jx[y]%=MO;sum[y]*=z;sum[y]%=MO;num[y]*=z;num[y]%=MO;ig[y]*=z;ig[y]%=MO;break;
 99             case /:scanf("%d %d",&x,&y);Markroot(x);Access(y);Splay(y);printf("%d\n",sum[y]);break;
100         }
101     }
102     return 0;
103 }
BZOJ 2631

 

 

差不多了 平衡树 lct 先这样吧 还有一些 写这些题目时的双倍经验 或 模板题 没有贴

 

博客开更!!

标签:

原文地址:http://www.cnblogs.com/ofsxb/p/5215696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!