codeforces 830 B. Cards Sorting(线段树)
题目链接: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(线段树)的更多相关文章
- codeforces 830 B Cards Sorting
B. Cards Sorting http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- 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 ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
随机推荐
- JDK的命令行工具系列 (二) javap、jinfo、jmap
javap: 反编译工具, 可用来查看java编译器生成的字节码 参数摘要: -help 帮助 -l 输出行和变量的表 -public 只输出public方法和域 -protected 只输出publ ...
- go interface衍生的插件化处理
在设计程序的许多应用场景中我们会遇到大体分为三个阶段的任务流. 第一.入口 一个或多个入口,等待阻塞的.或者主动请求方式的. ============================== 比如任务流需 ...
- Selenium模拟登陆百度贴吧
Selenium模拟登陆百度贴吧 from selenium import webdriver from time import sleep from selenium.webdriver.commo ...
- Docker系列之烹饪披萨(二)
前言 上一篇我们讲解了虚拟机和容器的区别,本节我们来讲讲Docker中关于Dockerfile.镜像.容器等基本概念.Docker是一个在容器内开发.部署.运行应用程序的平台,Docker本质上是容器 ...
- Scala集合(四)
1. 集合 集合主要有三种: Sequence Map Set sequence是一种线性元素的集合,可能会是索引或者线性的(链表).map是包含键值对的集合,就像Java的Map,set是包含无重复 ...
- HTML5 Device Access (设备访问)
camera api (含图片预览) 参考地址 主要为利用input type=file, accept="image/*" 进行处理 图片预览方式(两种) const file ...
- android ——Tablayout
Tabs make it easy to explore and switch between different views. 通过TabLayout可以在一个活动中通过滑动或者点击切换到不同的页面 ...
- 减谈迷宫C++
今天老师让做了个迷宫问题,我一看到就发现和我之前写过的一个程序是一样 的,但是在后来编写的时候有一个地方搞错了,最后下课了我还是没有正确的编写好,然后今天回来之后自己有看了一下,现在已经解决了. #i ...
- c语言实现基本的数据结构(一) 线性表
#include <stdio.h> #include <tchar.h> #include <stdlib.h> #define LIST_INIT_SIZE 1 ...
- MongoDB Day 1
创建数据库 db.createCollection("user"); 插入字段 //----insert------- db.user.insert({uid:1, user_co ...