[YCOJ 1059] rise longest sequence

1059-- rise longest sequence

Description

Maintaining a sequence, it may be the following two operations:
1. Add the end of a digital x
2. The entire sequence becomes like the x-th operation
after each operation, the output rises up to the current sequence of sequence the length of the
time series is initially empty

Input

The first line has a positive integer n, the number of operations.
The next n lines each have two integers op, x. If op is 0, then x add this number; if op is 1, then x times after the first operation back.

Output

For each operation, the output of an answer, indicates the longest length of the current rise in sequence.

Sample Input

5
0 2
0 0
1 0
1 0
0 5

Sample Output

1
1
0
0
1

Hint

30% of the data: n≤1000;
the other 20% of data is not a second operation
80% of data: n≤200000;
100% data: n≤500000, and the digital inputs are all long integer in the range of the non-negative integer

Source

2014NOIP simulation title

Hide Information »

 Solution: I feel a bit hard ah ah ah ah DP title, LIS advanced version of Oh

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const ll N=500002;
const ll oo=10000000000000000;
ll op[N],f[N],head[N];
ll a[N];
int ans[N];
struct node{
    ll next;
    ll to;
}e[N];
ll cnt,n;
void add(ll u,ll v){
    cnt++; e[cnt].to=v;
    e[cnt].next=head[u]; head[u]=cnt;
}

void dfs(ll  now){
    if(op[now]==1){
        ans[now]=lower_bound(f,f+1+n,oo)-(f+1);
        for(ll i=head[now];i;i=e[i].next)
            dfs(e[i].to);
    } 
    if(op[now]==0){
        ll ps=lower_bound(f,f+1+n,a[now])-f;
        ll tmp=f[ps]; f[ps]=a[now];
        ans[now]=lower_bound(f,f+1+n,oo)-(f+1);
        for(ll i=head[now];i;i=e[i].next)
            dfs(e[i].to);
        f[ps]=tmp;
    }
}

int main(){
    freopen("1059.in","r",stdin);
    freopen("1059.out","w",stdout);
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>op[i]>>a[i];
        if(op[i]==1) add(a[i],i);
        else add(i-1,i);
    }
    op[0]=1;
    for(ll i=1;i<=n;i++) f[i]=oo; dfs(0);
    for(ll i=1;i<=n;i++)
        printf("%d\n",ans[i]);
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11784821.html