/*
先来说一下第一眼看到想出的奇葩方法23333..
找每个数左右有几个比他小的
前几天刚学了区间第k小的求法
然后...
枚举中间的那个点 对于左区间 二分找到他是第几大 右区间同理
然后 *起来
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 50010
#define maxm 50010*18*5
#define ll long long
using namespace std;
ll n,cnt,root[maxn],order[maxn],a[maxn],ans;
struct node{
ll lc,rc,sum;
}t[maxm];
ll init(){
ll x=;char s=getchar();
while(s<''||s>'')s=getchar();
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
return x;
}
ll Build(ll S,ll L,ll R){
ll k=++cnt;t[k].sum=S;
t[k].lc=L;t[k].rc=R;
return k;
}
void Insert(ll &root,ll pre,ll pos,ll l,ll r){
root=Build(t[pre].sum+,t[pre].lc,t[pre].rc);
if(l==r)return;
ll mid=l+r>>;
if(pos<=mid)Insert(t[root].lc,t[pre].lc,pos,l,mid);
else Insert(t[root].rc,t[pre].rc,pos,mid+,r);
}
ll Query(ll L,ll R,ll k,ll l,ll r){
if(l==r)return l;
ll sum=t[t[R].lc].sum-t[t[L].lc].sum;
ll mid=l+r>>;
if(k<=sum)return Query(t[L].lc,t[R].lc,k,l,mid);
else return Query(t[L].rc,t[R].rc,k-sum,mid+,r);
}
int main()
{
//freopen("queueb.in","r",stdin);
//freopen("queueb.out","w",stdout);
n=init();
for(ll i=;i<=n;i++){
a[i]=init();
order[i]=a[i];
}
sort(order+,order++n);
ll num=n,pos,l,r,x,s1,s2;
for(ll i=;i<=n;i++){
pos=lower_bound(order+,order++n,a[i])-order;
Insert(root[i],root[i-],pos,,num);
}
for(ll i=;i<=n;i++){
x=a[i];s1=s2=;
l=;r=i;
while(l<=r){
ll mid=l+r>>;
pos=Query(root[],root[i],mid,,num);
if(order[pos]<x){
s1=mid;l=mid+;
}
else r=mid-;
}
l=;r=n-i+;
while(l<=r){
ll mid=l+r>>;
pos=Query(root[i-],root[n],mid,,num);
if(order[pos]<x){
s2=mid;l=mid+;
}
else r=mid-;
}
ans+=s1*s2;
}
printf("%lld\n",ans);
return ;
}
/*
其实求左右各有几个比他小的 可以用树状数组
逆序对是求左边几个比他大的这个一样啊
注意右边的时候离散化排序和左边不一样
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 50010
#define ll long long
using namespace std;
int n,b[maxn],t[maxn],l[maxn],r[maxn];
ll ans;
struct node{
int o,x;
}a[maxn];
int init(){
int x=;char s=getchar();
while(s<''||s>'')s=getchar();
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
return x;
}
int cmp1(const node &a,const node &b){
if(a.x==b.x)return a.o>b.o;
return a.x<b.x;
}
int cmp2(const node &a,const node &b){
if(a.x==b.x)return a.o<b.o;
return a.x<b.x;
}
void Add(int pos,int data){
while(pos<=n){
t[pos]+=data;
pos+=pos&(-pos);
}
}
int find(int pos){
int r=;
while(pos){
r+=t[pos];
pos-=pos&(-pos);
}
return r;
}
int main()
{
freopen("queueb.in","r",stdin);
freopen("queueb.out","w",stdout);
n=init();
for(int i=;i<=n;i++){
a[i].x=init();
a[i].o=i;
}
sort(a+,a++n,cmp1);
for(int i=;i<=n;i++)
b[a[i].o]=i;
for(int i=;i<=n;i++){
Add(b[i],);
l[i]=find(b[i]-);
}
memset(t,,sizeof(t));
sort(a+,a++n,cmp2);
for(int i=;i<=n;i++)
b[a[i].o]=i;
for(int i=n;i>=;i--){
Add(b[i],);
r[i]=find(b[i]-);
}
for(int i=;i<=n;i++)
ans+=(ll)l[i]*r[i];
printf("%lld\n",ans);
return ;
}

COGS 859. 数列的更多相关文章

  1. COGS 2638. 数列操作ψ 线段树

    传送门 : COGS 2638. 数列操作ψ 线段树 这道题让我们维护区间最大值,以及维护区间and,or一个数 我们考虑用线段树进行维护,这时候我们就要用到吉司机线段树啦 QAQ 由于发现若干次an ...

  2. cogs 264. 数列操作 单点修改 区间查询

    http://cogs.pro:8080/cogs/problem/problem.php?pid=pyNimmVeq 264. 数列操作 ★☆   输入文件:shulie.in   输出文件:shu ...

  3. cogs 1317. 数列操作C 区间修改 区间查询

    1317. 数列操作C ★★★   输入文件:shuliec.in   输出文件:shuliec.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 假设有一个长度为 n( ...

  4. cogs 1316. 数列操作B 区间修改 单点查询

    1316. 数列操作B ★★   输入文件:shulieb.in   输出文件:shulieb.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 假设有一个大小为 n(n ...

  5. COGS.1317.数列操作c(分块 区间加 区间求和)

    题目链接 #include<cmath> #include<cstdio> #include<cctype> #include<algorithm> u ...

  6. COGS.264.数列操作(分块 单点加 区间求和)

    题目链接 #include<cmath> #include<cstdio> #include<cctype> #include<algorithm> u ...

  7. COGS 264. 数列操作

    时间限制:1 s   内存限制:160 MB [问题描述] 假设有一列数 {Ai }(1 ≤ i ≤ n) ,支持如下两种操作: (1)将 A k 的值加 D .( k, D 是输入的数) (2) 输 ...

  8. 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列

    339. [NOI2005] 维护数列 时间限制:3 s   内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...

  9. cogs 66. [HAOI2004模拟] 数列问题

    66. [HAOI2004模拟] 数列问题 ★☆   输入文件:dfs3.in   输出文件:dfs3.out   简单对比时间限制:1 s   内存限制:128 MB 问题描述试编程将 1 至 N ...

随机推荐

  1. windows下实现uboot的tftp下载功能

    一.原理分析 带有uboot的开发板实际上充当的就是tftp客户端,而PC机扮演的角色就是tftp服务器端,而tftp下载功能实际上就是文件传输.tftp服务器可以建立在虚拟机linux下,也可以建立 ...

  2. windows 下安装nodejs及其配置环境

    第一步:下载安装文件下载nodejs,官网:http://nodejs.org/download/,我这里下载的是node-v0.10.28-x86.msi,如下图: 第二步:安装nodejs下载完成 ...

  3. BZOJ 3893 Cow Jog

    Description The cows are out exercising their hooves again! There are \(N\) cows jogging on an infin ...

  4. BZOJ 1021 循环的债务

    Description Alice.Bob和Cynthia总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在清还债务的 ...

  5. NYOJ 540

    为了给学弟学妹讲课,我水了一道题…… import java.util.Arrays; import java.util.Scanner; public class NYOJ540 { public ...

  6. Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?

    Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...

  7. org.springframework.web.context.ContextLoaderListener

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...

  8. Static Final用法

    一.final数据 在 java编程语言中,有时候需要告知编译器一段数据是不变的编译期常量.对于这种情况,编译器可以将此常量值带入需要用到它的计算式子当中,这种在编译时 执行计算式的方法减轻了运行时的 ...

  9. Gold Balanced Lineup(哈希表)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10711   Accepted: 3182 Description Farm ...

  10. 1B. Spreadsheets

    题目大意: 行和列的两种方式. A是1, B是2,....Z是26, AA是27, AB是28........... 如: BC23代表55列23行 还有一种表示方法:R23C55, 代表23行,55 ...