Codeforces 1167 F Scalar Queries 计算贡献+树状数组
题意
给一个数列\(a\),定义\(f(l,r)\)为\(b_1, b_2, \dots, b_{r - l + 1}\),\(b_i = a_{l - 1 + i}\),将\(b\)排序,\(f(l,r)\)=\(\sum\limits_{i = 1}^{r - l + 1}{b_i \cdot i}\)
计算\(\left(\sum\limits_{1 \le l \le r \le n}{f(l, r)}\right) \mod (10^9+7)\)
分析
考虑每个数字对答案的贡献,首先每个\(a_i\)的区间贡献为\((n-i+1)\times i\times a_i\)
在\(a_i\)左边的比它小的数\(a_j\)的贡献为\((n-i+1)\times j\times a_i\)
在\(a_i\)右边的比它小的数\(a_j\)的贡献为\(i\times (n-j+1)\times a_i\)
将所有贡献加起来即为答案
按排序后的下标建树状数组,维护原下标前缀和,边更新边加答案
Code
#include<bits/stdc++.h>
#define fi first
#define se second
#define bug cout<<"--------------"<<endl
using namespace std;
typedef long long ll;
const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=1e9;
const ll llf=1e18;
const int mod=1e9+7;
const int maxn=5e5+10;
int n;
ll a[maxn],c[maxn],tr[maxn];
void add(int x,ll k){
while(x<=n){
tr[x]=(tr[x]+k)%mod;
x+=x&-x;
}
}
ll dor(int x){
ll ret=0;
while(x){
ret=(ret+tr[x])%mod;
x-=x&-x;
}
return ret;
}
ll ans=0;
void solve(){
memset(tr,0,sizeof(tr));
for(int i=1;i<=n;i++){
ll t=dor(a[i])*(n-i+1)%mod;
ans+=c[a[i]]*t%mod;
ans%=mod;
add(a[i],i);
}
}
int main(){
ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
ans+=a[i]*(n-i+1)%mod*i%mod;
ans%=mod;
c[i]=a[i];
}
sort(c+1,c+n+1);
for(int i=1;i<=n;i++){
a[i]=lower_bound(c+1,c+n+1,a[i])-c;
}
solve();
reverse(a+1,a+n+1);
solve();
cout<<ans<<endl;
return 0;
}
Codeforces 1167 F Scalar Queries 计算贡献+树状数组的更多相关文章
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- codeforces 703D D. Mishka and Interesting sum(树状数组)
题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megaby ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- codeforces 985 E. Pencils and Boxes (dp 树状数组)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- Codeforces 1076E Vasya and a Tree(树状数组)或dfs
题意:给你一颗以1为根节点的树,初始所有节点的权值为0,然后有m个操作,每个操作将点x的所有距离不超过d的节点权值+1,问经过m次操作后每个节点权值是多少? 思路:如果是一个序列,就可以直接用树状数组 ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
随机推荐
- c# TCP/IP协议利用Socket Client通信(只含客户端Demo)
完全是基础,新手可以随意看看,大牛可以关闭浏览页了,哈哈. TCP/IP协议 TCP/IP是一系列网络通信协议的统称,其中最核心的两个协议是TCP和IP.TCP称为传输控制协议,IP称为互联网络协议. ...
- Struts的相关基础
为什么要用struts? 1.该框架基予mvc的开发设计模式上的,所以拥有mvc的全部优点,他在M.V.C上都有涉及,但它主要是提供一个好的控制器和一套定制的标签库上,有mvc的一系列优点:层次分明, ...
- java写webservice接口
有一个需求:要求根据设备mac和终端设备类型来查询设备库存状态. 接口协议是采用webservice协议,信息交互方式为xml格式信息 输入参数存放到XML各个节点下,并转为一个String,作为接口 ...
- centos7 修改时区,同步时间,Mysql修改时区
查看时区 timedatectl status [root@localhost nova-back]# timedatectl status Local time: Thu 2019-05-23 15 ...
- day12 css样式
目录 1.标签分类 2.浮动布局 3.margin塌陷 4.定位postion 5.背景图 一. 标签分类 默认在标准文档流 行内标签 span,a,em,i,strong,b,inp ...
- <%%> <%! %> <%=%> <%-- --%> jsp中jstl一些运用
<%%> 这里面可以添加java代码片段<%! %> 这里添加java方法 主要是用来声明变量的 <%=%> 将变量或表达式值输出到页面<%-- --%> ...
- Java十进制转二进制
闲着没事写了个简单的十进制转二进制的算法,很简单,个人记录一下,不妥之处请指正. public static String toBinaryString(int j) { if (j < 0) ...
- Shell脚本——for,while,until循环
1.for循环: 语句格式: for i in 循环判断 do 循环体 done 举例:九九乘法表(for循环版本) #!/bin/bash # Author: Sean Martin # Blog: ...
- Phoenix实现分页查询
1 利用offset语法,官网的语法 ELECT * FROM TEST LIMIT 1000; SELECT * FROM TEST LIMIT 1000 OFFSET 100; 2 公式如下 SE ...
- Jumpserver1.4.1安装
第1章 CentOS环境准备 Jumpserver官网: http://docs.jumpserver.org/zh/docs/step_by_step.html 测试推荐硬件 CPU: 64位双核处 ...