题目链接: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. Ubuntu下python安装mysqldb(驱动)

    最近在学习Django框架,需要使用到数据库,我使用的是mysql,跟java一样,需要安装驱动,这是驱动的下载网址http://sourceforge.net/projects/mysql-pyth ...

  2. 自定义 EditText 样式

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  3. Codeforces Round #575 (Div. 3)

    本蒟蒻已经掉到灰名了(菜到落泪),希望这次打完能重回绿名吧...... 这次赛中A了三题 下面是本蒟蒻的题解 A.Three Piles of Candies 这题没啥好说的,相加除2就完事了 #in ...

  4. 算法与数据结构基础 - 合并查找(Union Find)

    Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...

  5. linux安装启动mongodb

    1:下载 http://www.mongodb.org/downloads 在85机器上上传压缩包后解压缩. 首先在linux中解压缩安装程序 通过命令操作: 解压 tar -zxvf mongodb ...

  6. docker 容器之间互联

    容器之间的互联 一. 实验目的: 1.       熟悉容器之间基本的网络原理: 2.       掌握容器之间互联的方法: 二. 实验环境: Ubuntu16.04+Docker 三. 实验内容: ...

  7. Docker笔记(八):数据管理

    前面(哪个前面我也忘了)有说过,如果我们需要对数据进行持久化保存,不应使其存储在容器中,因为容器中的数据会随着容器的删除而丢失,而因通过将数据存储于宿主机文件系统的形式来持久化.在Docker容器中管 ...

  8. 【POJ - 3259】Wormholes(最短路 Floyd算法)

    Wormholes 题目描述 教学楼里有很多教室,这些教室由双向走廊连接.另外,还存在一些单向的秘密通道,通过它们可以回到过去.现在有 N (1 ≤ N ≤ 500) 个教室,编号 1..N, M ( ...

  9. SQL获取客户端网卡电脑名称等信息

    Select SYSTEM_USER 当前用户名, USER_NAME() 当前所有者,db_Name() 当前数据库,@@SPID 当前进程号,(select top 1 FileName from ...

  10. Stream和方法引用

    1.Stream流 1.for循环带来的弊端 在jdk8中,lambda专注于做什么,而不是怎么做 for循环的语法就是怎么做 for循环的循环体才是做什么 遍历是指每一个元素逐一进行处理,而并不是从 ...