题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3030
题目大意: 通过题目描述的方法产生一个正整数列,求递增非连续子数列的个数.
思路:dp[x]=dp[i]+...+dp[j]+1 (B[]为产生的正整数列,B[i]~B[j]的值小于B[x] && i,j<x,1为B[x]本身).求和可以用线段树和树状数组.
代码:
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <math.h> #include <time.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; #define ull unsigned __int64 #define ll __int64 //#define ull unsigned long long //#define ll long long #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define middle (l+r)>>1 #define MOD 1000000007 #define esp (1e-10) const int INF=0x3F3F3F3F; //const double pi=acos(-1.0); const int N=500010; int n,m; ll a[N],b[N],sum[N],hash[N]; void mod(ll& x){x= x<MOD? x:x%MOD;} int lowbit(int x){return x&(-x);} void Add(int pos,ll c){ while(pos<=m) mod(sum[pos]+=c),pos+=lowbit(pos); } ll Sum(int pos){ ll r=0; while(pos>0) mod(r+=sum[pos]),pos-=lowbit(pos); return r; } int bs(ll key,int size,ll A[]){ int l=0,r=size-1,mid; while(l<=r){ mid=middle; if(key>A[mid]) l=mid+1; else if(key<A[mid]) r=mid-1; else return mid; } return -1; } int main(){ //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); int i,j,k,pos; ll ret,X,Y,Z,tmp; char op[2]; int T,cas;scanf("%d",&T);for(cas=1;cas<=T;cas++){ scanf("%d%d%I64d%I64d%I64d",&n,&m,&X,&Y,&Z); for(i=0;i<m;i++) scanf("%I64d",&a[i]); for(i=0;i<n;i++){ j=i%m;hash[i]=b[i]=a[j]; a[j]=(X*a[j]+Y*(i+1))%Z; } sort(hash,hash+n); for(i=m=1;i<n;i++) if(hash[i]!=hash[i-1]) hash[m++]=hash[i]; memset(sum,0,sizeof(sum)); for(i=0,ret=0;i<n;i++){ pos=bs(b[i],m,hash)+1; tmp=Sum(pos)+1; mod(ret+=tmp); Add(pos+1,tmp); } printf("Case #%d: %I64d\n",cas,ret); } return 0; }