Bzoj 2124: Linear sub-sequence

Source: Internet
Author: User

Sol

Segment Tree +hash.

First of all, there are at least 3 items of the violent linear sequence, in the enumeration of the items, the enumeration tolerance can be, only one number in front of the item appears, and the other number is not present before the term. Complexity \ (O (n^2) \)

And then I thought of one although the complexity did not change (because I would not design this data structure ...) but it seems like a bit of an algorithm, is the enumeration of items, consider moving from one item to another, that is \ (\pm \delta\) on the ... If you can use the data structure to maintain this operation, it is often good. Transform the item is the position of the folding.

Mark it up ... is to maintain the hash line tree, a number has appeared, that is 1, did not appear to be 0, maintain a positive and negative sequence of 01 sequence, if a number less than its positive sequence 01 sequence and greater than its reverse order 01 sequence XOR value is not 0 then there is a linear sequence satisfying the condition.

01 string too long can be used to maintain the hash ...

Ps:100000007 is not a prime number ... Did not open long long directly see ancestors.

PS: My constant is so big, qaq.

Code
/************************************************************** problem:2124 User:beiyu language:c++ Result : Accepted time:2348 ms memory:8720 kb****************************************************************/#include < cstdio> #include <cstring> #include <iostream>using namespace std; typedef long LONG ll;const int N = 100005;const LL p = 100000007; #define DEBUG (a) cout<< #a << "=" <<a<&l t; " "#define MID ((L+R) >>1) #define LC (O&LT;&LT;1) #define RC (o<<1|1) int t,n;int a[n]; LL h1[n<<2],h2[n<<2],pow[n]; inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | |    ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x;    } inline void Update (int x,int o,int l,int r) {if (l==r) {H1[o]=h2[o]=1;return;}    if (x<=mid) Update (X,lc,l,mid);    else Update (X,RC,MID+1,R);    H1[o]= (h1[lc]+h1[rc]*pow[mid-l+1])%p; H2[o]= (H2[LC]*POW[R-MID]+H2[RC])%p;} InchLine LL Query1 (int l,int r,int o,int l,int R) {if (l>r) return-1;//debug (L), Debug (R), Debug (L), Debug (R) <<endl    ;    if (l==l&&r==r) return h1[o];    if (L<=mid&&r>mid) return (Query1 (l,mid,lc,l,mid) +query1 (mid+1,r,rc,mid+1,r) *pow[mid-l+1])%p;    if (L<=mid) return Query1 (L,r,lc,l,mid)%p; else return Query1 (l,r,rc,mid+1,r)%p;//Debug (res) <<endl;//return res;    inline LL Query2 (int l,int r,int o,int l,int R) {if (l>r) return-1;    if (l==l&&r==r) return h2[o];    if (L<=mid&&r>mid) return (Query2 (l,mid,lc,l,mid) *pow[r-mid]+query2 (mid+1,r,rc,mid+1,r))%p;    if (L<=mid) return Query2 (L,r,lc,l,mid)%p; else return Query2 (l,r,rc,mid+1,r)%p;//return res;}    int main () {pow[0]=1;for (int i=1;i<n;i++) pow[i]= (pow[i-1]<<1)%p; For (T=in ();        t--;) {n=in (); for (int i=1;i<=n;i++) a[i]=in ();        int f=0;        memset (H1,0,sizeof (H1)), memset (h2,0,sizeof (H2)); for (int i=1;i<=n;i++) {intL=min (n-a[i],a[i]-1);            int U=query2 (A[I]-L,A[I]-1,1,1,N);          int V=query1 (a[i]+1,a[i]+l,1,1,n);//cout<< "*********" <<endl;//debug (i) <<endl;// Debug (L), Debug (A[i]) <<endl;//Debug (a[i]-l), Debug (a[i]-1) <<endl;//Debug (a[i]+1), Debug (             A[I]+L) <<endl;//cout<<u<< "" <<v<<endl;            if (u!=v) {f=1;break;}        Update (A[i],1,1,n);    }if (f) puts ("Y"); else puts ("N"); } return 0;}

  

Bzoj 2124: Linear sub-sequence

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.