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种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...
随机推荐
- loj2083 优秀的拆分 [NOI2016] SA
正解:SA 解题报告: 我永远喜欢loj! 显然$AABB$串相当于是由两个$AA$串拼起来的,所以可以先考虑如果求出来了所有$AA$串怎么求答案? 就假如能统计出$st[i]$表示所有以$i$为开头 ...
- 如何彻底禁止win10易升更新(转)
原文:https://blog.csdn.net/qq_33075489/article/details/79755896 add by zhj: 第二步是我自己加的 Win10版本:家庭中文版64位 ...
- 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(四)
这篇文章是对前面三篇的一个总结: 1.从测试结果来看,原生的数据库性能分别是:SQL Server(4587)>Oracle(271)>Mysql(145),测试数据量分别为5W.50W. ...
- table的thead,tbody,tfoot
为了让大表格(table)在下载的时候可以分段的显示,就是说在浏览器解析HTML时,table是作为一个整体解释的,使用tbody可以优化显示. 如果表格很长,用tbody分段,可以一部分一部分地显示 ...
- 关于delete请求,后台接收不到数据
在前端用axios需要这样写 /** * 删除数据 */export function del(url, data = {}) { return axios.delete(url, { data: q ...
- .NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理?(转)
.NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理? 后端开发 作者: Rector 1973 阅读 0 评论 0 收藏 收藏本文 ...
- matlab——之class类(详细总结)
https://blog.csdn.net/qinze5857/article/details/80545885 开篇:搜了一下网上介绍matlab的class类,信息不全,且总结不全面,于是单独he ...
- eclipse 编码改成utf-8
Eclipse的编码格式是系统默认 修改为utf-8,点击Apply and Close 然后项目的编码格式会统一默认utf-8 当然也可以选择other,改成GBK.
- HVP plan
HVP,hier verification plan,建立整个验证的plan,在验证后期,通过vcs的coverage db可以直接进行反标, 包括反标code coverage,function c ...
- java 自动拆箱
Integer a0 = ; Integer a1 = ; Integer b0 = ; Integer b1 = ; ; System.out.println(a0 == a1); >> ...