P is a permutation of the integers from 1 to N(index starting from 1). 
Here is the code of Bubble Sort in C++.

for(int i=1;i<=N;++i)
for(int j=N,t;j>i;—j)
if(P[j-1] > P[j])
t=P[j],P[j]=P[j-1],P[j-1]=t;

After the sort, the array is in increasing order. ?? wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.

InputThe first line of the input gives the number of test cases T; T test cases follow. 
Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, inclusive.

limits 
T <= 20 
1 <= N <= 100000 
N is larger than 10000 in only one case. 
OutputFor each test case output “Case #x: y1 y2 … yN” (without quotes), where x is the test case number (starting from 1), and yi is the difference of rightmost place and leftmost place of number i.Sample Input

2
3
3 1 2
3
1 2 3

Sample Output

Case #1: 1 1 2
Case #2: 0 0 0

Hint

In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3)
the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3
In second case, the array has already in increasing order. So the answer of every number is 0. 思路:找一下左右的端点 左边取当前位置和最后的位置中最小的 右端点右侧等于比他小的数+当前位置
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std; struct node
{
int l,r;
int num;
}tree[maxn<<]; struct node1
{
int pos,val;
bool friend operator<(node1 x,node1 y)
{
return x.val<y.val;
}
}p[maxn];
void pushup(int m)
{
tree[m].num=(tree[m<<].num+tree[m<<|].num);
}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
if(l==r)
{
tree[m].num=;
return ;
}
int mid=(l+r)>>;
build(m<<,l,mid);
build(m<<|,mid+,r);
pushup(m);
}
void update(int m,int pos,int val)
{
if(tree[m].l==tree[m].r)
{
tree[m].num=val;
return;
}
int mid=(tree[m].l+tree[m].r)>>;
if(pos<=mid)
{
update(m<<,pos,val);
}
else
{
update(m<<|,pos,val);
}
pushup(m);
}
int query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
{
return tree[m].num;
}
int mid=(tree[m].l+tree[m].r)>>;
if(r<=mid)
{
return query(m<<,l,r);
}
else if(l>mid)
{
return query(m<<|,l,r);
}
else
{
return query(m<<,l,mid)+query(m<<|,mid+,r);
} }
int ans[maxn];
int main()
{
int T;
cin>>T;
int cnt=;
while(T--)
{
int n;
scanf("%d",&n);
build(,,n);
for(int t=;t<=n;t++)
{
scanf("%d",&p[t].val);
p[t].pos=t;
update(,p[t].val,);
}
//sort(p+1,p+n+1);
for(int t=;t<=n;t++)
{
ans[p[t].val]=abs(min(p[t].val,p[t].pos)-(t+query(,,p[t].val-)));
update(,p[t].val,);
}
printf("Case #%d: ",cnt++);
for(int t=;t<=n;t++)
{
if(t==n)
{
printf("%d\n",ans[t]);
}
else
{
printf("%d ",ans[t]);
}
} }
return ;
}

HDU - 5775-Bubble Sort(权值线段树)的更多相关文章

  1. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  2. HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

  4. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

  5. HDU 6464 权值线段树 && HDU 6468 思维题

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. 区间第k大问题 权值线段树 hdu 5249

    先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...

  7. HDU 5249:KPI(权值线段树)

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  8. HDU 6464 /// 权值线段树

    题目大意: 共Q次操作 操作有两种 操作一 在序列尾部加入f[i]个s[i] 操作二 查询序列第f[i]小到第s[i]小之间的总和 离线操作 把序列内的值离散化 然后利用离散化后的值 在线段树上对应权 ...

  9. HDU 6609 离散化+权值线段树

    题意 有一个长度为\(n\)的数组W; 对于每一个\(i\)(\(1<=i<=n\)),你可以选择中任意一些元素W[k] (\(1<=k<i\)),将他们的值改变为0,使得\( ...

  10. HDU - 5592 ZYB's Premutation (权值线段树)

    题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...

随机推荐

  1. 实验04——java保留小数的两种方法、字符串转数值

    package cn.tedu.demo; import java.text.DecimalFormat; /** * @author 赵瑞鑫 E-mail:1922250303@qq.com * @ ...

  2. 用Spark进行实时流计算

    Spark Streaming VS Structured Streaming Spark Streaming是Spark最初的流处理框架,使用了微批的形式来进行流处理. 提供了基于RDDs的Dstr ...

  3. Dropwizard+jersey+MDC实现日志跟踪以及接口响应时间统计

    一.实现接口响应时间统计 1.1添加全局请求过滤器并注册 import org.apache.log4j.MDC; import org.slf4j.Logger; import org.slf4j. ...

  4. Chrome-AdGuard 无与伦比的广告拦截扩展

    一款无与伦比的广告拦截扩展,对抗各式广告与弹窗. AdGuard 广告拦截器可有效的拦截所有网页上的所有类型的广告,甚至是在 Facebook.Youtube 以及其他万千网站上的广告! AdGuar ...

  5. 补充的javascript 数据类型笔记

        <p> 变量名命名规范</p>     由字母,数字,下划线,$组成     严格区分大小写 var app和var App 是两个变量     变量名不能以数字开头 ...

  6. Hello GCN

    参考链接: https://www.zhihu.com/question/54504471/answer/611222866 1 拉普拉斯矩阵 参考链接: http://bbs.cvmart.net/ ...

  7. Weblogic 连接 RMI 服务报错 Connection refused

    WebLogic 连接 RMI 服务报错 Connection refused 访问 WebLogic RMI 服务报错,连接被拒绝,连接超时. 奇怪的是,报错的 host 根本不是我要访问的. 报错 ...

  8. 2020重新出发,JAVA语言,JAVA的诞生和发展史

    java的诞生 在1991年时候,James Gosling在Sun公司的工程师小组想要设计这样一种主要用于像电视盒这样的消费类电子产品的小型计算机语言. 这些电子产品有一个共同的特点:计算处理能力和 ...

  9. MySQL查看没有主键的表

    select table_schema, table_name from information_schema.tables where table_name not in (select disti ...

  10. 【Spring】看了这篇Spring事务原理,我才知道我对Spring事务的误解有多深!

    写在前面 有很多小伙伴们留言说,冰河,你能不能写一篇关于Spring事务的文章呢?我:可以啊,安排上了!那还等什么呢?走起啊!! 事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有 ...