要点

  • 容易想到排序,然后对于每个数:
  • 人的惯性思维做法是:\(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(计算贡献)的更多相关文章

  1. Codeforces 1167F 计算贡献

    题意:给你一个函数f,计算∑(i = 1 to n)(j = i to n) f(i, j).f(i, j)的定义是:取出数组中i位置到j位置的所有元素,排好序,然后把排好序的位置 * 元素 加起来. ...

  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. ffmpeg拼接mp4视频

    首先需要把mp4格式的文件转成ts格式.拼接好之后,再将ts封装格式转换回mp4. ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4to ...

  2. struts2标签(转)

    Struts2 标签库讲解   要使用Struts2的标签,只需要在JSP页面添加如下一行定义即可: <%@ taglib prefix="s" uri="/str ...

  3. L89

    His voice was hoarse after several hours' speech.Attributive adjectives precede the noun.I gave the ...

  4. T57

    “期待使我产生了介于幸福与恐惧之间的激动”The anticipation produced in me a sensation somewhat between bliss and fear他猛一下 ...

  5. 四连测Day4

    四连爆炸 卡我常数 好像被AluminumGod拉到了创客...哇我这个天天爆炸的水平可能会被其他三位dalao吊起来打 orz Edmond-Karp_XiongGod orz Deidara_Wa ...

  6. 【Lintcode】119.Edit Distance

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  7. 程序设计中的计算复用(Computational Reuse)

    从斐波那契数列说起 我想几乎每一个程序员对斐波那契(Fibonacci)数列都不会陌生,在很多教科书或文章中涉及到递归或计算复杂性的地方都会将计算斐波那契数列的程序作为经典示例.如果现在让你以最快的速 ...

  8. 洛谷【P4883】mzf的考验

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  9. android和iOS平台的崩溃捕获和收集

    转自:http://www.cnblogs.com/lancidie/archive/2013/04/13/3019349.html 通过崩溃捕获和收集,可以收集到已发布应用(游戏)的异常,以便开发人 ...

  10. appium连真机问题

    adb devices -l 后出现:List of devices attached 解决方法:用管理员身份运行以上命令 adb kill-server adb start-server adb d ...