题目链接:http://codeforces.com/contest/830/problem/B

题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有几个数优先往后面找如果后面没了再轮到前面找。可以开始将每个数的下表定为1然后拿掉之后就为0然后两值之间有几个数就用区间求和来求。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 1e5 + ;
typedef long long ll;
int a[M] , b[M];
struct TGP {
int Min , pos;
TGP() {}
TGP(int Min , int pos):Min(Min), pos(pos) {}
};
struct TnT {
int l , r , sum;
TGP gp;
}T[M << ];
void push_up(int i) {
T[i].sum = T[i << ].sum + T[(i << ) | ].sum;
if(T[i << ].gp.Min <= T[(i << ) | ].gp.Min) {
T[i].gp = T[i << ].gp;
}
else T[i].gp = T[(i << ) | ].gp;
}
void build(int l , int r , int i) {
int mid = (l + r) >> ;
T[i].l = l , T[i].r = r;
if(l == r) {
T[i].gp.Min = a[l];
T[i].gp.pos = l;
T[i].sum = ;
return ;
}
build(l , mid , i << );
build(mid + , r , (i << ) | );
push_up(i);
}
void update(int pos , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == T[i].r && T[i].l == pos) {
T[i].gp.Min = inf;
T[i].sum = ;
return ;
}
if(pos > mid) update(pos , (i << ) | );
else update(pos , i << );
push_up(i);
}
TGP query(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
return T[i].gp;
}
if(mid < l) return query(l , r , (i << ) | );
else if(mid >= r) return query(l , r , i << );
else {
TGP a1 = query(l , mid , i << ) , a2 = query(mid + , r , (i << ) | );
if(a1.Min > a2.Min) return a2;
else return a1;
}
}
int getsum(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
return T[i].sum;
}
if(mid < l) return getsum(l , r , (i << ) | );
else if(mid >= r) return getsum(l , r , i << );
else return getsum(l , mid , i << ) + getsum(mid + , r , (i << ) | );
}
int main() {
int n;
scanf("%d" , &n);
for(int i = ; i <= n ; i++) scanf("%d" , &a[i]) , b[i] = a[i];
sort(b + , b + n + );
build( , n , );
ll ans = ;
int now = ;
for(int i = ; i <= n ; i++) {
TGP gg = query(now , n , );
//cout << gg.Min << ' ' << gg.pos << endl;
if(gg.Min == b[i]) {
ans += getsum(now , gg.pos , );
//cout << "ans: " << ans << endl;
now = gg.pos + ;
if(now > n) now = ;
update(gg.pos , );
}
else {
TGP gb = query( , now , );
//cout << gb.Min << ' ' << gb.pos << endl;
if(gb.Min == b[i]) {
ans += getsum(now , n , );
ans += getsum( , gb.pos , );
now = gb.pos + ;
if(now > n) now = ;
update(gb.pos , );
}
}
}
printf("%lld\n" , ans);
return ;
}

codeforces 830 B. Cards Sorting(线段树)的更多相关文章

  1. codeforces 830 B Cards Sorting

    B. Cards Sorting  http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

    Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  5. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  6. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  7. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  8. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  9. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

随机推荐

  1. 利用dockerfile 安装一个nginx-1.14.1

    FROM docker.io/centos MAINTAINER jim 107420988@qq.com ENV TZ "Asia/Shanghai" #ENV TERM xte ...

  2. js页面3秒自动跳转

    如何让当前页面3秒以后自动跳转到其他页面?JS页面自动跳转 想实现登陆后3秒自动跳转到某页的功能,在网上搜了一下,供以后使用 1.<script   language= "javasc ...

  3. 【POJ - 3258】River Hopscotch(二分)

    River Hopscotch 直接中文 Descriptions 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一块岩石跳到另一块岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点 ...

  4. export,export default,module.exports,import,require之间的区别和关联

    module.exports Node 应用由模块组成,采用 CommonJS 模块规范.根据这个规范,每个文件就是一个模块,有自己的作用域.在这些文件里面定义的变量.函数.类,都是私有的,对外不可见 ...

  5. 小伙子,你真的清楚 JVM GC ?

    序 正文 如何确定垃圾? 前面已经提到 JVM 可以采用 引用计数法 与 可达性分析算法 来确定需要回收的垃圾,我们来具体看一下这两种算法: 引用计数法 该方法实现为:给每个对象添加一个引用计数器,每 ...

  6. spring学习笔记之---bean属性注入

    bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...

  7. FTP服务端部署

    FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...

  8. 微信JSSDK签名

    微信JS-SDK说明文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 生成签名 1.签名规则 参与签名的 ...

  9. 解决VS2008,重新生成解决方案,很慢

    正所谓:“工欲善其事,必先利其器“.我也算是深受其害了,特把经验分享出来为大伙分忧! 在刚来公司的时候,使用的公司提供的VS2008作为开发工具,有一个非常让人不爽的问题,就是在重新编译代码(重新生成 ...

  10. virtualbox安装ubuntu16 LTS及其配置

    一.下载安装VirtualBox 1. 从官网下载VirtualBox,目前版本:VirtualBox 6.0.6 for Windows hosts x86/amd64 2. 下载好之后安装Virt ...