zoukankan      html  css  js  c++  java
  • BZOJ5157 [Tjoi2014]上升子序列 【树状数组】

    题目链接

    BZOJ5157

    题解

    我们只需计算每个位置为开头产生的贡献大小,就相当于之后每个大于当前位置的位置产生的贡献 + 1之和
    离散化后用树状数组维护即可
    要注意去重,后面计算的包含之前的,记录下来减去即可

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cls(s) memset(s,0,sizeof(s))
    #define cp pair<int,int>
    #define LL long long int
    #define lbt(x) (x & -x)
    using namespace std;
    const int maxn = 100005,maxm = 100005,INF = 1000000000,P = 1000000007;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    int A[maxn],b[maxn],last[maxn],tot,n,ans;
    int getn(int x){return lower_bound(b + 1,b + 1 + tot,x) - b;}
    int s[maxn];
    void add(int u,int v){while (u <= tot) s[u] = (s[u] + v) % P,u += lbt(u);}
    int query(int u){int re = 0; while (u) re = (re + s[u]) % P,u -= lbt(u); return re;}
    int sum(int l,int r){return ((query(r) - query(l - 1)) % P + P) % P;}
    int main(){
    	n = read();
    	REP(i,n) A[i] = b[i] = read();
    	sort(b + 1,b + 1 + n); tot = 1;
    	for (int i = 2; i <= n; i++) if (b[i] != b[tot]) b[++tot] = b[i];
    	for (int i = 1; i <= n; i++) A[i] = getn(A[i]);
    	tot++;
    	for (int i = n; i; i--){
    		int tmp = sum(A[i] + 1,tot),t = tmp - last[A[i]];
    		ans = (ans + t) % P;
    		last[A[i]] = tmp;
    		tmp = ((tmp - sum(A[i],A[i]) + 1) % P + P) % P;
    		add(A[i],tmp);
    	}
    	printf("%d
    ",(ans % P + P) % P);
    	return 0;
    }
    
    
  • 相关阅读:
    Codeforces 834D The Bakery
    hdu 1394 Minimum Inversion Number
    Codeforces 837E Vasya's Function
    Codeforces 837D Round Subset
    Codeforces 825E Minimal Labels
    Codeforces 437D The Child and Zoo
    Codeforces 822D My pretty girl Noora
    Codeforces 799D Field expansion
    Codeforces 438D The Child and Sequence
    Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D)
  • 原文地址:https://www.cnblogs.com/Mychael/p/9034400.html
Copyright © 2011-2022 走看看