"Bzoj 10 continuous Test" [noip2016 10 eighth field]problem B: Descending son of Levin (longest ascending subsequence + segment Tree |next array)

Source: Internet
Author: User
Tags first row time limit
problem B: [noip2016 10 even test eighth] descending son of LevinTime Limit:10 Sec Memory limit:512 MB
submit:39 solved:20
[Submit] [Status] [Web Board] Description

Son of Levin Harmon is fond of thunder, he wants to find magic lightning. Harmon has n wires in a row, each wire has a resistance value, magic light can only from a wire to the higher resistance than it, and must be from the left to the right conduction, of course, the wire does not have to be continuous. Harmon wanted to know how many wires the lightning could pass, and how many of these were there. Input The first row of two integers n and type. Type represents the data type
The second row of n integers represents the resistor.
n<=100000, the resistor value does not exceed 100000 Output The first line an integer indicates how many wires the lightning can pass.
If the type=1 needs to output the second line, indicating the number of scenarios, modulo 123456789.

Sample Input

5 1
1 3 2 5 4
Sample Output
3
4
HINT "Solving" "the longest ascending subsequence +next Array (good felling, in fact, it should be a line tree, but the data is very weak ...) )】
"O (n log n) to find the longest ascending subsequence, use the heap to do"
#include <cstdio> #include <cstring> #include <algorithm> #define MOD 123456789 using namespace std;
int f[100010],top,g[100010],ans[100010];
int A[100010],nxt[100010],p[100010],num[100010],tot;
int n,opt;  inline void Add (int x,int y,int val) {tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot; num[tot]=val;} inline void Solve (int
    X,int val) {if (x==1) {Add (x,val,1); ans[x]++; return;}
    int cnt=0;
      For (int. I=p[x-1];i!=-1;i=nxt[i]) if (a[i]>=val) break;
    else Cnt+=num[i],cnt%=mod; ans[x]+=cnt;
    Ans[x]%=mod;
Add (X,val,cnt%mod);
    } inline void Ask (int nm,int len,int x) {int l=1,r=len,mid;
        while (l<=r) {mid= (l+r) >>1;
         if (f[mid]<x) l=mid+1;
     else r=mid-1; 
    } f[l]=x;
if (opt==1) g[nm]=l,solve (l,x);
    } int main () {//freopen ("hamon9.in", "R", stdin);
    int i,j;
    memset (P,-1,sizeof (p));
    memset (nxt,-1,sizeof (NXT));
    scanf ("%d%d", &n,&opt);
     for (I=1;i<=n;++i) {   int x;
        scanf ("%d", &x);
            if (X>f[top]) {f[++top]=x;
          if (opt==1) g[i]=top,solve (top,x);
     } else ask (i,top,x);
    } if (opt==1) printf ("%d\n%d\n", top,ans[top]%mod);
    else printf ("%d\n", top);
return 0; }


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.