# [BZOJ3531] Peaks加强版

## Description

BytemountainsN座山峰，每座山峰有他的高度h_i。有些山峰之间有双向道路相连，共M条路径，每条路径有一个困难值，这个值越大表示越难走，现在有Q组询问，每组询问询问从点v开始只经过困难值小于等于x的路径所能到达的山峰中第k高的山峰，如果无解输出-1。加强版：原题强制在线
Input

$N<=10^{5}, M,Q<=5*10^{5}，h_i,c,x<={10^9}$

## Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
const int BUF_SIZE = (int)1e6 + 10;
struct fastIO {
char buf[BUF_SIZE], buf1[BUF_SIZE];
int cur, cur1;
FILE *in, *out;
fastIO() {
cur = BUF_SIZE, in = stdin, out = stdout;
cur1 = 0;
}
inline char getchar() {
if(cur == BUF_SIZE) fread(buf, BUF_SIZE, 1, in), cur = 0;
return *(buf + (cur++));
}
inline void putchar(char ch) {
*(buf1 + (cur1++)) = ch;
if (cur1 == BUF_SIZE) fwrite(buf1, BUF_SIZE, 1, out), cur1 = 0;
}
inline int flush() {
if (cur1 > 0) fwrite(buf1, cur1, 1, out);
return cur1 = 0;
}
}IO;
#define getchar IO.getchar
#define putchar IO.putchar
char ch = getchar();
int x = 0, flag = 1;
for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
void putString(char s[], char EndChar = '\n') {
rep(i, 0, strlen(s) - 1) putchar(*(s + i));
if(~EndChar) putchar(EndChar);
}

#define Maxn 100009
int n, m, q, x[Maxn * 4], amt, fa[Maxn * 4][21];
struct edge {
int to, nxt;
}g[Maxn * 4];
struct node {
int u, v, w;
int operator < (const node b) const {
return w < b.w;
}
}lst[Maxn * 7];
namespace DSU {
int fa[Maxn * 2];
/**/    inline void init() { rep(i, 1, Maxn * 2 - 1) fa[i] = i;}
int find(int u) { return u ^ fa[u] ? (fa[u] = find(fa[u])) : u; }
inline void merge(int u, int v) { u = find(u), v = find(v); fa[v] = u; }
}
int clk, rt[Maxn * 2], y[Maxn * 2], d, dfn[Maxn * 2], size[Maxn * 2], efn[Maxn * 2];
namespace Chairman {
int tree[Maxn * 60], cnt, lc[Maxn * 60], rc[Maxn * 60];
int modify(int rt, int l, int r, int pos) {
int u = ++amt;
lc[u] = lc[rt], rc[u] = rc[rt], tree[u] = tree[rt] + 1;
if(l == r) return u;
int mid = (l + r) >> 1;
(pos <= mid) ? (lc[u] = modify(lc[rt], l, mid, pos)) : (rc[u] = modify(rc[rt], mid + 1, r, pos));
return u;
}
int query(int prert, int rt, int l, int r, int val) {
if(l == r) return l;
int res = tree[rc[rt]] - tree[rc[prert]], mid = (l + r) >> 1;
return (val <= res) ? query(rc[prert], rc[rt], mid + 1, r, val) : query(lc[prert], lc[rt], l, mid, val - res);
}
}
namespace INIT {
void add(int u, int v) {
}
void dfs(int u, int f) {
fa[u][0] = f;
rep(i, 1, 20) fa[u][i] = fa[fa[u][i - 1]][i - 1];
size[u] = 1; dfn[u] = ++clk; efn[clk] = u;
for(int i = head[u]; ~i; i = g[i].nxt) {
int v = g[i].to;
if(v != f){
dfs(v, u);
size[u] += size[v];
}
}
}
void Main() {
rep(i, 1, n) x[i] = read();
rep(i, 1, m) {
lst[i] = (node){u, v, c};
}
DSU :: init();
sort(lst + 1, lst + m + 1);
int z = 0;
rep(i, 1, m) {
int u = DSU :: find(lst[i].u), v = DSU :: find(lst[i].v);
if(v != u) {
x[++amt] = lst[i].w;
DSU :: merge(amt, lst[i].v);
DSU :: merge(amt, lst[i].u);
if(++z == n - 1) break;
}
}
rep(i, 1, amt) y[i] = x[i];
y[amt + 1] = -1; x[0] = INT_MAX;
sort(y + 1, y + amt + 2);
d = unique(y + 1, y + amt + 2) - y - 1;
dfs(amt, 0);
rep(i, 1, amt)
if(efn[i] <= n)
rt[i] = Chairman :: modify(rt[i - 1], 1, d, lower_bound(y + 1, y + d + 1, x[efn[i]]) - y);
else rt[i] = rt[i - 1];
}
}
namespace SOLVE {
int query(int u, int lim, int kth) {
drep(i, 20, 0) if(x[fa[u][i]] <= lim) u = fa[u][i];
int tmp = Chairman :: query(rt[dfn[u] - 1], rt[dfn[u] + size[u] - 1], 1, d, kth);
return y[tmp];
}
void Main() {
int last_ans = -1;
rep(i, 1, q) {
if(~last_ans) v ^= last_ans, x ^= last_ans, k ^= last_ans;
write(last_ans = query(v, x, k)), putchar('\n');
}
}
}
int main() {
freopen("BZOJ3551.in", "r", stdin);
freopen("BZOJ3551.out", "w", stdout);
INIT :: Main();
SOLVE :: Main();
#ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return IO.flush();
}


posted @ 2018-10-25 14:44  Qrsikno  阅读(114)  评论(0编辑  收藏  举报