题目链接: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. 阿里技术面全A,终面却被产品经理拉下马。。。

    大纲: 一.投递简历 二.准备面试 三.技术一面 四.健身房里的技术二面 五.产品经理的死亡三面 六.总结 一.投递简历 找内推.大公司投简历尽量找内推,无论是校招还是社招.校招可以去牛客网或知乎找, ...

  2. 【Python】Django 的邮件引擎用法详解!!(调用163邮箱为例)

    1. send_mall()方法介绍 位置: 在django.core.mail模块提供了send_mail()来发送邮件. 方法参数: send_mail(subject, message, fro ...

  3. Python基础总结之初步认识---clsaa类(上)。第十四天开始(新手可相互督促)

    最近的类看着很疼,坚持就是胜利~~~ python中的类,什么是类?类是由属性和方法组成的.类中可能有很多属性,以及方法. 我们这样定义一个类: 前面是class关键字 后面school是一个类的名字 ...

  4. Unity实战案例教程之:不免费的PacMan(初级→中级)

    课程内容介绍: 本套课程适合以下人士: - 免费资料没教会你游戏开发的: - 学了Unity基础不知道怎么用在游戏项目里的: - 想快速开发一款好玩的游戏的: - 想学游戏不知道如何入门的: - 对游 ...

  5. 一份新的lilypond谱子,能设置页边距和设置换页符了

    给学生做的一份乐谱,这回能设置页边距了,以及设置换页符了. 顺带能设置一些代码片段(snippet),可以用热键代替使用 设置页边距的snippet: \paper { %双引号里面填页面大小 #(s ...

  6. 编码规范 | Java函数优雅之道(上)

    导读 随着软件项目代码的日积月累,系统维护成本变得越来越高,是所有软件团队面临的共同问题.持续地优化代码,提高代码的质量,是提升系统生命力的有效手段之一.软件系统思维有句话“Less coding, ...

  7. zookeeper中的分布式一致性协议

    1. zookeeper中的一致性协议-ZAB协议 在深入了解ZK之前,相信很多同学都会认为ZK就是Paxos算法的一个实现.但事实上,ZK并没有完全采用Paxos算法,而是使用了一种称为ZooKee ...

  8. 简单认识Nginx---负载均衡

    中大型项目都会考虑到分布式,前面几篇文章着重介绍了数据处理的技术集群.今天来研究一下关于服务器的负载均衡–Nginx.他除了静态资源的处理外还有可以决定将请求置于那台服务上. Nginx的安装 点我下 ...

  9. android 编译突然出错,错误原因 Could not resolve com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+.

    错误追根是因为微信支付依赖的错误 解决办法: 微信支付依赖版本+号改为微信支付依赖最新版本 在这里https://bintray.com/wechat-sdk-team/maven可以查看到wecha ...

  10. 浅谈IDEA集成SSM框架(SpringMVC+Spring+MyBatis)

    前言 学习完MyBatis,Spring,SpringMVC之后,我们需要做的就是将这三者联系起来,Spring实现业务对象管理,Spring MVC负责请求的转发和视图管理, MyBatis作为数据 ...