hdu 2227 Find the nondecreasing subsequences(樹狀數組)

來源:互聯網
上載者:User

給出一個長度為n的序列求這個序列中非遞減的序列有幾個。

1,2,3: {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.一共有7個。

 1 <= n <= 100000, 0 <= si <= 2^31.資料過大,所以需要離散化。

怎麼離散化呢?將值對應到下標。再取原數組可知每個數在什麼位置。

再將得到的下標查詢,查詢後+1,每次都是增加1,再將這個數更新進樹狀數組。說的不是很清楚,還是看代碼吧


/*Problem ID:meaning:Analyzing:*/#include <iostream>#include <algorithm>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<vector>using namespace std;typedef struct even{int y1,y2,x;}even;#define clr(A,k) memset(A,k,sizeof(A))#define FOR(i,s,t) for(int i=(s); i<(t); i++)#define LL long long#define BUG puts("here!!!")#define print(x) printf("%d\n",x)#define STOP system("pause")#define eps 1e-8#define PI acos(-1.0)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxn 100005#define maxm 1005#define MM 1<<31#define MOD  1000000007#define lowbit(x) x&(-x)LL gcd(LL a,LL b) {return a?gcd(b%a,a):b;}int S[maxn],C[maxn],A[maxn];int n,tot;void update(int pos,int val){    while(pos<=n){        C[pos]+=val;        if(C[pos]>=MOD) C[pos]%=MOD;        pos+=lowbit(pos);    }}LL query(int x){    LL ret=0;    while(x>0){        ret+=C[x];        if(ret>=MOD) ret%=MOD;        x-=lowbit(x);    }    return ret;}int Binsearch(int x){    int l=1,r=tot;    int m;    while(l<=r){        int m=(l+r)>>1;        if(S[m]==x) return m;        if(S[m]>x) r=m-1;        else l=m+1;    }}int main(){    while(~scanf("%d",&n)){        clr(C,0);        for(int i=1;i<=n;i++){            scanf("%d",&A[i]);            S[i]=A[i];        }        sort(S+1,S+n+1);        tot=1;        for(int t=2;t<=n;t++){            if(S[t]!=S[t-1]){                S[++tot]=S[t];            }        }        LL ret=0;        for(int i=1;i<n+1;i++){            int id=Binsearch(A[i]);            LL tp=query(id);            ret+=tp+1;            if(ret>=MOD) ret%=MOD;            update(id,tp+1);        }        printf("%I64d\n",ret);    }return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.