3545: [ONTAK2010]Peaks

题意:带权图,多组询问与一个点通过边权\(\le x\)的边连通的点中点权k大值


又读错题了,输出点一直WA,问的是点权啊

本题加强版强制在线了,那这道题肯定离线啊,边权从小到大加边不就是煞笔提吗

奇怪的是合并的时候先序遍历才行...中序和后序都T了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
const int N=1e5+5, M=5e5+5;
inline int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
} int n, m, Q, val[N], u, v, w, ans[M];
struct meow{
int u,v,w;
bool operator <(const meow &r)const{return w<r.w;}
}a[M];
struct qmeow{
int u,w,k,id;
bool operator <(const qmeow &r)const{return w<r.w;}
}q[M];
int fa[N];
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);} struct SplayTree{
struct meow{int ch[2], fa, v, size, w;}t[N];
int sz, root;
inline void ini() {
for(int i=1; i<=n; i++) t[i].size=t[i].w=1, t[i].v=val[i];
}
inline int wh(int x) {return t[pa].ch[1]==x;}
inline void update(int x) {t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline void rotate(int x) {
int f=t[x].fa, g=t[f].fa, c=wh(x);
if(g) t[g].ch[wh(f)]=x; t[x].fa=g;
t[f].ch[c]=t[x].ch[c^1]; t[t[f].ch[c]].fa=f;
t[x].ch[c^1]=f; t[f].fa=x;
update(f); update(x);
}
inline void splay(int x, int tar) {
for(; pa!=tar; rotate(x))
if(t[pa].fa != tar) rotate(wh(x)==wh(pa) ? pa : x);
if(tar==0) root=x;
}
void insert(int p) {
int x=root, f=0, v=t[p].v; //printf("insert %d root %d\n",p,x);
while(x) {
if(t[x].v == v) {t[x].w++; t[x].size++; splay(x, 0); return;}
f=x;
x = v<t[x].v ? lc : rc;
}
x=p;
t[f].ch[ v>t[f].v ]=x; t[x].fa=f; //printf("f %d %d\n",f,x);
splay(x, 0); //see(x);
}
void order(int x) {
int l=lc, r=rc;
lc=rc=pa=0; t[x].size=t[x].w;
if(l) order(l);
if(r) order(r);
insert(x);
}
void Merge(int x, int y) {
if(x==y) return; //printf("Merge %d %d\n",x,y);
splay(x, 0); splay(y, 0);
if(t[x].size > t[y].size) swap(x, y);
fa[x]=y; root=y;
order(x);
}
int kth(int x, int k) {
splay(x, 0); int lsize=0; //printf("kth %d %d %d\n",x,k,t[x].size);
if(k>t[x].size) return -1;
k=t[x].size-k+1;
while(x) {
int _=lsize+t[lc].size;
if(k<=_) x=lc;
else if(k<=_+t[x].w) return t[x].v;
else lsize=_+t[x].w, x=rc;
}
return -1;
}
}S; int main() {
freopen("in","r",stdin);
n=read(); m=read(); Q=read();
for(int i=1; i<=n; i++) val[i]=read(), fa[i]=i;
S.ini();
for(int i=1; i<=m; i++) u=read(),v=read(),w=read(),a[i]=(meow){u,v,w};
for(int i=1; i<=Q; i++) u=read(),v=read(),w=read(),q[i]=(qmeow){u,v,w,i}; sort(a+1,a+1+m); sort(q+1, q+1+Q);
int now=1;
for(int i=1; i<=Q; i++) {
int w=q[i].w;
while(now<=m && a[now].w<=w) S.Merge(find(a[now].u), find(a[now].v)), now++;
ans[q[i].id] = S.kth(find(q[i].u), q[i].k);
}
for(int i=1; i<=Q; i++) printf("%d\n",ans[i]);
}

BZOJ 3545: [ONTAK2010]Peaks [Splay启发式合并]的更多相关文章

  1. BZOJ 3545: [ONTAK2010]Peaks( BST + 启发式合并 + 并查集 )

    这道题很好想, 离线, 按询问的x排序从小到大, 然后用并查集维护连通性, 用平衡树维护连通块的山的权值, 合并就用启发式合并.时间复杂度的话, 排序是O(mlogm + qlogq), 启发式合并是 ...

  2. BZOJ.3545.[ONTAK2010]Peaks(线段树合并)

    题目链接 \(Description\) 有n个座山,其高度为hi.有m条带权双向边连接某些山.多次询问,每次询问从v出发 只经过边权<=x的边 所能到达的山中,第K高的是多少. \(Solut ...

  3. BZOJ 3545: [ONTAK2010]Peaks 启发式合并 + 离线 + Splay

    Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询 ...

  4. ●BZOJ 3545 [ONTAK2010]Peaks(离线)

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3545 http://www.lydsy.com/JudgeOnline/problem.ph ...

  5. bzoj 3545: [ONTAK2010]Peaks

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1124  Solved: 304[Submit][Status][Discuss] Descripti ...

  6. bzoj 3545: [ONTAK2010]Peaks Kruskal重构树

    题目: 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经 ...

  7. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  8. splay启发式合并

    3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1889  Solved: 501[Submit][Sta ...

  9. 【BZOJ-2809】dispatching派遣 Splay + 启发式合并

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2334  Solved: 1192[Submi ...

随机推荐

  1. Codeforces780C

    题解:n个气球 从1到n染色,如果a.b和c是不同的正方形,a和b在它们之间有一条直接的路径,b和c之间有一条直接的路径,然后在这三个方块上的气球颜色是不同的. AC代码 #include <s ...

  2. 2017ecjtu-summer training #6 Gym 100952D

    D. Time to go back time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 对于hive使用的一点记录

    最近一段时间因工作需要接触了一些hive上的使用!当然大部分都是比较基本的使用,仅当入门!各位看到有不足之处望多多指正! 废话不多说,开始: 首先是创建数据库 create database '数据库 ...

  4. javaScript事件流是什么?

    一.事件 事件是文档或者浏览器窗口中发生的,特定的交互瞬间. 事件是用户或浏览器自身执行的某种动作,如click,load和mouseover都是事件的名字. 事件是javaScript和DOM之间交 ...

  5. light oj 1152 Hiding Gold

    题目: You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x ...

  6. light oj 1184 Marriage Media

    题目: You run a marriage media. You take some profiles for men and women, and your task is to arrange ...

  7. 基于Vue的页面切换左右滑动效果

    HTML文本页面: <template> <div id="app> <transition :name="direction" mode= ...

  8. IDEA关掉重复代码波浪线

    如图: File----Settings

  9. mysql 中文乱码

  10. eclipse导入包之后中文乱码

    windows ->preferences  ->workspace -> default ->GBK