Scalar Queries CodeForces - 1167F (计数,树状数组)
You are given an array $a_1,a_2,…,a_n$. All $a_i$ are pairwise distinct.
Let's define function $f(l,r)$ as follows:
- let's define array $b_1,b_2,…,b_{r-l+1}$, where $b_i=a_{l-1+i}$;
- sort array $b$ in increasing order;
- result of the function $f(l,r)$ is $\sum\limits_{i=1}^{r-l+1}b_i\cdot i$.
Calculate $\Bigg(\sum\limits_{1\le l\le r\le n}f(l,r)\Bigg )mod(10^9+7)$, i.e. total sum of $f$ for all subsegments of $a$ modulo $10^9+7$.
可以得到$a_x$的贡献为
$\sum\limits_{\substack{a_i<a_x\\ i<x}} i\cdot (n-x+1)+\sum\limits_{\substack{a_i<a_x\\ i>x}}x\cdot (n-i+1)+x\cdot (n-x+1)$
#include <iostream>
#include <cstdio>
#include <algorithm>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
using namespace std;
typedef long long ll; const int N = 1e6+10, P = 1e9+7;
int n, a[N], b[N];
ll c[N], cnt[N];
void add(int x, int v) {
for (; x<=n; x+=x&-x) c[x]+=v;
}
ll query(int x) {
ll r = 0;
for (; x; x^=x&-x) r+=c[x];
return r%P;
} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d",a+i),b[i]=a[i];
sort(b+1,b+1+n);
REP(i,1,n) a[i]=lower_bound(b+1,b+1+n,a[i])-b;
REP(i,1,n) {
cnt[i] += query(a[i])*(n-i+1)%P;
add(a[i], i);
}
REP(i,1,n) cnt[i] += (ll)i*(n-i+1)%P, c[i] = 0;
PER(i,1,n) {
cnt[i] += query(a[i])*i%P;
add(a[i], (n-i+1));
}
ll ans = 0;
REP(i,1,n) ans+=cnt[i]*b[a[i]]%P;
printf("%lld\n", ans%P);
}
Scalar Queries CodeForces - 1167F (计数,树状数组)的更多相关文章
- Sereja and Brackets CodeForces - 380C (树状数组+离线)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- codeforces 597C (树状数组+DP)
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- HDU 6348 序列计数 (树状数组 + DP)
序列计数 Time Limit: 4500/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- gym 100589A queries on the Tree 树状数组 + 分块
题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...
- CodeForces 371D Vessels(树状数组)
树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include& ...
- Mishka and Interesting sum Codeforces Round #365 (树状数组)
树状数组,与Turing Tree类似. xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l] 其中xr[r] ...
- Codeforces 1096F(dp + 树状数组)
题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...
- DNA Evolution CodeForces - 828E(树状数组)
题中有两种操作,第一种把某个位置的字母修改,第二种操作查询与[L, R]内与给出字符串循环起来以后对应位置的字母相同的个数.给出的字符串最大长度是10. 用一个四维树状数组表示 cnt[ATCG的编号 ...
随机推荐
- Linux-常用shell简介及shell基本操作
1.查询shell环境变量,切换shell种类 表明目前使用的shell种类是bash. 要想改变shell种类,在终端输入想要运行的shell名称即可.在切换shell种类的过程中,可能会操 ...
- Linux编程之recvmsg和sendmsg函数
recvmsg 和 sendmsg 函数 #include <sys/types.h> #include <sys/socket.h> ssize_t send(int soc ...
- HTTP之缓存技术
1. 缓存简介 缓存是位于服务器和客户端的中间单元,主要根据用户代理发送过来的请求,向服务器请求相关内容后提供给用户,并保存内容副本,例如 HTML 页面.图片.文本文件或者流媒体文件.然后,当下一个 ...
- 外网访问内网的FTP服务器
转自 外网访问内网的FTP服务器 首先感谢作者给出的总结,原文是介绍Serv-U的,我针对FileZilla Server进行了稍微修改,仅看操作可直接跳到分割线后第3部分. 1. 背景简介最近研究如 ...
- ElasticSearch3:RestAPI
1.设置分片数和副本数 es7默认主分片数和主分片副本数都为1,通过 default_template 指定分片数 PUT http://192.168.8.101:9200/_template/de ...
- box-sizing Bootstrap
https://getbootstrap.com/docs/4.0/getting-started/introduction/#box-sizing Box-sizing For more strai ...
- Mac下持续集成-自动发送邮件
找到下面这项填写邮件地址 注意下面绿色标记的邮箱要和上面的一致,否则会报错 如果两个绿色标记的邮箱不一致会报这样的错: ---------------------------------------- ...
- shell 脚本基础与条件判断
#!shell脚本格式决定专业性 #!/bin/bash #filename:脚本名 #author:作者 #date:时间 #脚本作用 脚本的执行方式 #脚本名为wk.sh 绝对路径 /root/ ...
- redis的服务器信息状态信息查看
Redis的服务器信息状态信息查看 Redis的提供了一个信息命令查看Redis的服务器的信息,类似的Linux提供一个顶级命令查看系统的信息 redis-cli info # Server #服务器 ...
- OGG ERRORS 总结
OGG ERRORS 总结 */--> OGG ERRORS 总结 Table of Contents 1. libnnz11.so 2. 00446 2.1. missing filename ...