Codeforces 1167F 计算贡献
题意:给你一个函数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 计算贡献的更多相关文章
- Codeforces 1167F(计算贡献)
要点 容易想到排序,然后对于每个数: 人的惯性思维做法是:\(a[i]*(rank1的+rank2的+-)\).然而解法巧妙之处在于直接把所有的加和当成一个系数,然后先假装所有情况系数都是1,接着往上 ...
- 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<= ...
随机推荐
- centos 搭建本地YUM源并使用apache共享YUM源
搭建本地YUM源 1.挂载镜像 2.搭建本地YUM源 删除多余repo文件保留一个就行 本地YUM源就搭建好了 yum repolist 查看yum源 3.使用apache共享YUM源 YUM服务器配 ...
- Dev常用控件
GridControl TreeView DEV GridControl小结.. https://blog.csdn.net/happy09li/article/details/7186829 Dev ...
- irrlicht鬼火
中文鬼火 开源3d引擎 ogre osg等 libpng png图片处理 jpeg jpg图片库
- 使用 sar 查看网卡的流量
1.常用命令 sar -n DEV #查看当天从零点到当前时间的网卡流量信息 sar -n DEV 1 10 #每秒显示一次,共显示10次 sar -n DEV -f /var/log/sa/saxx ...
- Python3.5-20190507-廖老师-自我笔记-迭代
可以使用for x in 数据 的那么 这个数据就是可迭代对象. 通过计算生成下一个值的数据就是生成器 可以使用next(数据) 来计算出下一个值的数据就是迭代器(生成器属于迭代器) -------- ...
- atcoder 泛做
https://atcoder.jp/contests/arc060/tasks/arc060_b 先考虑一些特殊情况: $$n>s$$ $$n=s$$ $b$小于$sqrt(N)$可以枚举,如 ...
- ambari安装hadoop
前言 hadoop除了社区版之外,还有两个主流的分支,一个是cdh版本,一个是hdp版本,其中ambari是hdp版本的部署工具 1.ambari安装准备 https://docs.hortonwor ...
- css强制换行和超出部分隐藏实现
一.强制换行 1 word-break: break-all; 只对英文起作用,以字母作为换行依据. 2 word-wrap: break-word; 只对英文起作用,以单词作为换行依据. 3 whi ...
- 【UML】最简单的类图
Rational Rose简明实用教程 https://blog.csdn.net/gz153016/article/details/49641847 Rational Rose是Rational公 ...
- 部署.net Core 到 Windows server 2008 r2 IIs
1. 将项目发布 2.iis 新建网站,设置 3 安装windows server hosting 4 关于报错 1.下载sdk https://www.microsoft.com/net/lea ...