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 series
May change, but only one value at the same time is changed. 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 Select a subsequence as the first element and the third element, or the second element and the third element , can meet the requirements of 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 number, which indicates that the sequence is primitive
State. 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,000output
Outputs an integer that represents the corresponding answer
Sample Input3 4
1 2 3
1 2
2 3
2 1
3 4Sample Output3set b[i] means a[i] can become the maximum value, C[i] represents a[i] can become the minimum value, and then you can DP. F[i]=max (f[j]| B[j]<=a[i]&&a[j]<=c[i]) +1, it is not difficult to find a three-dimensional partial order structure. First I wrote a tree line segment tree, and then I was subtly stuck in memory.
#include <cstdio> #include <cctype> #include <queue> #include <cstring> #include <algorithm > #define REP (i,s,t) for (int. i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for (int i=first[x ];i;i=next[i]) using namespace Std;const int Buffersize=1<<16;char Buffer[buffersize],*head,*tail;inline Char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin); Tail= (Head=buffer) +l; } return *head++;} inline int read () {int X=0,f=1;char c=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} const int MAXN=100010;CONST int Maxnode=13000010;const int Inf=1e9;int n,m,ans,a[maxn],b[maxn],c[maxn],f[maxn];int Root[maxn<<2],maxv[maxnode],ls[maxnode],rs[maxnode],tot;void Insert (int& o,int l,int r,int p,int val) {if (! o) O=++tot;maxv[o]=max (maxv[o],val); if (l==r) Return;int mid=l+r>>1; if (p<=mid) insert (ls[o],l,mid,p,val);else insert (rs[o],mid+1,r,p,val);} void Insert (int o,int l,int r,int x,int y,int val) {insert (root[o],1,100000,y,val); if (l==r) return; int mid=l+r>>1,lc=o<<1,rc=lc|1; if (x<=mid) insert (lc,l,mid,x,y,val); else insert (rc,mid+1,r,x,y,val);} int query (int o,int l,int R,int p) {if (!o) return 0; if (l==r) return maxv[o]; int mid=l+r>>1; if (p<=mid) return query (LS[O],L,MID,P); Return Max (Maxv[ls[o]],query (rs[o],mid+1,r,p));} int query (int o,int l,int r,int x,int y) {if (l==r) return query (root[o],1,100000,y); int mid=l+r>>1,lc=o<<1,rc=lc|1; if (x<=mid) return query (lc,l,mid,x,y); Return max (query (root[lc],1,100000,y), query (Rc,mid+1,r,x,y));} int main () {n=read (); M=read (); Rep (I,1,n) A[i]=b[i]=c[i]=read (); Rep (i,1,m) {int x=read (), Y=read (); B[x]=max (B[x],y); C[x]=min (C[x],y); } rep (i,1,n) {f[i]=query (1,1,100000,a[i],c[i]) +1; Insert (1,1,100000,b[i],a[i],f[i]); AnS=max (Ans,f[i]); } printf ("%d\n", ans); return 0;}
The
Then wrote a kd tree and ran really slow ...
#include <cstdio> #include <cctype> #include <set> #include <cstring> #include <algorithm > #define L t[o].lc#define R t[o].rc#define Rep (i,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t; i--) #define REN for (int i=first[x];i;i=next[i]) using namespace Std;const int Buffersize=1<<16;char buffer[ Buffersize],*head,*tail;inline Char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin); Tail= (head= Buffer) +l;} return *head++;} inline int read () {int X=0,f=1;char c=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} const int Maxn=100010;const int Inf=1e9;int d,n,m,ans,a[maxn],b[maxn],c[maxn],f[maxn];struct Node {int x[2],mx[2],mn[2 ],lc,rc,val,maxv;bool operator < (const node& ths) const {return x[d]<ths.x[d]| | (X[d]==ths.x[d]&&x[d^1]<ths.x[d^1]);}} T[maxn],p;void maintain (int o) {T[o].maxv=max (T[o].val,max (T[L].MAXV,T[R].MAXV));} void Build (Int& O,int l,int r,int cur) {int mid=l+r>>1;d=cur;o=mid;nth_element (t+l,t+mid,t+r+1); if (L<mid) build (L,l,mid-1, CUR^1); if (mid<r) build (r,mid+1,r,cur^1); Rep (c,0,1) {t[o].mn[c]=min (T[o].x[c],min (t[l].mn[c],t[r].mn[c))); T[o].mx[c]=max (T[o].x[c],max (T[l].mx[c],t[r].mx[c));}} int query (int o) {if (!o) return 0;if (t[o].mn[0]>p.x[0]| | T[O].MN[1]>P.X[1]) return 0;if (T[o].mx[0]<=p.x[0]&&t[o].mx[1]<=p.x[1]) return t[o].maxv;int ans=0; if (t[o].x[0]<=p.x[0]&&t[o].x[1]<=p.x[1]) Ans=t[o].val;return max (Ans,max (Query (L), query (R));} void Insert (int o,int cur,int val) {if (t[o].x[0]==p.x[0]&&t[o].x[1]==p.x[1]) T[o].val=max (t[o].val,val); else {D=cur;insert (P<t[o]? L:r,cur^1,val);} Maintain (o);} int main () {t[0].mn[0]=t[0].mn[1]=inf; T[0].mx[0]=t[0].mx[1]=-inf;n=read (); M=read (); int Rt=0;rep (i,1,n) a[i]=b[i]=c[i]=read (); Rep (i,1,m) {int x=read (), y= Read (); B[x]=max (B[x],y); C[x]=min (c[x],y);} Rep (i,1,n) t[i].x[0]=b[i],t[i].x[1]=a[i];build (rt,1,n,0); Rep (i,1,n) {p.x[0]=a[i]; P.x[1]=c[i];f[i]=query (RT) +1; P.x[0]=b[i]; P.x[1]=a[i];insert (Rt,0,f[i]); Ans=max (Ans,f[i]);} printf ("%d\n", ans); return 0;}
BZOJ4553: [tjoi2016&heoi2016] Sequence