题意

题解

对于[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. 如何写一个全局的 Notice 组件?

    下面将会实现这样的效果: 组件动态创建脚本: NotificationBanner.js import Vue from "vue"; import Notice from &qu ...

  2. 关于vue打包上线遇到的坑

    打包上线经常会经常遇到路径找不到,页面空白,那么下面我们就解决一下. 第一步.先找到config目录的index.js 改成如上图红框标注所示 第二步.找到build下的utils.js文件 加上如上 ...

  3. 手把手教你实现一个图片压缩工具(Vue与Node的完美配合)

    前言 图片压缩对于我们日常生活来讲,是非常实用的一项功能.有时我们会在在线图片压缩网站上进行压缩,有时会在电脑下软件进行压缩.那么我们能不能用前端的知识来自己实现一个图片压缩工具呢?答案是有的.效果展 ...

  4. 原生实现.NET5.0+ 自定义日志

    一.定义一个静态类 声明一个 ReaderWriterLockSlim 对象 用于并发控制 1 /// <summary> 2 /// IO锁 3 /// </summary> ...

  5. 图片放在div中低下会出现一条缝

    页面要达到的样子 中间写的是时候是向div里面放一张图片就行了 <head> <link rel="stylesheet" href="reset.cs ...

  6. 简单实现python接口自动化(一)

    目的:excel中维护接口用例数据,通过python中requests库进行读取用例,并把运行结果与excel中的预期结果对比,最后把执行情况写入到excel中去. excel维护数据: 具体的接口名 ...

  7. MYSQL中IF IN语句

    以下代码摘自后台管理系统中的一部分SQL语句: 当取数状态为1或2时,才展示取数时间,否则,取数时间展示为空 当申报状态为2.3.4或5时,才展示申报时间,否则,申报时间展示为空 select A.Q ...

  8. NC212914 牛牛与后缀表达式

    NC212914 牛牛与后缀表达式 题目 题目描述 给定牛牛一个后缀表达式 \(s\) ,计算它的结果,例如,1+1对应的后缀表达式为1#1#+,'#'作为操作数的结束符号. 其中,表达式中只含有'+ ...

  9. Pytorch分布式训练

    用单机单卡训练模型的时代已经过去,单机多卡已经成为主流配置.如何最大化发挥多卡的作用呢?本文介绍Pytorch中的DistributedDataParallel方法. 1. DataParallel ...

  10. 超小体积单键/1路1感应通道触摸触控检测IC-VKD233HS DFN6 2*2mm,常用于TWS蓝牙耳机入耳检测、运动手环等小体积单键触摸产品

    产品品牌:永嘉微电/VINKA 产品型号:VKD233HS 封装形式:DFN6 产品年份:新年份 概述: VKD233HS  DFN6具有1个触摸按键,可用来检测外部触摸按键上人手的触摸动作.该芯片具 ...