1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 const long long ha=100000007; 6 long long shu[2][40000],a[10004]; 7 void geng(int a1,int a2,int a3,int a4) 8 { 9 if(a2+1==a3) 10 { 11 shu[0][a1]=1; 12 shu[1][a1]=1; 13 return; 14 } 15 int mid; 16 mid=(a2+a3)/2; 17 if(a4<mid) 18 geng(a1<<1,a2,mid,a4); 19 else 20 geng(a1<<1|1,mid,a3,a4); 21 shu[0][a1]=((shu[0][a1<<1]*a[a3-mid])%ha+shu[0][a1<<1|1])%ha; 22 shu[1][a1]=(shu[1][a1<<1]+(shu[1][a1<<1|1]*a[mid-a2])%ha)%ha; 23 return; 24 } 25 long long zhao(int a1,int a2,int a3,int a4,int a5,int a6) 26 { 27 if(a2==a4&&a3==a5) 28 return shu[a6][a1]; 29 int mid; 30 mid=(a2+a3)/2; 31 long long le=0,re=0,sum; 32 if(a4<mid) 33 le=zhao(a1*2,a2,mid,a4,min(mid,a5),a6); 34 if(a5>mid) 35 re=zhao(a1*2+1,mid,a3,max(a4,mid),a5,a6); 36 if(a6) 37 sum=(le+(re*a[max(0,mid-a4)])%ha)%ha; 38 else 39 sum=((le*a[max(0,a5-mid)])%ha+re)%ha; 40 return sum; 41 } 42 int main() 43 { 44 int t; 45 scanf("%d",&t); 46 a[0]=1; 47 for(int i=1;i<=10000;i++) 48 a[i]=(a[i-1]*2)%ha; 49 for(int j=0;j<t;j++) 50 { 51 int n,i; 52 scanf("%d",&n); 53 memset(shu,0,sizeof(shu)); 54 for( i=0;i<n;i++) 55 { 56 int a1; 57 long long a2,a3; 58 scanf("%d",&a1); 59 int len=min(a1-1,n-a1); 60 if(a1!=1&&a1!=n&&len) 61 { 62 a2=zhao(1,1,n+1,a1-len,a1,0); 63 a3=zhao(1,1,n+1,a1+1,a1+len+1,1); 64 if(a2!=a3) 65 { 66 printf("Y "); 67 break; 68 } 69 } 70 geng(1,1,n+1,a1); 71 } 72 if(i==n) 73 printf("N "); 74 for(++i;i<n;++i) 75 scanf("%*d"); 76 } 77 return 0; 78 }
线段树 由于是1到n的全排列,每次插入一个值时看这个值左右两边已经有的是否对称,不对称就输出Y