Codeforces 1167F(计算贡献)
要点
- 容易想到排序,然后对于每个数:
- 人的惯性思维做法是:\(a[i]*(rank1的+rank2的+…)\)。然而解法巧妙之处在于直接把所有的加和当成一个系数,然后先假装所有情况系数都是1,接着往上加,树状数组记录着所有之前比它小的数的情况,只有这些小的数也同时存在的区间才会增大它的系数。而且只在乎数量,不关注具体方案。
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
int n, pos[maxn];
ll a[maxn];
ll ans;
struct FenwickTree {
ll F[maxn], T[maxn];
void Update1(int x, ll val) {
for (; x <= n; x += x&-x)
F[x] += val, F[x] %= mod;
}
ll Query1(int x) {
ll res = 0;
for (; x; x -= x&-x)
res += F[x], res %= mod;
return res;
}
void Update2(int x, ll val) {
for (; x; x -= x&-x)
T[x] += val, T[x] %= mod;
}
ll Query2(int x) {
ll res = 0;
for (; x <= n; x += x&-x)
res += T[x], T[x] %= mod;
return res;
}
}bit;
int main() {
read(n);
rep(i, 1, n) read(a[i]), pos[i] = i;
sort(pos + 1, pos + 1 + n, [](int x, int y) { return a[x] < a[y]; });
rep(i, 1, n) {
int p = pos[i];
//至少系数也得是1吧
ans += a[p] * p % mod * (n - p + 1) % mod; ans %= mod;
//然而有比自己小的,把系数挤得大于1
ans += a[p] * (bit.Query1(p - 1) * (n - p + 1) % mod + bit.Query2(p + 1) * p % mod) % mod; ans %= mod;
bit.Update1(p, p), bit.Update2(p, n - p + 1);
}
writeln(ans);
return 0;
}
Codeforces 1167F(计算贡献)的更多相关文章
- Codeforces 1167F 计算贡献
题意:给你一个函数f,计算∑(i = 1 to n)(j = i to n) f(i, j).f(i, j)的定义是:取出数组中i位置到j位置的所有元素,排好序,然后把排好序的位置 * 元素 加起来. ...
- codeforces#1167F. Scalar Queries(树状数组+求贡献)
题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...
- 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 ...
- Codeforces Round #574 (Div. 2) D1. Submarine in the Rybinsk Sea (easy edition) 【计算贡献】
一.题目 D1. Submarine in the Rybinsk Sea (easy edition) 二.分析 简单版本的话,因为给定的a的长度都是定的,那么我们就无需去考虑其他的,只用计算ai的 ...
- Codeforces 1119D(贡献计算)
题目传送 排序看一看. 关键点在于发现性质: 算一个点的贡献时: 1.与后一个有重叠.\[当 a[i] + r >= a[i + 1] + l, 即 r - l >= a[i + 1] - ...
- Codeforces 360C DP 计算贡献
题意:给你一个长度为n的字符串,定义两个字符串的相关度为两个串对应的子串中第一个串字典序大于第二个串的个数.现在给你相关度,和第二个串,问满足条件的第一个串有多少个? 思路:设dp[i][j]为填了前 ...
- Codeforces Round #574 (Div. 2) D2. Submarine in the Rybinsk Sea (hard edition) 【计算贡献】
一.题目 D2. Submarine in the Rybinsk Sea (hard edition) 二.分析 相比于简单版本,它的复杂地方在于对于不同长度,可能对每个点的贡献可能是有差异的. 但 ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- 牛客练习赛42 C 反着计算贡献
https://ac.nowcoder.com/acm/contest/393/C 题意 给你一个矩阵, 每次从每行挑选一个数,组成一个排列,排列的和为不重复数字之和,求所有排列的和(n,m<= ...
随机推荐
- 2017SN多校D1T1 loveletter:模拟
题意: 两张n*m的纸.一张透明纸,上面有些地方是黑的,有些地方是透明的.另一张纸上写满了字母. 你需要将透明纸放在写字的纸上,分别顺时针旋转0°.90°.180°.270°,每次从上到下.从左到右写 ...
- 获取url的参数
function getPrams () { var url, urlPrams, urlPramsArr=[], item=[], prams={}; url = location.href; if ...
- codeforces 660D D. Number of Parallelograms(计算几何)
题目链接: D. Number of Parallelograms time limit per test 4 seconds memory limit per test 256 megabytes ...
- Python 微信通知 先挖个坑
桑心病狂,试试把报警信息发到微信上 原文 https://segmentfault.com/a/1190000009717078
- 【Lintcode】088.Lowest Common Ancestor
题目: Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two n ...
- bzoj 3456 城市规划 —— 分治FFT / 多项式求逆 / 指数型生成函数(多项式求ln)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 首先考虑DP做法,正难则反,考虑所有情况减去不连通的情况: 而不连通的情况就是那个经典 ...
- Jasper-template
ylbtech-Jasper: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech. ...
- PowerDesigner 导出 Excel
http://www.cnblogs.com/hggc/archive/2013/10/15/3369857.html
- winform 控件合集2
http://www.cnblogs.com/peterzb/archive/2009/06/18/1505424.html 包含自定义绘制的ListBox, 带拖动,图片显示, 内嵌其它控件, 打印 ...
- Linux命令总结_文件的输入与 输出
1.echo命令 例子:echo string 解释:echo命令用于显示文本或变量,或者把字符串输入到文件,常用的几个特殊字符有以下几个 \c 不换行 \f 进纸 \t 调格 \n 换行 例子:ec ...