题意

给一个数列\(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 计算贡献+树状数组的更多相关文章

  1. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  2. 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 ...

  3. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  4. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  5. 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 ...

  6. CodeForces 380C Sereja and Brackets(扫描线+树状数组)

    [题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...

  7. 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 ...

  8. Codeforces 1076E Vasya and a Tree(树状数组)或dfs

    题意:给你一颗以1为根节点的树,初始所有节点的权值为0,然后有m个操作,每个操作将点x的所有距离不超过d的节点权值+1,问经过m次操作后每个节点权值是多少? 思路:如果是一个序列,就可以直接用树状数组 ...

  9. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

随机推荐

  1. k8s-高可用架构设计

    docker的私有仓库harbor.容器化kubernetes部分组建.使用阿里云日志服务收集日志. 部署完成后,你将理解系统各组件的交互原理,进而能快速解决实际问题,所以本文档主要适合于那些有一定k ...

  2. Boot-crm管理系统开发教程(三)

    (ps:前两章我们已经把管理员登录和查看用户的功能实现了,那么今天我们将要实现:添加用户,删除用户,和修改用户功能) 由于Cusomer的POJO类型已经写好了,所以这次我们之前从CustomerCo ...

  3. Hyperledger Fabric-sdk-java

    Hyperledger Fabric-sdk-java 2018年04月18日 23:36:02 l_ricardo 阅读数 975更多 分类专栏: 区块链 java   版权声明:本文为博主原创文章 ...

  4. 开发过程遇到的css样式问题记录

    一.移动端 1.部分安卓机圆角border-radius失效,显示为方形状?   background-clip: padding-box; 2.部分安卓机字体图标出现锯齿? 使用iconfont图标 ...

  5. vs2010 回车、退格键等不能用

    有时候在vs2010中,突然回退键.回车键.方向键就用不了了,百度一堆方法,最后找到按Alt+Enter,就可以用了.

  6. 服务端相关知识学习(一)之什么是zookeeper

    什么是zookeeper zookeeper是分布式协调服务,可以在分布式系统中共享配置.协调锁资源.提供命名服务那分布式协调服务又是个什么东西呢?首先我们来看“协调”是什么意思.在一个并发的环境里, ...

  7. luogu题解 UVA11992 【Fast Matrix Operations】

    题目链接: https://www.luogu.org/problemnew/show/UVA11992 题目大意: 一个r*c的矩阵,一开始元素都是0,然后给你m次三种操作,分别是将一个子矩阵中所有 ...

  8. [转载]为什么用Java开发桌面程序的比较少

    这个是本人这几天用javafx开发桌面程序时候想到的,因为虽然javafx比C#之类的确实坑多不少,但是习惯了之后也还行,而且它的一次编译.各处使用真的深得我心.但为什么不流行呢: 作者:bell 来 ...

  9. [leetcode] 题解记录 1-10

    博客园markdown太烂, 题解详见https://github.com/TangliziGit/leetcode/blob/master/solution/1-10.md Leetcode Sol ...

  10. JavaScript的常用浏览器设置

    用什么浏览器?如果您不告诉我您使用的浏览器,我将告诉您有关JavaScript的常用浏览器设置.~火狐在菜单栏中选择工具->选项->内容以查看启用javascript的选项.Interne ...