思路

恶心人的题目

还是类似永无乡一题的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. 发现了一个比较有意思的url参数

    今天登录阿里云发现需要二次验证了,手机号不是我的很麻烦,然后就看到有个手机app快捷登录的方式,点进去一看,链接地址是这样的http://qd.alibaba.com/onekey.htm?spm=0 ...

  2. es6下 vue实例属性template不能使用

    esm模式下 不能使用template,需要引入非esm的vue.js,查看vue源码的包的dist目录下 文件标有esm是支持ems,没有标记,就是不支持(这个知识,怎么说了,应该属于webpack ...

  3. Oracle集群时区

    1.环境及问题 OS:SUSE 12SP3 DB:12.2.0.1.190115 2节点RAC Q:集群日志的时间和主机时间相差较大 grid@WWJD1:~> date Mon Feb 11 ...

  4. centOS 7 设置DNS方法 同之前版本不同

    在CentOS 7下,手工设置 /etc/resolv.conf 里的DNS,过了一会,发现被系统重新覆盖或者清除了.和CentOS 6下的设置DNS方法不同,有几种方式: 1.使用全新的命令行工具 ...

  5. ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

    问题 ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set r ...

  6. vue二、脚手架搭建

    1:安装nodeJs(下载一路回车) https://nodejs.org/zh-cn/ 2:检验nodeJs是否安装成功 (注意nodeJs是否添加到window路径中) 进入cmd -> n ...

  7. Linux 命令整理-ps

    ps 命令 ps -ef | grep tomcat ps -ef :以长格式(全格式)显示所有进程:“|” :是管道grep :检索tomcat :与字符tomcat有关的进程 ps[选项]-e:显 ...

  8. Unity3D判断当前所在平台

    Unity3D是一个跨平台的开发工具,支持的平台五花八门,常常开发一款游戏要发布到不同的平台,在不同的平台上会使用不同的代码,难道要我们各平台分别使用一套代码,单独编译一次吗?当然不用了,呵呵.    ...

  9. Seq2Seq ---学习笔记

    应用场景:机器翻译 与language model 不同 MT model 的a<0> 是由encoder 生成的. language model 的 a<0> 是 初始化的. ...

  10. pygame 游戏舞台搭建典型应用

    #两个文件要放在同一目录中,包括图片 一.搭建游戏舞台主程序 #!/usr/bin/env python3#_*_coding:utf-8_*_ author ==$ VACyp import sys ...