BZOJ 2124 线段树维护hash值
思路:
http://blog.csdn.net/wzq_QwQ/article/details/47152909
(代码也是抄的他的)
自己写得垃圾线段树怎么都过不了
隔了两个月 再写 再挂
又隔了10天 再写 终于A了………………………..
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mod 1000000007
#define int long long
int cases,n,a[10050],hash[999999],hash2[999999],p[10050];
void push_up(int pos,int num){
int lson=pos<<1,rson=pos<<1|1;
hash[pos]=(hash[lson]*p[num/2]+hash[rson])%mod;
hash2[pos]=(hash2[rson]*p[num-num/2]+hash2[lson])%mod;
}
void insert(int l,int r,int pos,int num){
if(l==r){hash[pos]=hash2[pos]=1;return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(num<=mid)insert(l,mid,lson,num);
else insert(mid+1,r,rson,num);
push_up(pos,r-l+1);
}
int query(int L,int R,int l,int r,int pos){
if(L==l&&r==R)return hash[pos];
int mid=(l+r)>>1;
if(R<=mid)return query(L,R,l,mid,pos<<1);
else if(L>mid)return query(L,R,mid+1,r,pos<<1|1);
else return (query(L,mid,l,mid,pos<<1)*p[R-mid]+query(mid+1,R,mid+1,r,pos<<1|1))%mod;
}
int query2(int L,int R,int l,int r,int pos){
if(L==l&&r==R)return hash2[pos];
int mid=(l+r)>>1;
if(R<=mid)return query2(L,R,l,mid,pos<<1);
else if(L>mid)return query2(L,R,mid+1,r,pos<<1|1);
else return (query2(L,mid,l,mid,pos<<1)+query2(mid+1,R,mid+1,r,pos<<1|1)*p[mid-L+1])%mod;
}
signed main(){
p[0]=1;
for(int i=1;i<=10000;i++)p[i]=(p[i-1]*3)%mod;
scanf("%lld",&cases);
while(cases--){
memset(hash,0,sizeof(hash)),memset(hash2,0,sizeof(hash2));
scanf("%lld",&n);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
for(int i=1;i<=n;i++){
int len=min(n-a[i],a[i]-1);
int tmp1=query(a[i]-len,a[i],1,n,1);
int tmp2=query2(a[i],a[i]+len,1,n,1);
if(tmp1!=tmp2){puts("Y");goto ed;}
insert(1,n,1,a[i]);
}puts("N");ed:;
}
}