• bzoj1014: [JSOI2008]火星人prefix splay+hash


    我写的代码好像自古以来就是bzoj不友好型的

    本地跑的比std快,但是交上去巧妙被卡

    答案。。。应该是对的,拍了好久了

      1 #include <bits/stdc++.h>
      2 #define MOD 998244353
      3 #define mid (l+r>>1)
      4 using namespace std;
      5 int n,m,x,y;char ch;
      6 long long mi[300001];
      7 struct spla
      8 {
      9     int c[300001][2],fa[300001],ch[300001],size[300001],ha[300001];
     10     int rt,cnt;
     11     void up(int now)
     12     {
     13         size[now]=size[c[now][0]]+size[c[now][1]]+1;
     14         ha[now]=(ha[c[now][0]]+mi[size[c[now][0]]]*ch[now]%MOD+mi[size[c[now][0]]+1]*ha[c[now][1]]%MOD)%MOD;
     15     }
     16     void rot(int x,int &root)
     17     {
     18         int y=fa[x],k=c[y][1]==x;
     19         if(y!=root) c[fa[y]][c[fa[y]][1]==y]=x;
     20         else root=x;
     21         fa[x]=fa[y];
     22         fa[y]=x;
     23         fa[c[x][!k]]=y;
     24         c[y][k]=c[x][!k];
     25         c[x][!k]=y; 
     26         up(y);up(x);
     27     }
     28     void splay(int x,int &root)
     29     {
     30         for(int y=fa[x];x!=root;rot(x,root),y=fa[x])
     31             if(y!=root)
     32             rot(((c[fa[y]][1]==y)^(c[y][1]==x))?x:y,root);
     33     }
     34     void add(int x,int y)
     35     {
     36         ch[++cnt]=y;size[cnt]=1;ha[cnt]=y;
     37         if(!rt)
     38         {
     39             rt=cnt;
     40             return;
     41         }
     42         if(x==0)
     43         {
     44             splay(fin(1),rt);
     45             c[rt][0]=cnt;fa[cnt]=rt;
     46             return;
     47         }
     48         splay(fin(x),rt);
     49         if(x==n)
     50             c[rt][1]=cnt,fa[cnt]=rt;
     51         else
     52             splay(fin(x+1),c[rt][1]),c[c[rt][1]][0]=cnt,fa[cnt]=c[rt][1];
     53     }
     54     /*
     55     void add(int x,int y)
     56     {
     57         ch[++cnt]=y;size[cnt]=1;ha[cnt]=y;
     58         if(!rt)
     59         {
     60             rt=cnt;
     61             return;
     62         }
     63         int now=rt;
     64         while(1)
     65         {
     66             if(x>size[c[now][0]])
     67                 if(c[now][1]) x-=size[c[now][0]]+1,now=c[now][1];
     68                 else
     69                 {
     70                     c[now][1]=cnt;fa[cnt]=now;
     71                     splay(cnt,rt);
     72                     return;
     73                 }
     74             else
     75                 if(c[now][0]) now=c[now][0];
     76                 else
     77                 {
     78                     c[now][0]=cnt;fa[cnt]=now;
     79                     splay(cnt,rt);
     80                     return;
     81                 } 
     82         }
     83     }*/ 
     84     int fin(int x)
     85     {
     86         int now=rt;
     87         while(x>1 || c[now][0])
     88         {
     89             if(x==size[c[now][0]]+1)
     90                 break;
     91             if(x>size[c[now][0]])
     92                 x-=size[c[now][0]]+1,now=c[now][1];
     93             else
     94                 now=c[now][0];
     95         }
     96         return now;
     97     }
     98     void change(int x,int y)
     99     {
    100         int now=fin(x);
    101         splay(now,rt);
    102         ch[now]=y;
    103         up(now);
    104     }
    105     int hash(int x,int y)
    106     {
    107         if(x==1 && y==n) return ha[rt];
    108         if(x==1)
    109         {
    110             splay(fin(y+1),rt);
    111             return ha[c[rt][0]]; 
    112         }
    113         if(y==n)
    114         {
    115             splay(fin(x-1),rt);
    116             return ha[c[rt][1]];
    117         }
    118         splay(fin(x-1),rt);
    119         splay(fin(y+1),c[rt][1]);
    120         return ha[c[c[rt][1]][0]];
    121     }
    122 } sp;
    123 inline int read()
    124 {
    125     char ch=getchar();
    126     for(;!isdigit(ch);ch=getchar());
    127     int re=0;
    128     bool fl=1;
    129     if (ch=='-')
    130     {
    131         re=0;
    132         ch=getchar();
    133     }
    134     while (isdigit(ch))
    135     {
    136         re=re*10+ch-'0';
    137         ch=getchar();
    138     }
    139     return fl?re:-re;
    140 }
    141 inline void write(int re)
    142 {
    143     if (re<0)
    144     {
    145         putchar('-');
    146         re=-re;
    147     }
    148     if (re>9) write(re/10);
    149     putchar(re%10+'0');
    150 }
    151 void work(int x,int y)
    152 {
    153     if(x>y) swap(x,y);
    154     int l=1,r=n-y+2;
    155     while(l<r)
    156     if(sp.hash(x,x+mid-1)==sp.hash(y,y+mid-1)) l=mid+1;
    157         else r=mid;
    158     write(l-1);puts("");
    159 }
    160 int main()
    161 {
    162     mi[0]=1;
    163     for(int i=1;i<=250000;i++)
    164         mi[i]=mi[i-1]*233%MOD;
    165     for(ch=getchar();isalpha(ch);ch=getchar())
    166         sp.add(n,ch-'a'+1),++n;
    167     m=read();
    168     for(int i=1;i<=m;i++)
    169     {
    170         for(ch=getchar();!isalpha(ch);ch=getchar());
    171         x=read();
    172         if(ch=='Q') y=read();
    173         else
    174         {
    175             char cas=ch; 
    176             for(ch=getchar();!isalpha(ch);ch=getchar());
    177             y=ch-'a'+1;
    178             ch=cas;
    179         }
    180         if(i==7)
    181             int e=1;
    182         if(ch=='Q') work(x,y);
    183         else
    184         if(ch=='R')
    185             sp.change(x,y);
    186         else
    187             sp.add(x,y),++n;
    188     }
    189     return 0;
    190  } 
  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/wanglichao/p/7267308.html
Copyright © 2020-2023  润新知