bzoj-1125 Poc

Source: Internet
Author: User

Test instructions

Gives the n string, the length is Len;

There are m operations, each time two characters are exchanged;

Ask each string at any point in time, with the same number of its most strings;

n<=1000,len<=100,m<=100000;


Exercises

Poi!

String length is very small, we first consider the use of hash to determine the same string;

These sections are simple, but it is difficult to count the answers to a string at any point in time;

Because if the violence is updated the complexity is O (nm), so do not do so;

And there is no good way to delay the update, only to find the data structure of the marker;

Here I wrote a splay, with the hash value size for the keyword sort;

Change it to the root of the deletion, and then modify and then plug it back,

Then the hash value of the same paragraph is put forward to mark the updated answer;

So the complexity of each time is O (Len+logn).

Note When swapping characters in the same string, do not let it be deleted two times oh 2333;


Code:


#include <stdio.h> #include <string.h> #include <algorithm> #define N 1100#define seed 1331#define which (x) (ch[fa[x]][1]==x) using namespace Std;typedef unsigned long long ll;int n,len;int Size[n],fa[n],ch[n][2],cov[n] , Ans[n],root;char str[n][111];ll val[n],pow[111];void pushup (int x) {size[x]=size[ch[x][0]]+size[ch[x][1]]+1;} void pushdown (int x) {if (cov[x]) {Ans[ch[x][0]]=max (ans[ch[x][0]],cov[x]); Cov[ch[x][0]]=max (Cov[ch[x][0]],cov[x]); Ans[ch[x][1]]=max (Ans[ch[x][1]],cov[x]); Cov[ch[x][1]]=max (Cov[ch[x][1]],cov[x]); cov[x]=0;}} void Rotate (int x) {int f=fa[x];bool k=which (x); Pushdown (f), pushdown (x); Ch[f][k]=ch[x][!k];ch[x][!k]=f;ch[fa[f]][which (f)]=x;fa[x]=fa[f];fa[f]=x;fa[ch[f][k]]= F Pushup (f); Pushup (x);} void splay (int x,int g) {while (fa[x]!=g) {int f=fa[x];if (fa[f]==g) {Rotate (x); if (which (x) ^which (f)) Rotate (x); elserotate (f); Rotate (x);} if (!g) root=x;} int Pre (ll v) {int p=root,ret=root;while (p) {if (v>val[p]) ret=p,p=ch[p][1];elsep=ch[p][0];} return ret;} int Sub (ll v) {int P=rooT,ret;while (P) {if (v<val[p]) ret=p,p=ch[p][0];elsep=ch[p][1];} return ret;} void down (int x) {if (!x) return; Pushdown (x);D own (ch[x][0]);D own (ch[x][1]);} void Insert (int x) {int l=pre (val[x]), R=sub (Val[x]); Splay (l,0), splay (r,l); int t=ch[r][0];fa[t]=x,ch[x][0]=t;fa[x]=r,ch[r][0]=x; Pushup (x), Pushup (R), Pushup (L); Ans[x]=max (Ans[x],size[x]); Cov[x]=max (Cov[x],size[x]);} void del (int x) {splay (x,0); int p=ch[x][1];while (ch[p][0]) p=ch[p][0]; Splay (p,x); ch[p][0]=ch[x][0];fa[ch[p][0]]=p,fa[p]=0;root=p; Pushup (P); ch[x][0]=ch[x][1]=0;size[x]=1;} void Init () {pow[0]=1;for (int i=1;i<=len;i++) pow[i]=pow[i-1]*seed;root=n+1,size[n+1]=2,ch[n+1][1]=n+2;size[n+2 ]=1,fa[n+2]=n+1;val[n+1]=0,val[n+2]= (1ll<<64)-1;} int main () {int m,i,j,k,a,b,x,y;scanf ("%d%d%d", &n,&len,&m), Init (); for (i=1;i<=n;i++) {scanf ("%s", str [i]+1]; for (j=1;j<=len;j++) Val[i]=val[i]*seed+str[i][j];insert (i);} for (i=1;i<=m;i++) {scanf ("%d%d%d%d", &x,&a,&y,&b), if (x==y) {del (x); swap (str[x][a],str[x][b]); for (j=1,val[x]=0;j<=len;j++) Val[x]=val[x]*seed+str[x][j];insert (x); continue;} Del (x), Del (y), swap (str[x][a],str[y][b]); for (j=1,val[x]=val[y]=0;j<=len;j++) val[x]=val[x]*seed+str[x][j],val [Y]=val[y]*seed+str[y][j];insert (x), Insert (y);} (root), for (i=1;i<=n;i++) printf ("%d\n", Ans[i]); return 0;}



bzoj-1125 Poc

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.