pb_ds(平板电视)整理
有人说BZOJ3040用普通的<queue>中priority_queue搞dijkstra过不了。
我只想说你们的djk可能写的太丑了。
先上代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<ext/pb_ds/priority_queue.hpp>
#define pir pair<ll,int>
using namespace std;
typedef long long ll;
//typedef std::priority_queue<pir,vector<pir>,greater<pir> > heap;
//3840 ms
typedef __gnu_pbds::priority_queue<pir,greater<pir> > heap;//默认是pairing_heap_tag
//3304 ms
const int N=1e6+,M=1e7+;
const ll INF=1e15;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,T,rxa,rxc,rya,ryc,rp,a,b;
int x,y,z;
struct node{
int v,w,next;
}e[M];
int cnt,head[N];ll dis[N];bool vis[N];
inline void add(int u,int v,int w){
e[++cnt].v=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;
}
heap q;
inline void dijkstra(){
int S;
for(int i=;i<=n;i++) dis[i]=INF;
dis[S=]=;
q.push(make_pair(dis[S],S));
while(!q.empty()){
pir t=q.top();q.pop();
int x=t.second;
if(vis[x]) continue;
vis[x]=;
for(int i=head[x];i;i=e[i].next){
int v=e[i].v;
if(!vis[v]&&dis[v]>dis[x]+e[i].w){
dis[v]=dis[x]+e[i].w;
q.push(make_pair(dis[v],v));
}
}
}
}
int main(){
n=read();m=read();
T=read();rxa=read();rxc=read();rya=read();ryc=read();rp=read();
m=m-T;
x=rxc%rp;y=ryc%rp;
a=min(x%n+,y%n+);
b=max(y%n+,y%n+);
while(T--) add(a,b,-*a);
while(m--) x=read(),y=read(),z=read(),add(x,y,z);
dijkstra();
printf("%lld",dis[n]);
return ;
}
对比:
平衡二叉树(Balanced Binary Tree)
| English | Vietnamese |
In this problem, you have to maintain a dynamic set of numbers which support the two fundamental operations
- INSERT(S,x): if x is not in S, insert x into S
- DELETE(S,x): if x is in S, delete x from S
and the two type of queries
- K-TH(S) : return the k-th smallest element of S
- COUNT(S,x): return the number of elements of S smaller than x
Input
- Line 1: Q (1 ≤ Q ≤ 200000), the number of operations
- In the next Q lines, the first token of each line is a character I, D, K or C meaning that the corresponding operation is INSERT, DELETE, K-TH or COUNT, respectively, following by a whitespace and an integer which is the parameter for that operation.
If the parameter is a value x, it is guaranteed that 0 ≤ |x| ≤ 109. If the parameter is an index k, it is guaranteed that 1 ≤ k ≤ 109.
Output
For each query, print the corresponding result in a single line. In particular, for the queries K-TH, if k is larger than the number of elements in S, print the word 'invalid'.
Example
Input
8
I -1
I -1
I 2
C 0
K 2
D -1
K 1
K 2 Output
1
2
2
invalid
#include<cstdio>
#include<ext/pb_ds/assoc_container.hpp>
//pb_ds库这次内置了红黑树(red-black tree)、伸展树(splay tree)和排序向量树(ordered-vector tree,没找到通用译名,故自行翻译)。
//这些封装好的树都支持插入(insert)、删除(erase)、求kth(find_by_order)、求rank(order_of_key)操作,O(logn)内完成
using namespace std;
using namespace __gnu_pbds;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
tree<int,null_mapped_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>bbt;
//SPOJG++版本稍旧(4.3.2),需要写成null_mapped_type才可以(高级版本可以写null_type)
char in(){
for(char ch=getchar();;ch=getchar()) if(ch>='A'&&ch<='Z') return ch;
}
int main(){
char c;int x;
for(int T=read();T--;){
c=in();x=read();
if(c=='I'){
bbt.insert(x);
}
else if(c=='D'){
bbt.erase(x);
}
else if(c=='K'){
if(x<=bbt.size())
printf("%d\n",*bbt.find_by_order(x-));
else
puts("invalid");
}
else{
printf("%d\n",bbt.order_of_key(x));
}
}
return ;
}
BZOJ3224: Tyvj 1728 普通平衡树
#include<cstdio>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("02")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
ll read(){
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
typedef __gnu_pbds::tree<ll,null_mapped_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> T;
T bbt;ll ans;
int main(){
ll n=read();
for(ll i=,o,k;i<=n;i++){
o=read();k=read();
if(o==) bbt.insert((k<<)+i);else
if(o==) bbt.erase(bbt.lower_bound(k<<));else
if(o==) printf("%d\n",bbt.order_of_key(k<<)+);else
{
if(o==) ans=*bbt.find_by_order(k-);else
if(o==) ans=*--bbt.lower_bound(k<<);else
if(o==) ans=*bbt.lower_bound((k+)<<);
printf("%lld\n",ans>>);
}
}
return ;
}
pb_ds(平板电视)整理的更多相关文章
- STL++?pb_ds平板电视初步探索
什么是pb_ds? 除了众所周知的STL库,c++还自带了ext库(应该可以这么叫吧),其中有用pb_ds命名的名称空间(俗称平板电视).这个名称空间下有四个数据类型结构.这些都是鲜为人知的.经过测试 ...
- pb_ds(平板电视)简介
据说NOI赛制可以用pbds,故整理常用方法: 1.splay 所需声明及头文件: #include <ext/pb_ds/tree_policy.hpp> #include <ex ...
- NOIP模板整理计划
先占个坑 [update]noip结束了,弃了 一.图论 1.单源最短路 洛谷P3371 (1)spfa 已加SLF优化 #include <iostream> #include < ...
- ACM算法模板整理
史诗级ACM模板整理 基本语法 字符串函数 istream& getline (char* s, streamsize n ); istream& getline (char* s, ...
- dotNET跨平台相关文档整理
一直在从事C#开发的相关技术工作,从C# 1.0一路用到现在的C# 6.0, 通常情况下被局限于Windows平台,Mono项目把我们C#程序带到了Windows之外的平台,在工作之余花了很多时间在M ...
- UWP学习目录整理
UWP学习目录整理 0x00 可以忽略的废话 10月6号靠着半听半猜和文字直播的补充看完了微软的秋季新品发布会,信仰充值成功,对UWP的开发十分感兴趣,打算后面找时间学习一下.谁想到学习的欲望越来越强 ...
- SQL Server 常用内置函数(built-in)持续整理
本文用于收集在运维中经常使用的系统内置函数,持续整理中 一,常用Metadata函数 1,查看数据库的ID和Name db_id(‘DB Name’),db_name('DB ID') 2,查看对象的 ...
- kafka学习笔记:知识点整理
一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险. ...
- JAVA程序员常用软件整理下载
********为了大家学习方便,特意整理软件下载如下:*************Java类软件:-------------------------------JDK7.0:http://pan.ba ...
随机推荐
- swift 笔记 (二十) —— 泛型
泛型 泛型是为了解决在针对不同数据类型.而做了同一种功能的操作导致的每一个类型我们都要写一份代码的问题. 有了泛型,我们能够仅仅写一份逻辑代码,而适应于不同的数据类型. func swapInt(in ...
- leetcode[50] N-Queens
题目:给定一个n,那么在n*n的棋盘里面放国际象棋的皇后,皇后之间互不在攻击范围.(皇后的攻击范围是她所在位置的哪一行,那一列,和她的正负1的对角线) The n-queens puzzle is t ...
- leetcode第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...
- JavaScript中的execCommand()命令详解及实例展示
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其 ...
- bootstrap错误警告信息提示
bootstrap提供了成功执行.警告和错误信息的样式. 在使用该功能的时候需要引入以下几个文件: bootstrap.css jquery.js(需放在bootstrap.js之前) bootstr ...
- Ecological Premium - UVa10300
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10300.html 题目描述 Pr ...
- iOS基础 - UIDatePicker and UIPickerView and UITextField
1.UIDatePicker继承自UIControl,因此不会通过代理来监听事件的改变,而是通过addTarget来监听事件.监听的事件是值改变事件. 2.UIPickerView继承自UIView, ...
- Redis一些命令总结
链接操作相关的命令 quit:关闭连接(connection) auth:简单密码认证 持久化 save:将数据同步保存到磁盘 bgsave:将数据异步保存到磁盘 lastsave:返回上次成功将 ...
- Mac OS X安装之虚拟机环境下的总结
最近一直忙着公司iOS Touch的新版发布,终于忙过了.现在,又开始了新的阶段,不过算是轻松了很多.回来一看,自己的博客空空如也,实在受不了了.于是,开始更一下吧,哈哈. 这个文档是我几个月前,开始 ...
- asp.net 的一个简单进度条功能
我们先看下效果 我点击了按钮后他会显示进度页面,进度完成后,进度条消失,其实也是比较简单的了. 我们需要一个进度条代码文件ProgressBar.htm(注意:是没有head这些标签的) <sc ...