思路

恶心人的题目

还是类似永无乡一题的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的更多相关文章

  1. HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. [la P5031&hdu P3726] Graph and Queries

    [la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: ...

  3. HDU 3726 Graph and Queries (离线处理+splay tree)

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. HDU 3726 Graph and Queries treap树

    题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...

  5. 题解 UVA1479 【Graph and Queries】

    \[ \text{Preface} \] 算是一道思维难度稍易,代码难度稍难的题吧. \[ \text{Description} \] 给出一张 \(n\) 个点,\(m\) 条边的图,点带权.需要支 ...

  6. 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 ...

  7. UVALive5031 Graph and Queries(Treap)

    反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...

  8. UVa 1479 (Treap 名次树) Graph and Queries

    这题写起来真累.. 名次树就是多了一个附加信息记录以该节点为根的树的总结点的个数,由于BST的性质再根据这个附加信息,我们可以很容易找到这棵树中第k大的值是多少. 所以在这道题中用一棵名次树来维护一个 ...

  9. uvalive 5031 Graph and Queries 名次树+Treap

    题意:给你个点m条边的无向图,每个节点都有一个整数权值.你的任务是执行一系列操作.操作分为3种... 思路:本题一点要逆向来做,正向每次如果删边,复杂度太高.逆向到一定顺序的时候添加一条边更容易.详见 ...

随机推荐

  1. 从手机中提取boot.img

    测试环境:nexus 5,android 6.0 怕出问题可以先用TWRP备份 adb shell su cd /dev/block/platform/msm_sdcc./by-name ls -l ...

  2. main.js中封装全局登录函数

    1. 在 main.js 中封装全局登录函数 通过 vue 对象的原型扩展,可以扩展一个函数,这样这个函数就可以在每一个界面通过类似指向对象的方式,去访问这个函数. 如下是 main.js 扩展的函数 ...

  3. ASM检查RAC是否成功

    [grid@asm ~]$ crsctl status resourceNAME=ora.DATA.dgTYPE=ora.diskgroup.typeTARGET=ONLINESTATE=ONLINE ...

  4. C# .net 语言加密方案

    C# .net 语言加密方案 方案背景 当前C# .net语言的应用范围越来越广泛,IIS 的服务器架构后台代码.桌面应用程序的 winform .Unity3d 的逻辑脚本都在使用.C# .net ...

  5. Navicat for MySQL下载安装和破解教程

    1.进https://navicatformysql.en.softonic.com/官网 2.第二步 3.第三步等待下载完成 4.第四步双击 二,破解 1.链接:https://pan.baidu. ...

  6. sql 生成随机字符串

    生成三位随机字母+12位数字 ),), @c int; select @CardCode=abs(CHECKSUM(NEWID())) -LEN(@CardCode); ,@c)) set @Card ...

  7. lotus domino 软件学习网站(自己收藏的)

    lotus domino 软件学习网站(自己收藏的) 我学习lotus domino时间也不是很长,相比较学习lotus,学习java的时间还是比较长的,刚开始看网上的说法都是不看好lotus的, 但 ...

  8. 2017.11.19 C语言基础及流水灯实现

    /* 从右往左*/ #include <reg52.h> sbit ADDR0 = P1^0; sbit ADDR1 = P1^1; sbit ADDR2 = P1^2; sbit ADD ...

  9. springboot+web文件上传和下载

    一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...

  10. JAVA课堂测试之一位数组可视化

    代码: package test;//求最大子数组 import java.util.Scanner; import javax.swing.JOptionPane; public class shu ...