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 ...
随机推荐
- 常用jQuery选择器总结【转】
在Dom 编程中我们只能使用有限的函数根据id 或者TagName 获取Dom 对象. 然而在jQuery 中则完全不同,jQuery 提供了异常强大的选择器用来帮助我们获取页面上的对象, 并且将对象 ...
- 安装search everything中文语言包
Everything 作为很多人的必备工具,特写这篇文章,一方面让想使用国外优秀软件的英语小白有一段过渡期,另一方面也为自己整理下资料.当然,这个可不是不学好英语的正当理由. 步骤: 1. 下载好se ...
- tableview 编辑状态设置
#pragma mark - tableview 编辑状态设置 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSI ...
- Gunicorn快速入门
Gunicorn (独角兽)是一个高效的Python WSGI Server,通常用它来运行 wsgi application(由我们自己编写遵循WSGI application的编写规范) 或者 w ...
- 【Linux】 诊断工具-strace
1,别人家的总结: http://www.cnblogs.com/bangerlee/archive/2012/02/20/2356818.html 2,我自己碰到问题: 暂无. 3,使用场景: 程序 ...
- Selenium 自动化验收测试
Web 应用程序的验收测试常常涉及一些手工任务,例如打开一个浏览器,并执行一个测试用例中所描述的操作.但是手工执行的任务容易出现操作人员人为的错误,也比较费时间.因此,尽可能将这些任务自动化,以消除人 ...
- Strust2的json插件
以下这段摘自网上: Json是一种轻量级的数据交换格式,JSon插件提供了一种名为json的ActionResultType .一旦为Action指定了该结果处理类型,JSON插件就会自动将Actio ...
- 【转】Android开发中adb启动失败adb连接异常的解决办法 offline
原文网址:http://www.cnblogs.com/yejiurui/p/4173521.html 一.情况描述: 我们在使用eclipse开发有时候会出现adb连接异常中,有时候控制台会打印出来 ...
- slf4j简介
SLF4J不是日志框架而是一个简单日志门面,它的目的是允许最终用户在部署时使用期望的日志框架. The Simple Logging Facade for Java (SLF4J) serves as ...
- Linked List Cycle——LeetCode
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...