cf 61 E. Enemy is weak 离散化+树状数组
题意:
给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ax is the value at position x. 的个数
明显数组的值太大了
先离散化,然后就是简单的树状数组了
对于每一个i,只要统计i前面的数中比a[i]大的数的个数,和i后面的数中比a[i]小的数的个数即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <math.h> #define LL long long using namespace std; const int maxn = + ; struct Node
{
int id,init,chg;
};
Node node[maxn];
int fir[maxn];
int sec[maxn];
int c[maxn]; bool cmp1(Node x,Node y)
{
return x.init<y.init;
} bool cmp2(Node x,Node y)
{
return x.id<y.id;
} inline int lb(int x)
{
return x & (-x);
} void update(int x,int add)
{
while(x <= maxn){
c[x] += add;
x += lb(x);
}
} int query(int x)
{
int ret = ;
while(x > ){
ret += c[x];
x -= lb(x);
}
return ret;
} LL solve(int n)
{
sort(node+,node+n+,cmp1);
for(int i=;i<=n;i++)
node[i].chg = i;
sort(node+,node+n+,cmp2);
memset(c,,sizeof c);
for(int i=;i<=n;i++){
fir[i] = i - - query(node[i].chg - );
update(node[i].chg,);
}
memset(c,,sizeof c);
for(int i=n;i>;i--){
sec[i] = query(node[i].chg - );
update(node[i].chg,);
}
LL ret = ;
for(int i=;i<=n;i++)
ret += (LL) fir[i] * sec[i];
return ret;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&node[i].init);
node[i].id = i;
}
//printf("%I64d\n",solve(n));
cout<<solve(n)<<endl;
return ;
}
cf 61 E. Enemy is weak 离散化+树状数组的更多相关文章
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
- poj-----Ultra-QuickSort(离散化+树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 38258 Accepted: 13784 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组
题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...
- 【bzoj5055】膜法师 离散化+树状数组
题目描述 给定一个序列$a$,求满足$i<j<k$且$a_i<a_j<a_k$的三元组$(i,j,k)$的个数. 输入 第一行1个数 n 第二行n个数 a_i 输出 一个数,表 ...
随机推荐
- Java中将16进制字符串转换成汉字
技术交流群:233513714 /** * 将16进制字符串转换成汉字 * @param str * @return */ public static String deUnicode(String ...
- cve-2015-1635 poc
import socket import random ipAddr = "10.1.89.20" hexAllFfff = " req1 = "GET / H ...
- jQuery UI 对话框(Dialog) - 模态表单
<!doctype html><html lang="en"><head> <meta charset="utf-8" ...
- 在VS 2015 RTM 版中 提示 未能正确加载 NuGetPackage包
在原来的项目中曾经启用了Nuget在编译时还原包功能.这样就会在 *.sln在平行目录生成 一个.Nuget文件夹, 删除了它,就好了. 我分析原因是, VS 2015 使用的是 NugetP ...
- 生成n个数的全排列【递归、回溯】
下面讨论的是n个互不相同的数形成的不同排列的个数.毕竟,假如n个数当中有相同的数,那n!种排列当中肯定会有一些排列是重复的,这样就是一个不一样的问题了. /*===================== ...
- ios 企业发布
ios 1. 支持的tls 协议 1.2 windows server 默认没有启用 检测的网站: https://www.ssllabs.com/ssltest 解决的方法: IIS crypto ...
- spring基于注解的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Python 2.x与3.x共存
(1)检查在Path环境变量中是否有以下4个变量(没有则添加): 1.c:\Python27 2.c:\Python27\Scripts 3.c:\Python35 4.c:\Python35\Scr ...
- html初学(一)
学习了一些html的基本用法,代码和效果如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...
- redis主从配置及主从切换 转
redis主从配置及主从切换 转自 http://blog.sina.com.cn/s/blog_67196ddc0101h8v0.html (2014-04-28 17:48:47) 转载▼ 分 ...