BZOJ3207: 花神的嘲讽计划Ⅰ

居然一A了感动qwq
今天一个下午在捣鼓新到的MacBookPro
要好好努力辣~
然后最后终于可以用了(撒花
一开始以为是子序列…

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char c;
inline void read(int &a)
{
    a=0;do c=getchar();while(c<'0'||c>'9');
    while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}

struct Node
{
    Node *lc,*rc;
    int l,r;
    int Time;    
    Node(){lc=rc=NULL;Time=0;}

};

int k;
const
  int Mod=1000007;

struct Key
{
    int *A;
    Key(){A=new int[k];}
    inline friend bool operator !=(Key a,Key b)
    {
       for(int i=0;i<k;i++)if(a.A[i]!=b.A[i])return true;
      return false;
    }
};

struct Tab
{
    Tab*next;
    int Data,last;
    Key Rec;
}*Head[Mod];
Tab *Tp;
bool find(Tab *Cur,Key Rec)
{
   if(Cur==NULL)return false;
   if(Cur->Rec!=Rec)return find(Cur->next,Rec);
   return Tp=Cur,true;
}

int Con;
inline void Add(int pl,int C,Key Rec)
{
   Tab *tp=new Tab;
   tp->Rec=Rec;
   tp->last=C;
   tp->next=Head[pl],Head[pl]=tp;    
   tp->Data=++Con;
}

int Hash(Key a)
{
   int res=0;
   for(int i=0;i<k;i++)
      res=((res<<3)+(res>>1)+a.A[i])%Mod;
   return res;
}

int Pre[1000001];

inline Node *Build(int l,int r)
{
  Node *N=new Node;
  N->l=l,N->r=r;N->Time=0;
  return N;
}

Node *Modify(Node *Old,int l)
{
  int Mid=Old->l+Old->r>>1;
  Node *New=new Node;
  *New=*Old;
  if(New->l==New->r)
    return New->Time++,New;
   if(New->lc==NULL)
        New->lc=Build(Old->l,Mid),New->rc=Build(Mid+1,Old->r);
  if(l<=Mid)New->lc=Modify(New->lc,l);
  else New->rc=Modify(New->rc,l);
  return New->Time++,New;
}
int Query(Node *Old,Node *New,int l)
{
   if((Old?Old->Time:0)==New->Time)return 0;
   if(New->l==New->r)
       return New->Time-(Old?Old->Time:0);
   int Mid=New->l+New->r>>1;
   if(Mid>=l)return Query((Old?Old->lc:NULL),New->lc,l);
   return Query((Old?Old->rc:NULL),New->rc,l); 
}


Node *T[1000001];
int n,m;
int A[1000001];
int main()
{

   read(n),read(m),read(k);
   if(n<k)
       {
          while(m--)
              puts("Yes");
         return 0;
       }
   int i;
   for(i=1;i<k;i++)
      read(A[i]);
    Key ty;
    T[k-1]=Build(1,n);
      for(i;i<=n;i++)
    {
       read(A[i]);ty.A=A+i-k+1;
       int pl=Hash(ty),o;
       if(!find(Head[pl],ty))
           {
           Add(pl,i,ty);
           o=Con;
           Pre[i]=0;
           }

       else
           {
               Pre[i]=Tp->last;
               Con=Tp->Data;
               Tp->last=i;
           }
       T[i]=Modify(T[i-1],Con);
    }
    ty.A=new int[k];
    for(int i=1;i<=m;i++)
        {
           int x,y,pl;
           read(x),read(y);
           for(int j=0;j<k;j++)read(ty.A[j]);
            if(!find(Head[pl=Hash(ty)],ty))
                 {puts("Yes");}
            else
              {

                int TT=Query(T[x+k-2],T[y],Tp->Data);
                if(TT)puts("No");
                else puts("Yes");
              }

        }
  return 0;
}

你可能感兴趣的:(BZOJ3207: 花神的嘲讽计划Ⅰ)