/*
先来说一下第一眼看到想出的奇葩方法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. sys.argv[]用法

    #-*- coding: utf-8 -*- """ sys.argv 用来获取命令行参数 sys.argv[0] 表示当前执行文件 "-k".sta ...

  2. [r]Setting up Django and your web server with uWSGI and nginx

    Setting up Django and your web server with uWSGI and nginx This tutorial is aimed at the Django user ...

  3. 解决Maven中Missing artifact javax.jms:jms:jar:1.1:compile

    搭建好项目后报错: Missing artifact javax.jms:jms:jar:1.1:compile  于POM.xml中 解决方案: 一 :在nexus中配置一个代理仓库     地址为 ...

  4. Unity的Profiler性能分析

    1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时间总 ...

  5. MiniProfiler.3.0.10 用于MVC4.0中不能显示SQL语句

    MiniProfiler.3.0.10 用于MVC4.0中可以显示执行时间,但是不能显示SQL语句,怎么解决?

  6. Java中swap解惑

    直接上代码…… public class Swap { public static void main(String[] args) { int a[] = new int[]{1,2}; Syste ...

  7. The Child and Sequence

    Codeforces Round #250 (Div. 1)D:http://codeforces.com/problemset/problem/438/D 题意:给你一个序列,然后有3种操作 1x ...

  8. LA 3521 Joseph's Problem

    题意:给你正整数n和k,然后计算从i到n k%i的和: 思路:如果n小于1000000,直接暴力计算,然后大于1000000的情况,然后在讨论n和k的大小,根据k%i的情况,你会发现规律,是多个等差数 ...

  9. Delphi判断文件是否正在被使用(CreateFile也可以只是为了读取数据,而不是创建)

    首先,我们先来认识下CreateFile函数,它的原型如下   HANDLE CreateFile( LPCTSTR lpFileName,    //指向文件名的指针 DWORD dwDesired ...

  10. Json传递后台数据的问题

    在后台我有两个类: public Class Person { private String name; private Address address;//一个自定义的类 //getter和sett ...