UVA1479 Graph and Queries
思路
恶心人的题目
还是类似永无乡一题的Treap启发式合并思路
但是由于加边变成了删边
所以应该离线后倒序处理
数组要开够
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <stack>
using namespace std;
int Nodecnt=0,root[250000*2],fa[250000*2],w_p[250000*2],cnt,n,m;
struct Edge{
int u,v;
}E[250000*2];
struct oper{
int opt,x,k,v;
}Ask[250000*2];
struct Node{
int lson,rson,sz,val,ran;
}Treap[250000*2];
set<int> use;
stack<int> ans;
int find(int x){
if(fa[x]==x)
return x;
else
return fa[x]=find(fa[x]);
}
queue<int> q;
void throwin(int x){
q.push(x);
}
int getnew(int val){
int o;
if(q.size())
o=q.front(),q.pop();
else
o=++Nodecnt;
Treap[o].lson=Treap[o].rson=0;
Treap[o].sz=1;
Treap[o].ran=rand();
Treap[o].val=val;
return o;
}
void pushup(int o){
Treap[o].sz=Treap[Treap[o].lson].sz+Treap[Treap[o].rson].sz+1;
}
void rorateL(int &o){
int x=Treap[o].rson;
Treap[o].rson=Treap[x].lson;
Treap[x].lson=o;
pushup(o);
pushup(x);
o=x;
}
void rorateR(int &o){
int x=Treap[o].lson;
Treap[o].lson=Treap[x].rson;
Treap[x].rson=o;
pushup(o);
pushup(x);
o=x;
}
void insert(int val,int &o){
if(!o){
o=getnew(val);
return;
}
Treap[o].sz++;
if(val<=Treap[o].val){
insert(val,Treap[o].lson);
if(Treap[Treap[o].lson].ran<Treap[o].ran)
rorateR(o);
}
else{
insert(val,Treap[o].rson);
if(Treap[Treap[o].rson].ran<Treap[o].ran)
rorateL(o);
}
}
void del(int val,int &o){
if(o==0)
return;
if(Treap[o].val==val){
if(Treap[o].lson*Treap[o].rson==0){
throwin(o);
o=Treap[o].lson+Treap[o].rson;
return;
}
else{
if(Treap[Treap[o].lson].ran<Treap[Treap[o].rson].ran){
rorateR(o);
Treap[o].sz--;
del(val,Treap[o].rson);
return;
}
else{
rorateL(o);
Treap[o].sz--;
del(val,Treap[o].lson);
return;
}
}
}
else if(val<Treap[o].val){
Treap[o].sz--;
del(val,Treap[o].lson);
return;
}
else{
Treap[o].sz--;
del(val,Treap[o].rson);
return;
}
}
int query(int val,int o){
if(!o)
return 0;
if(val==Treap[Treap[o].rson].sz+1)
return Treap[o].val;
else if(val>Treap[Treap[o].rson].sz+1)
return query(val-Treap[Treap[o].rson].sz-1,Treap[o].lson);
else
return query(val,Treap[o].rson);
}
void dfs(int &o,int to){
if(!o)
return;
insert(Treap[o].val,root[to]);
dfs(Treap[o].lson,to);
dfs(Treap[o].rson,to);
throwin(o);
o=0;
}
void init(void){
Nodecnt=0;
cnt=0;
memset(Treap,0,sizeof(Treap));
memset(root,0,sizeof(root));
memset(fa,0,sizeof(fa));
memset(w_p,0,sizeof(w_p));
memset(E,0,sizeof(E));
memset(Ask,0,sizeof(Ask));
while(!q.empty())
q.pop();
use.clear();
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
int inq=0;
while(scanf("%d %d",&n,&m)==2){
inq++;
if(n==0&&m==0)
break;
init();
for(int i=1;i<=n;i++){
scanf("%d",&w_p[i]);
fa[i]=i;
}
for(int i=1;i<=m;i++)
scanf("%d %d",&E[i].u,&E[i].v),use.insert(i);
char opt=getchar();
while(opt!='E'&&opt!='Q'&&opt!='C'&&opt!='D')
opt=getchar();
while(opt!='E'){
++cnt;
if(opt=='D'){
Ask[cnt].opt=1;
scanf("%d",&Ask[cnt].x);
use.erase(Ask[cnt].x);
}
else if(opt=='Q'){
Ask[cnt].opt=2;
scanf("%d %d",&Ask[cnt].x,&Ask[cnt].k);
}
else if(opt=='C'){
Ask[cnt].opt=3;
scanf("%d %d",&Ask[cnt].x,&Ask[cnt].v);
int t=w_p[Ask[cnt].x];
w_p[Ask[cnt].x]=Ask[cnt].v;
Ask[cnt].v=t;
}
opt=getchar();
while(opt!='E'&&opt!='Q'&&opt!='C'&&opt!='D')
opt=getchar();
}
for(int i=1;i<=n;i++)
insert(w_p[i],root[i]);
for(auto it = use.begin();it!=use.end();it++){
int a=E[(*it)].u,b=E[(*it)].v;
if(find(a)!=find(b)){
if(Treap[root[find(a)]].sz<Treap[root[find(b)]].sz){
dfs(root[find(a)],find(b));
fa[find(a)]=find(b);
}
else{
dfs(root[find(b)],find(a));
fa[find(b)]=find(a);
}
}
}
for(int i=cnt;i>=1;i--){
if(Ask[i].opt==1){
int a=E[Ask[i].x].u,b=E[Ask[i].x].v;
if(find(a)!=find(b)){
if(Treap[root[find(a)]].sz<Treap[root[find(b)]].sz){
dfs(root[find(a)],find(b));
fa[find(a)]=find(b);
}
else{
dfs(root[find(b)],find(a));
fa[find(b)]=find(a);
}
}
}
else if(Ask[i].opt==2){
ans.push(query(Ask[i].k,root[find(Ask[i].x)]));
}
else{
del(w_p[Ask[i].x],root[find(Ask[i].x)]);
w_p[Ask[i].x]=Ask[i].v;
insert(w_p[Ask[i].x],root[find(Ask[i].x)]);
}
}
double mid=0.0;
int qqq=0;
while(!ans.empty()){
qqq++;
// printf("%d\n",ans.top());
mid+=ans.top();
ans.pop();
}
printf("Case %d: %.6lf\n",inq,mid/qqq);
}
return 0;
}
UVA1479 Graph and Queries的更多相关文章
- HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
- HDU 3726 Graph and Queries (离线处理+splay tree)
Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3726 Graph and Queries treap树
题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...
- 题解 UVA1479 【Graph and Queries】
\[ \text{Preface} \] 算是一道思维难度稍易,代码难度稍难的题吧. \[ \text{Description} \] 给出一张 \(n\) 个点,\(m\) 条边的图,点带权.需要支 ...
- HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)
Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...
- UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...
- UVa 1479 (Treap 名次树) Graph and Queries
这题写起来真累.. 名次树就是多了一个附加信息记录以该节点为根的树的总结点的个数,由于BST的性质再根据这个附加信息,我们可以很容易找到这棵树中第k大的值是多少. 所以在这道题中用一棵名次树来维护一个 ...
- uvalive 5031 Graph and Queries 名次树+Treap
题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
随机推荐
- iOS 代码混淆
一般做了防调试的话,被调试进程会退出的,是防动态分析措施. 代码混淆加花这些是防静态分析措施. 反调试是防动态分析措施. 混淆的方法方法名混淆其实就是字符串替换,有2个方法可以,一个是#define, ...
- Windows下MongoDB设置用户、密码
在默认情况下,mongod是监听在127.0.0.1之上的,任何客户端都可以直接连接27017,且没有认证. 好处是,用户可以即时上手,不用担心被一堆配置弄的心烦意乱. 坏处是,公网服务器搭建Mong ...
- Redis的数据结构之sorted-set
存储Sorted-Set Sorted-Set和Set的区别 Sorted-Set中的成员在集合中的位置是有序的 存储Sorted-set常用命令 添加元素 获得元素 删除元素 范围查询 扩展命令 z ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- 一、初识CocoaPods——XCode的依赖库管理工具
概述 任意一款功能完整的APP,其中所涉及的内容都将是来自各个领域各个方面的.如果每个领域的每个方面都要重新开发并给予充分测试,那么1个APP的开发周期将会变得非常漫长,长到足以让房价再涨一倍,长到足 ...
- A-the Beatles
传送门: 题意:题目给出n,k分别代表在这个环中饭店的个数和两个饭店相离的距离.然后再给出一组a,b分别代表在某一点s里最近饭店的距离和在这个s点走一步之后到达的点离最近饭店的距离. 然后问这个人再次 ...
- 从零开始一起学习SLAM | 掌握g2o边的代码套路
点"计算机视觉life"关注,置顶更快接收消息! 小白:师兄,g2o框架<从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码>,以及顶点<从零开始 ...
- dubbo搭建
1.安装java : yum install java 2.下载Tomcat: wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.1 ...
- Python_lambda
最近学习到python的lambda表达式也是匿名函数, lambda不需要使用def 语句这样标准的形式定义一个函数,并不需要花很多时间去额外定义一个不常用的函数.lambda的本省就是一个长度为一 ...
- 《ASP.NET Core In Action》读书笔记系列二 ASP.NET Core 能用于什么样的应用,什么时候选择ASP.NET Core
ASP.NET Core 能用于什么样的应用 ASP.NET Core 可以用作传统的web服务.RESTful服务.远程过程调用(RPC)服务.微服务,这归功于它的跨平台支持和轻量级设计.如下图所示 ...