题意

题解

对于[l,r]中的一个数,不论[l,r]有多大,只有比它小的数可以影响到它的排名,那么就可以把ai从小到大排序,一个一个加入线段树中,线段树中下表为 i 的是ai(原来的位置,不是排序后的)分别为最右端和最左端时的排名总和(设为suml[i]、sumr[i]),ai的总贡献就是 ai * (suml[i-1] * (n-i+1) + sumr[i] * i)

当循环到一个点 i 时,他左边的任意一个点 j 会为 l <= j 且 r >= i 的区间中 i 的排名贡献1,右边的任意一个点 k 会为 l <= i 且 r >= k 的区间中 i 的排名贡献1,他自己也有贡献

所以加入一个点 i 时,就要把 i~n 的 suml 都加上 i ,把 1~i 的 sumr 都加上 n-i+1。

CODE

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define MAXN 500005
#define MAXM 1000005
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x)&(x))
//#define int LL
//#pragma GCC optimize(2)
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + (s - '0');s = getchar();}
return x * f;
}
const int jzm = 1000000007;
int n,m,i,j,s,o,k;
struct it{
int nm,id;
}a[MAXN];
bool cmp(it a,it b) {return a.nm < b.nm;}
int cl[MAXN],cr[MAXN];
void addt(int *c,int x,int y) {
while(x <= n) {
c[x] = (c[x] +0ll+jzm + y) % jzm;
x += lowbit(x);
}return ;
}
int sum(int *c,int x) {
int as = 0;
while(x > 0) {
as = (as +0ll+jzm + c[x]) % jzm;
x -= lowbit(x);
}
return as;
}
int main() {
n = read();
for(int i = 1;i <= n;i ++) {
a[i].nm = read();a[i].id = i;
}
sort(a + 1,a + 1 + n,cmp);
int ans = 0;
for(int i = 1;i <= n;i ++) {
addt(cl,a[i].id,a[i].id);
addt(cr,1,n - a[i].id + 1);
addt(cr,a[i].id+1,a[i].id - n - 1);
ans = (ans +0ll+jzm +(sum(cl,a[i].id-1) *1ll* (n - a[i].id + 1) % jzm +0ll+ sum(cr,a[i].id) *1ll* a[i].id % jzm) % jzm *1ll* a[i].nm % jzm) % jzm;
// printf("ans: %d\n",ans);
}
printf("%d\n",ans);
return 0;
}

CF1167F Scalar Queries (线段树/树状数组)的更多相关文章

  1. 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 ...

  2. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  3. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

  4. Turing Tree_线段树&树状数组

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  5. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  6. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  7. HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. Super Mario 树状数组离线 || 线段树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 2019南昌网络赛  I. Yukino With Subinterval 树状数组套线段树

    I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...

随机推荐

  1. 2 万字 + 20张图| 细说 Redis 九种数据类型和应用场景

    作者:小林coding 计算机八股文网(操作系统.计算机网络.计算机组成.MySQL.Redis):https://xiaolincoding.com 大家好,我是小林. 我们都知道 Redis 提供 ...

  2. docker 操作 记录

    docker ps  #查看当前docker容器 docker exec -it  容器名称 sh  进入docker容器 docker stop 停止docker容器

  3. webpack中文api

    1.      简介 1.Plugins://插件 webpack has a rich plugin interface.Most of the features are internal plug ...

  4. bat-配置环境变量2-给PATH追加环境变量

    使用setx /M path "%path%;%%winrar%%"这种方式修改环境变量存在的问题 对于 path 这种 既有用户级变量和系统级变量的变量 直接使用setx /M ...

  5. python小题目练习(十二)

    题目:如下图所示 代码展示: """Author:mllContent:春节集五福Date:2020-01-17"""import rand ...

  6. Node.js精进(8)——错误处理

    在 Node.js 中,提供了 error 模块,并且内置了标准的 JavaScript 错误,常见的有: EvalError:在调用 eval() 函数时出现问题时抛出该错误. SyntaxErro ...

  7. 使用APICloud AVM框架开发预约应用

    前段时间跟朋友一起搞了一个预约的项目,前端用的APICloud的AVM框架做的,后端用的php开发的,用的tp5框架,没几天就搞出来了.简单跟大家分享一下开发中的一些功能点的实现吧.也欢迎大家一起探讨 ...

  8. docker安装dvwa

    docker run -d --name dvwa -p 8011:80 -p 33066:3306 -e MYSQL_PASS="password" citizenstig/dv ...

  9. 编译安装Python出现“pip is configured with locations that require TLS/SSL, however the ssl.....”

    ubuntu: sudo apt-get install libssl-dev Cenos: sudo yum install openssl-devel 重新编译: ./configure --en ...

  10. MySQL--用通配符进行过滤(LIKE操作符)

    1.LIKE操作符 怎样搜索产品名中包含文本anvil的所有产品?用简单的比较操作符肯定不行,必须使用通配符.利用通配符可创建比较特定数据的搜索模式.在这个例子中,如果你想找出名称包含anvil的所有 ...