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种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
随机推荐
- Mac上安装Charles进行抓包全流程设置
安装 -- 官网下载最新版的Charles版本,按照提示安装即可 破解 -- https://blog.csdn.net/qq_25821067/article/details/79848589. M ...
- Ajax post数据查询
<?php $server = '127.0.0.1'; $user = 'root'; $password = ''; $database = 'yiibaidb'; $officecode ...
- Java8(1)之Lambda表达式初步与函数式接口
Lambda表达式初步 介绍 什么是Lambda表达式? 在如 Lisp.Python.Ruby 编程语言中,Lambda 是一个用于表示匿名函数或闭包的运算符 为何需要lambda表达式? 在 Ja ...
- FreeSwitch 终端命令详细介绍
FreeSwitch版本:1.6.9 以下为部分终端命令 alias 语法: alias [add|stickyadd] <alias> <command> | del [&l ...
- JS中使用base64编码上传下载文件
下载文件:使用FileSaver.js https://github.com/eligrey/FileSaver.js/blob/master/README.md 手机端UC浏览器无法下载 安卓 ...
- selenium的
1.简介 selenium可以认为是反反爬虫的最佳利器,它基本可以等同于真实的浏览器访问,用它可以加载到动态数据,也省去了cookie的操作,但是用这个有一个重大的效率问题.所以selenium可以用 ...
- 24.C# Lambda表达式
1.Lambda表达式的含义 Lambda表达式是C#3.0引入的一种结构,使用它可以简化C#编程. 2.Lambda表达式与匿名方法 我们知道匿名方法可用于事件处理,如下delegate声明了一个匿 ...
- 安装rosetta2016时出现git@172.16.25.11s password: \r\nPermission denied错误,解决方法。
当在source目录执行 ./external/scons-local/scons.py -j8 mode=release bin 时,报错 git@.11s password: \r\nPermis ...
- Python3.0科学计算学习之绘图(一)
基本绘图: (1) plot是标准的绘图库,调用函数plot(x,y)就可以创建一个带有绘图的图形窗口(其中y是x的函数).输入的参数为具有相同长度的数组(或列表):或者plot(y)是plot(r ...
- POJ 3162 bit区间查询最值+树形DP
POJ 3162 『题目链接』POJ 3162 『题目类型』bit区间查询最值+树形DP ✡Problem: 一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远 ...