题意:求有长为m的序列生成的长度为n的序列的上升子序列的个数
思路:生成完长为n的序列后,首先我们想到(nlogn)的求上升序列的方法,然后再这个基础上改进,每插入一个的时候,我们可以得到左边的小于它的个数,然后我们就可以得新增加的上升子序列是sum(id-1)+1,然后更新树状数组
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; const int MOD = 1000000007; const int MAXN = 500010; struct Node{ int id; ll x; }node[MAXN]; ll c[MAXN],a[MAXN],val[MAXN]; ll x,y,z; int b[MAXN],n,m; bool cmp(Node a,Node b){ return a.x < b.x; } int lowbit(int x){ return x&(-x); } void update(int x,ll d){ while (x <= n){ c[x] += d; x += lowbit(x); c[x] %= MOD; } } ll sum(int x){ ll ans = 0; while (x > 0){ ans += c[x]; ans %= MOD; x -= lowbit(x); } return ans; } int main(){ int t,cas=1; scanf("%d",&t); while (t--){ scanf("%d%d%lld%lld%lld",&n,&m,&x,&y,&z); for (int i = 0; i < m; i++) scanf("%lld",&a[i]); for (int i = 0; i < n; i++){ b[i+1] = val[i+1] = a[i%m]; a[i%m] = (x*a[i%m]+y*(i+1))%z; } sort(b+1,b+1+n); memset(c,0,sizeof(c)); ll ans = 0; node[0].id = -1; for (int i = 1; i <= n; i++){ int id = lower_bound(b+1,b+1+n,val[i])-b; ll tot = sum(id-1); ans += tot+1; ans %= MOD; update(id,tot+1); } printf("Case #%d: ",cas++); cout << ans << endl; } return 0; }
HDU - 3030 Increasing Speed Limits,布布扣,bubuko.com
HDU - 3030 Increasing Speed Limits
原文:http://blog.csdn.net/u011345136/article/details/23535967