Uvalive 6667 longest chain (divide and conquer three-dimensional LIS)

Source: Internet
Author: User

Question:

The question gives a defined less than the number, and then finds a lis...


Train of Thought Analysis:

This question is strictly incrementing, similar to HDU 4742. However, HDU is a sequence that does not decrease.

Briefly describe the practice of HDU 4742.

First, we can think of a one-dimensional Lis, which is simply n.

Then the two-dimensional Lis sorts the one-dimensional first, and then calculates the second-dimensional Lis.

Now the problem is extended to 3D. It is still sorted in one dimension.

Assume that we are sorting Z.

Then write down the sorted ID. It is known that the Z with a small ID is small.

Then start the grouping, similar to the recursion of the Line Segment tree. For an interval, all the elements in the interval are obtained and sorted by Y. Obtain another sequence.

For this sequence, the information we have is the ID of each element, and this sequence is ordered by Y, So we add it to the tree array through the order of Y from large to small. Determine the ID when adding.

If the ID is on the left, update it. If the ID is on the right, no update is required. Why do I not update the right side of an edge. Because we can only determine that the ID on the right is larger than the ID on the left, while the ID on the same side is smaller than the ID size.

So we use the left side to update the DP on the right.

After being inserted, the system determines the number of X in BITs, which is smaller than the number of X in bits.


Now the problem has become strictly increasing.

The final judgment is equal.

For the X in bits, we can directly ask for 1-z-1. This is easy to think.

For y, if y is equal when we sort by Division and governance, we will put the Z size before it.

For X, we will open another bit, and put all together, and then put together different from Mid + 1.


#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <utility>#define lowbit(x) (x&(-x))#define maxn 300005using namespace std;struct node{    int x,y,z,id;    bool operator < (const node &cmp)const    {        if(x!=cmp.x)return x<cmp.x;        if(y!=cmp.y)return y<cmp.y;        return z<cmp.z;    }}p[maxn],b[maxn];bool cmp_yz(node a,node b){    if(a.y!=b.y)return a.y<b.y;    return a.z>b.z;}int x[maxn];int bit[2][maxn],dp[maxn];int m;void update(int &a,int b){    a=max(a,b);}void add(int index,int val,int g){    for(int idx=index;idx<=m;idx+=lowbit(idx))        update(bit[g][idx],val);}int query(int index,int g){    int res=0;    for(int idx=index;idx>=1;idx-=lowbit(idx))    {        update(res,bit[g][idx]);    }    return res;}void Clear(int index,int g){    for(int idx=index;idx<=m;idx+=lowbit(idx))        bit[g][idx]=0;}void solve(int l,int r){    if(l==r)return;    int mid=(l+r)>>1;    solve(l,mid);    int cnt=1;    for(int i=l;i<=r;i++)    {        b[cnt]=p[i];        b[cnt++].x=0;    }    int tx=p[mid+1].x;    sort(b+1,b+cnt,cmp_yz);    for(int i=1;i<cnt;i++)    {        if(b[i].id<=mid)        {            add(b[i].z,dp[b[i].id],0);            if(p[b[i].id].x!=tx)add(b[i].z,dp[b[i].id],1);        }        else        {            int t;            if(p[b[i].id].x!=tx)t=query(b[i].z-1,0);            else t=query(b[i].z-1,1);            t++;            update(dp[b[i].id],t);        }    }    for(int i=1;i<cnt;i++)        if(b[i].id<=mid)        {            Clear(b[i].z,0);            Clear(b[i].z,1);        }    solve(mid+1,r);}int ta , tb , C = ~(1<<31), M = (1<<16)-1;int r() {    ta = 36969 * (ta & M) + (ta >> 16);    tb = 18000 * (tb & M) + (tb >> 16);    return (C & ((ta << 16) + tb)) % 1000000;}int main(){    int n,tm;    while(scanf("%d%d%d%d",&n,&tm,&ta,&tb)!=EOF)    {        if(n+tm+ta+tb==0)break;        for(int i=1;i<=n;i++)        {            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);            x[i]=p[i].z;            dp[i]=1;            bit[0][i]=bit[1][i]=0;        }        for(int i=n+1;i<=n+tm;i++)        {            p[i].x=r();            p[i].y=r();            p[i].z=r();            dp[i]=1;            bit[0][i]=bit[1][i]=0;            x[i]=p[i].z;        }        n+=tm;        sort(p+1,p+1+n);        sort(x+1,x+1+n);        m=unique(x+1,x+1+n)-x-1;        for(int i=1;i<=n;i++)        {            p[i].z=lower_bound(x+1,x+1+m,p[i].z)-x;            p[i].id=i;        }        solve(1,n);        int ans=0;        for(int i=1;i<=n;i++)            update(ans,dp[i]);        printf("%d\n",ans);    }    return 0;}/*6 0 1 10 0 00 2 21 1 12 0 22 2 02 2 25 0 1 10 0 01 1 12 2 23 3 34 4 410 0 1 13 0 02 1 02 0 11 2 01 1 11 0 20 3 00 2 10 1 20 0 30 10 1 15 0 0 01 1 12 1 23 1 34 1 45 1 50 0 0 0*/



Uvalive 6667 longest chain (divide and conquer three-dimensional LIS)

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.