题意:给你一个函数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. Pxe自动化安装

    Centos7环境 Systemctl stop firewalld Setenforce Yum本地源 cd /etc/yum.repos.d/ 进入/etc/yum.repos.d/ Ls 查看 ...

  2. 网络通信_socket

    socket又称套接字 使用server实现循环通信 代码如下 import socket server = socket.socket() server.bind(()) server.listen ...

  3. web之请求转发与重定向

    请求转发: 重定向:

  4. jenkins参数化配置,pom.xml配置

    1.要实现Jenkins参数化构建,要先在代码里写好能接收该参数value的配置,在pom.xml文件里加配置,如下: 1)<properties></properties>里 ...

  5. disk或者Partition镜像的制作

    备份镜像还原一般都是在client-server端这边才有涉及,不过作为平时爱折腾的咸鱼,表示偶尔玩玩这种操作也不错: 工具:pc  X  1(装有 大白菜,装机吧,一类制作pe软件的即可,大同小异) ...

  6. python连接mongodb集群

    一 安装模块pymongo pip3 install pymongo 二 创建一个MongoClient conn=MongoClient('mongodb://cbi:pass@ip1:20000, ...

  7. 检测ip是否通过

    #!/bin/bashnetstat -an |grep "ESTABLISHED" |awk '{print $4}' |awk -F ':' '{print $1}' |sor ...

  8. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  9. 推荐五个java基础学习网站,小白必备

    不知道去哪找java基础资料?推荐几个学习网站,小白必备 Java经过20多年的发展,仍然是世界上最受欢迎的编程语言之一,有无限多种方法使用Java.拥有庞大的客户群.并且java应用范围很广,基本只 ...

  10. windows 下安装python 的requests模块

    下载地址:https://codeload.github.com/requests/requests/legacy.zip/master 下载好后解压,进入目录执行下面命令 在cmd下:python ...