COGS 859. 数列
/*
先来说一下第一眼看到想出的奇葩方法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. 数列的更多相关文章
- COGS 2638. 数列操作ψ 线段树
传送门 : COGS 2638. 数列操作ψ 线段树 这道题让我们维护区间最大值,以及维护区间and,or一个数 我们考虑用线段树进行维护,这时候我们就要用到吉司机线段树啦 QAQ 由于发现若干次an ...
- cogs 264. 数列操作 单点修改 区间查询
http://cogs.pro:8080/cogs/problem/problem.php?pid=pyNimmVeq 264. 数列操作 ★☆ 输入文件:shulie.in 输出文件:shu ...
- cogs 1317. 数列操作C 区间修改 区间查询
1317. 数列操作C ★★★ 输入文件:shuliec.in 输出文件:shuliec.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 假设有一个长度为 n( ...
- cogs 1316. 数列操作B 区间修改 单点查询
1316. 数列操作B ★★ 输入文件:shulieb.in 输出文件:shulieb.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 假设有一个大小为 n(n ...
- COGS.1317.数列操作c(分块 区间加 区间求和)
题目链接 #include<cmath> #include<cstdio> #include<cctype> #include<algorithm> u ...
- COGS.264.数列操作(分块 单点加 区间求和)
题目链接 #include<cmath> #include<cstdio> #include<cctype> #include<algorithm> u ...
- COGS 264. 数列操作
时间限制:1 s 内存限制:160 MB [问题描述] 假设有一列数 {Ai }(1 ≤ i ≤ n) ,支持如下两种操作: (1)将 A k 的值加 D .( k, D 是输入的数) (2) 输 ...
- 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列
339. [NOI2005] 维护数列 时间限制:3 s 内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际 ...
- cogs 66. [HAOI2004模拟] 数列问题
66. [HAOI2004模拟] 数列问题 ★☆ 输入文件:dfs3.in 输出文件:dfs3.out 简单对比时间限制:1 s 内存限制:128 MB 问题描述试编程将 1 至 N ...
随机推荐
- nginx+uwsgi+WSGI applications
uwsgi一个专业的部署运用的工具,不仅能够部署Python运用,还能够部署其他运用比如Perl,Ruby等 uWSGI 安装: pip install uwsgi WSGI application( ...
- 转:使用xhprof进行线上PHP性能追踪及分析
原文来自于:http://avnpc.com/pages/profiler-php-performance-online-by-xhprof 原创作者:AlloVince 之前一直使用基于Xdebug ...
- ORACLE和MONGODB,必须也得进入。
先在WIN下面操作一下. 参考的是<MONGODB权威指南>
- 24篇Delphi文件操作文章
http://www.cnblogs.com/keyvip/category/268043.html
- Unity NGUI根据高度自适应屏幕分辨率
Unity版本:4.5.1 NGUI版本:3.6.5 本文内容纯粹转载,转载保留参考链接和作者 参考链接:http://blog.csdn.net/asd237241291/article/detai ...
- javascript 基础学习整理
1. javascript是动态语言,脚本语言,弱类型语言. 2. javascript代码在html文件中的位置安排,放在<body></body>内部与外部的区别.如何引用 ...
- (转载)PHP去掉转义后字符串中的反斜杠\函数stripslashes
(转载)http://www.beijibear.com/index.php?aid=182 addslashes()函数主要是在字符串中添加反斜杠对特殊字符进行转义,stripslashes()则是 ...
- Redis源码阅读笔记(1)——简单动态字符串sds实现原理
首先,sds即simple dynamic string,redis实现这个的时候使用了一个技巧,并且C99将其收录为标准,即柔性数组成员(flexible array member),参考资料见这里 ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- nginx在mac下的安装与基本操作
1. 安装 brew install nginx(需要安装homebrew) 2. 执行 nginx 直接启动nginx服务 3. nginx -s reload/stop 4. 配置地址 sud ...