题意:给你一个函数f,计算∑(i = 1 to n)(j = i to n) f(i, j)。f(i, j)的定义是:取出数组中i位置到j位置的所有元素,排好序,然后把排好序的位置 * 元素 加起来。比如[7, 4, 2, 1], f(1, 4)是1 * 1 + 2 * 2 + 3 * 4  + 5 * 7;

思路:(来自PinkRabbit)我们计算每一个每一个x出现了多少次,然后加起来就是答案。我们假设一个元素y < x, 并且在x的左边,那么y对x的贡献为pos(y) * (n - pos(x) + 1)(pos(x)是x的位置),即包含y和x的区间个数。右边同理。我们用树状数组来维护.

代码:

#include <bits/stdc++.h>
#define LL long long
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define lowbit(x) (x & (-x))
using namespace std;
const int maxn = 500010;
const LL mod = 1000000007;
LL a[maxn], p[maxn];
LL n;
class BIT {
public:
LL tr[maxn];
void add(int x, LL y) {
for (; x <= n; x += lowbit(x)) {
tr[x] += y;
tr[x] %= mod;
}
} LL ask(int x) {
LL ans = 0;
for (; x; x -= lowbit(x)) {
ans += tr[x];
ans %= mod;
}
return ans;
}
};
BIT t1, t2;
bool cmp(LL x, LL y) {
return a[x] < a[y];
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
p[i] = i;
}
LL ans = 0;
sort(p + 1, p + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
LL now = p[i];
ans = (ans + (a[now] * (((n - now + 1) * now) % mod)) % mod) % mod;
ans = (ans + (a[now] * ((t1.ask(now) * (n - now + 1)) % mod + (t2.ask(n - now + 1) * now) % mod) % mod) % mod) % mod;
t1.add(now, now);
t2.add(n - now + 1, n - now + 1);
}
printf("%lld\n", ans);
}

  

Codeforces 1167F 计算贡献的更多相关文章

  1. Codeforces 1167F(计算贡献)

    要点 容易想到排序,然后对于每个数: 人的惯性思维做法是:\(a[i]*(rank1的+rank2的+-)\).然而解法巧妙之处在于直接把所有的加和当成一个系数,然后先假装所有情况系数都是1,接着往上 ...

  2. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

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

  4. Codeforces Round #574 (Div. 2) D1. Submarine in the Rybinsk Sea (easy edition) 【计算贡献】

    一.题目 D1. Submarine in the Rybinsk Sea (easy edition) 二.分析 简单版本的话,因为给定的a的长度都是定的,那么我们就无需去考虑其他的,只用计算ai的 ...

  5. Codeforces 1119D(贡献计算)

    题目传送 排序看一看. 关键点在于发现性质: 算一个点的贡献时: 1.与后一个有重叠.\[当 a[i] + r >= a[i + 1] + l, 即 r - l >= a[i + 1] - ...

  6. Codeforces 360C DP 计算贡献

    题意:给你一个长度为n的字符串,定义两个字符串的相关度为两个串对应的子串中第一个串字典序大于第二个串的个数.现在给你相关度,和第二个串,问满足条件的第一个串有多少个? 思路:设dp[i][j]为填了前 ...

  7. Codeforces Round #574 (Div. 2) D2. Submarine in the Rybinsk Sea (hard edition) 【计算贡献】

    一.题目 D2. Submarine in the Rybinsk Sea (hard edition) 二.分析 相比于简单版本,它的复杂地方在于对于不同长度,可能对每个点的贡献可能是有差异的. 但 ...

  8. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  9. 牛客练习赛42 C 反着计算贡献

    https://ac.nowcoder.com/acm/contest/393/C 题意 给你一个矩阵, 每次从每行挑选一个数,组成一个排列,排列的和为不重复数字之和,求所有排列的和(n,m<= ...

随机推荐

  1. MYSQL如何优化?

    MYSQL如何优化?结合你的经验 1.数据库的设计尽量把数据库设计的更小的占磁盘空间.1).尽可能使用更小的整数类型.(mediumint就比int更合适).2).尽可能的定义字段为not null, ...

  2. 一次goldengate故障引发的操作系统hang起,HA自动切换

    现场: 跑着数据库的主机A报警应用连接不上数据库,我们无法ssh到主机.第一反应是通过telnet到远程控制口,发现数据库资源和硬件资源在被切换到HA架构的主机B(备机,通常性能比主机A的差,抗不住应 ...

  3. 转载 jQuery实现放大镜特效

    效果图. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  4. iview table的render()函数的用法

    语法:render:(h,params)=>{} render:(h,params) => { return h(" 定义的元素 ",{ 元素的性质 }," ...

  5. spring在WEB中的应用。

    1:创建IOC容器.在WEB应用程序启动的时候就创建.利用到监听器. ServletContextListener类的contextInitialized方法中 package com.struts2 ...

  6. 树莓派配置freeopcua

    一.下载freeopcua的源码 git clone https://github.com/FreeOpcUa/freeopcua.git 切换回2016-10-8的版本 git reset 8c16 ...

  7. Fidder的使用

    默认的header是类似这样的 User-Agent: Fiddler Host: localhost Content-Length: 34 只需要改成这样的 User-Agent: Fiddler ...

  8. haproxy笔记

    haproxy安装.启动.日志配置 方法1:#安装 yum install haproxy -y #日志配置 sed -i 's/^#$ModLoad imudp/$ModLoad imudp/g' ...

  9. 46、tensorflow入门初步,手写识别0,1,2,3,4,5,6

    1.使用tensorflow的SoftMax函数,对手写数字进行识别 Administrator@SuperComputer MINGW64 ~ $ docker run -it -p 8888:88 ...

  10. Python机器学习及分析工具:Scikit-learn篇

    https://www.jianshu.com/p/e0844e7cdba5 https://sklearn.apachecn.org/docs/0.21.3/62.html 中文文档