"BZOJ4553" [tjoi2016&heoi2016] sequence CDQ Division + Tree Array

Source: Internet
Author: User

"BZOJ4553" [tjoi2016&heoi2016] Sequence description

When her sister's birthday, her little friend bought him a funny toy from a treasure. There is a series on the toy, the value of some items in the sequence may change, but at most one value changes at the same time. Now that her sister has studied all the possibilities of change, she would like to ask you, can you choose a sub-sequence, so that in any one change, this sub-sequence is not descending? Please tell her the longest length of this subsequence. Note: A maximum of one value varies for each change. In sample input 1, all the changes are:

1 2 32 2 31 3 31 1 31 2 4 Select Subsequence as the original sequence, that is, in any one of the changes are not descending sub-sequence in the sample input 2, all the changes are: 3 3 33 2 3 The selection sub-sequence is the first element and the third element, or the second element and the third element can satisfy the requirement input

The first line of input has two positive integers n, m, respectively, indicating the length of the sequence and the number of changes. The next line has n numbers, which indicate the original state of the sequence. The next m line, each row has 2 number x, Y, indicating that the X-item of the sequence can change to the value of Y. 1 <= x <= N. All numbers are positive integers and are less than or equal to 100,000


Outputs an integer that represents the corresponding answer

Sample Input3 4
1 2 3
1 2
2 3
2 1
3 4Sample Output3

Key: We set the possible maximum value for each number of R, the minimum value is L, the initial value is V, then we select the sequence of two adjacent items must be satisfied: Ra<=vb and va<=lb. is obviously a similar three-dimensional partial order of things, CDQ Division + tree-like array maintenance can be.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;int n,m,n,now,ans;const int maxn=100010;int s[maxn],tim[maxn];struct node{int l,r,v,org,f;} P[maxn];bool CMPR (const node &a,const node &b) {return A.R&LT;B.R;} BOOL CMPV (const node &a,const node &b) {return A.V&LT;B.V;} BOOL Cmpo (const node &a,const node &b) {return a.org<b.org;} inline Int rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+ (gc^ ' 0 '), Gc=getchar (); return ret*f;} inline void updata (int x,int val) {for (int i=x;i<=n;i+=i&-i) {if (Tim[i]<now) Tim[i]=now,s[i]=0;s[i]=max (S[i] , Val);}} inline int query (int x) {int i,ret=0;for (i=x;i;i-=i&-i) {if (Tim[i]<now) Tim[i]=now,s[i]=0;ret=max (Ret,s[i]);} return ret;} void Solve (int l,int r) {if (l==r) return; int mid= (L+R) >>1,i,h1=l,h2=mid+1;sort (P+l,p+r+1,cmpo); solve (l,mid); Sort (P+L,P+MID+1,CMPR), sort (p+MID+1,P+R+1,CMPV); Now++;for (i=l;i<=r;i++) {if (h1<=mid&& (h2>r| | P[H1].R&LT;=P[H2].V)) Updata (P[H1].V,P[H1].F), H1++;elsep[h2].f=max (P[h2].f,query (P[H2].L) +1), h2++;} Solve (mid+1,r);} int main () {n=rd (), m=rd (); int i,a,b;for (i=1;i<=n;i++) p[i].v=p[i].l=p[i].r=rd (), N=max (N,P[I].V), P[i].org=i,p[i] . F=1;for (i=1;i<=m;i++) a=rd (), B=rd (), P[a].l=min (p[a].l,b), P[a].r=max (p[a].r,b), N=max (n,b); solve (1,n); for (I=1 ; i<=n;i++) Ans=max (ANS,P[I].F);p rintf ("%d", ans); return 0;}

"BZOJ4553" [tjoi2016&heoi2016] sequence CDQ + tree-like array

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.