zoukankan      html  css  js  c++  java
  • BZOJ3173: [Tjoi2013]最长上升子序列

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3173

    题解:水题。插入一个数+查询前缀最大值?splay裸题。

     什么你说treap?我的treap写法比较无脑,还是splay来得好。。。

    2222这代码长度。。。

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 100000+5
    14 #define maxm 100000+5
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define for0(i,n) for(int i=0;i<=(n);i++)
    19 #define for1(i,n) for(int i=1;i<=(n);i++)
    20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
    23 #define mod 1000000007
    24 using namespace std;
    25 inline int read()
    26 {
    27     int x=0,f=1;char ch=getchar();
    28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    30     return x*f;
    31 }
    32 int n,rt,t1,t2,s[maxn],c[maxn][2],fa[maxn],v[maxn],mx[maxn],tot;
    33 inline void pushup(int x)
    34 {
    35     int l=c[x][0],r=c[x][1];
    36     s[x]=s[l]+s[r]+1;
    37     mx[x]=max(v[x],max(mx[l],mx[r]));
    38 }
    39 inline void rotate(int x,int &k)
    40 {
    41     int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
    42     if(y!=k)c[z][c[z][1]==y]=x;else k=x;
    43     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
    44     c[y][l]=c[x][r];c[x][r]=y;
    45     pushup(y);pushup(x);
    46 }
    47 inline void splay(int x,int &k)
    48 {
    49     while(x!=k)
    50     {
    51         int y=fa[x],z=fa[y];
    52         if(y!=k)
    53         {
    54             if(c[z][0]==y^c[y][0]==x)rotate(x,k);else rotate(y,k);
    55         }
    56         rotate(x,k);
    57     }
    58 }
    59 inline int find(int x,int k)
    60 {
    61     int l=c[x][0],r=c[x][1];
    62     if(s[l]+1==k)return x;
    63     else if(s[l]>=k)return find(l,k);
    64     else return find(r,k-s[l]-1);
    65 }
    66 inline void split(int l,int r)
    67 {
    68     t1=find(rt,l);t2=find(rt,r);
    69     splay(t1,rt);splay(t2,c[t1][1]);
    70 }
    71 int main()
    72 {
    73     freopen("input.txt","r",stdin);
    74     freopen("output.txt","w",stdout);
    75     n=read();
    76     rt=1;
    77     fa[c[1][1]=2]=1;
    78     tot=2;
    79     int ans=0;
    80     for1(i,n)
    81     {
    82         int rk=read();
    83         split(1,rk+2);
    84         int t=mx[c[t2][0]];
    85         split(rk+1,rk+2);
    86         fa[c[t2][0]=++tot]=t2;
    87         s[tot]=1;v[tot]=mx[tot]=t+1;
    88         pushup(t2);pushup(t1);
    89         ans=max(ans,t+1);
    90         printf("%d
    ",ans);
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    Java并发容器和线程池
    CountDownLatch、CyclicBarrier
    spring data jpa
    转换时间的工具类(1)
    swagger 报错: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot h
    自动生成entry-mapper-service-controller
    Hutool工具包导出Excel文件异常 You need to add dependency of poi-ooxml to your project
    Java获取24小时制的时间方法
    查询全国城市行政区
    Java对象与Json字符串之间的转化
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4263769.html
Copyright © 2011-2022 走看看