有n个数,给定一个k,求所有长度大于等于k的区间中前k大数的总和。这样就比较简单相信大家都会,所以此题要求当k=1~n的总和,即求

∑nk=1∑n−k+1i=1∑nj=i+k−1  区间前K大和

Input
输入五个数n,a1,A,B,C。a1表示第一个数,A,B,C用来生成其余n-1个数。a(i)=(a(i-1)*A+B)mod C。1<=n<=1,000,000,0<=a1,A,B,C<=1,000,000,000
Output
一个数表示答案,最后答案对1,000,000,007取模。
Input示例
3 3 1 1 10
Output示例63样例解释:
三个数为3,4,5
K=1:[1,1]=3,[1,2]=[2,2]=4,[1,3]=[2,3]=[3,3]=5(表示各个区间在k=1时的答案)
K=2:[1,2]=7,[2,3]=[1,3]=9
K=3:[1,3]=12

题目大意:求sigma(k=1--n) f(k),k为区间大于等于k的前k大的和。 题解:树状数组
对于区间[l,r],假设它的答案为ans,如果新增一个数a[r+1],那么它
出现的次数就是[l,r]中小于它的数的个数。如果Ai<Aj,i<j,那么包含这两个数
的区间的个数为i*(n-j+1),则对答案的贡献为Aj*(n-j+1),对于Aj>Ai,i>j,
倒过来再处理一遍。注意:双关键字排序 代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define mod 1000000007
#define maxn 1000009
#define LL long long
using namespace std; LL n,ans,A,B,C,a[maxn],tree[maxn];
typedef pair<LL,int> PII;
PII b[maxn]; void add(int x,int p){
for(;x<=n;x+=x&(-x))tree[x]=(tree[x]+p)%mod;
} LL getsum(int x){
LL all=;
for(;x;x-=x&(-x))all=(all+tree[x])%mod;
return all;
} int main(){
scanf("%lld%lld%lld%lld%lld",&n,&a[],&A,&B,&C);
b[].first=a[];b[].second=;
for(int i=;i<=n;i++){
a[i]=(a[i-]*A+B)%C;
b[i].first=a[i];b[i].second=i;
}
sort(b+,b+n+);
for(int i=;i<=n;i++)
a[i]=lower_bound(b+,b+n+,make_pair(a[i],i))-b;
for(int i=;i<=n;i++){
add(a[i],i);
LL tmp=b[a[i]].first*1LL*(n-i+)%mod;
LL amp=getsum(a[i]);
ans=(ans%mod+tmp*amp%mod)%mod;
}
memset(tree,,sizeof(tree));
for(int i=n;i>=;i--){
LL tmp=(b[a[i]].first*1LL*i)%mod;
LL amp=getsum(a[i]);
ans=(ans%mod+tmp*amp%mod)%mod;
add(a[i],n-i+);
}
printf("%lld\n",ans);
return ;
}
 

51nod1680 区间求和的更多相关文章

  1. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. vijos1740 聪明的质监员 (二分、区间求和)

    http://www.rqnoj.cn/problem/657 https://www.vijos.org/p/1740 P1740聪明的质检员 请登录后递交 标签:NOIP提高组2011[显示标签] ...

  4. LightOJ 1112 Curious Robin Hood (单点更新+区间求和)

    http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i        将第i个数值输出,并将第i个值清0 2 i v     ...

  5. POJ 3468 A Simple Problem with Integers(线段树区间求和)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  6. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  7. poj3468树状数组的区间更新,区间求和

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 47174   ...

  8. D 区间求和 [数学 树状数组]

    D 区间求和 题意:求 \[ \sum_{k=1}^n \sum_{l=1}^{n-k+1} \sum_{r=l+k-1}^n 区间前k大值和 \] 比赛时因为被B卡了没有深入想这道题 结果B没做出来 ...

  9. [用CDQ分治解决区间加&区间求和]【习作】

    [前言] 作为一个什么数据结构都不会只会CDQ分治和分块的蒟蒻,面对区间加&区间求和这么难的问题,怎么可能会写线段树呢 于是,用CDQ分治解决区间加&区间求和这篇习作应运而生 [Par ...

随机推荐

  1. oc字符串+数组+字典操作题目

    1. 判断中间目录是否存在 (10分) 比如 传入字符串 @"/home/qianfeng/oc.txt" 和 @"qianfeng" 返回:YES 传入字符串 ...

  2. 快乐学习 Ionic Framework+PhoneGap 手册1-2{介绍Header,Content,Footer的使用}

    *先运行第一个简单的APP,介绍Header,Content,Footer的使用 {2.1}运行一个简单的APP,效果如下 {2.2}Header代码 <ion-header-bar class ...

  3. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  4. centos7下安装jdk7

     CentOS7.1 JDK安装 1.卸载自带OPENJDK    用 java -version 命令查看当前jdk版本信息   #java -version    用rpm -qa | grep ...

  5. MyBatis联合查询association使用

    1.需求 两张表 channels(频道表)  member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...

  6. 通过代码或者配置文件 对log4net进行配置

    1.通过代码进行配置 1.1代码 http://stackoverflow.com/questions/16336917/can-you-configure-log4net-in-code-inste ...

  7. Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'company' in 'class java.lang.String'

    Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' ...

  8. Java -- 利用反射 操作任意数组,包括对象数组 和 基本数据类型的数组

    items为任意数组

  9. hdu 5884 Sort 队列+多叉哈夫曼树

    Sort Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Des ...

  10. jQuery Fancybox插件使用参数详解

    Fancybox的特点如下: 可以支持图片.html文本.flash动画.iframe以及ajax的支持 可以自定义播放器的CSS样式 可以以组的形式进行播放 如果将鼠标滚动插件(mouse whee ...