Orz..跑得还挺快的#10

自从会树链剖分后LCA就没写过倍增了...

这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树

-------------------------------------------------------------------------

#include<bits/stdc++.h>
 
#define rep(i, n) for(int i = 0; i < n; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define M(l, r) (((l) + (r)) >> 1)
#define foreach(i, x) for(__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
 
using namespace std;
 
const int maxn = 200009;
 
int w[maxn], n, N, id[maxn];
vector<int> G[maxn];
 
namespace LCA {
    int top[maxn], son[maxn], dep[maxn], size[maxn], fa[maxn], TOP;
    
    void dfs(int x) {
   size[x] = 1; son[x] = -1;
   foreach(it, G[x]) if(*it != fa[x]) {
   dep[*it] = dep[x] + 1; 
   fa[*it] = x;
   dfs(*it);
   size[x] += size[*it];
   if(!~son[x] || size[*it] > size[son[x]])
       son[x] = *it;
   }
    }
    void DFS(int x) {
top[x] = TOP;
if(~son[x]) DFS(son[x]);
foreach(it, G[x]) if(*it != son[x] && *it != fa[x])
    DFS(TOP = *it);
}
   
void init() {
dep[0] = 0; fa[0] = -1;
dfs(0); DFS(TOP = 0);
}
int LCA(int x, int y) {
for(; top[x] != top[y]; x = fa[top[x]])
if(dep[top[x]] < dep[top[y]]) swap(x, y);
return dep[x] < dep[y] ? x : y;
}
}
 
struct Node {
Node *l, *r;
int s;
} pool[maxn * 40], *pt = pool, *null, *root[maxn];
 
void init() {
null = pt++; null->s = 0;
null->l = null->r = null;
}
 
int v;
Node* modify(Node* t, int l, int r) {
Node* h = pt++;
h->s = t->s + 1;
if(r > l) {
int m = M(l, r);
if(v <= m) {
h->l = modify(t->l, l, m);
h->r = t->r;
} else {
h->l = t->l;
h->r = modify(t->r, m + 1, r);
}
}
return h;
}
 
void build(int x, int fa = -1, Node* p = null) {
v = w[x] + 1;
root[x] = modify(p, 1, N);
foreach(it, G[x]) if(*it != fa)
   build(*it, x, root[x]);
}
 
inline int read() {
char c = getchar();
int ans = 0, f = 1;
for(; !isdigit(c); c = getchar())
   if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) 
   ans = ans * 10 + c - '0';
return ans * f;
}
 
int ans = 0;
void work(bool F) {
int x = (read()^ans) - 1, y = read() - 1, k = read(), lca = LCA::LCA(x, y);
Node *X = root[x], *Y = root[y], *A = root[lca], *P = lca ? root[LCA::fa[lca]] : null;
int L = 1, R = N;
while(L < R) {
int s = X->l->s + Y->l->s - A->l->s - P->l->s, m = M(L, R);
if(s >= k) {
X = X->l; Y = Y->l; A = A->l; P = P->l;
R = m;
} else {
X = X->r; Y = Y->r; A = A->r; P = P->r;
k -= s;
L = m + 1;
}
}
ans = id[L - 1];
printf("%d", ans);
if(F) putchar('\n');
}
 
int main() {
freopen("test.in", "r", stdin);
init();
n = read();
int m = read();
rep(i, n) {
id[i] = w[i] = read();
   G[i].clear();
}
sort(id, id + n);
N = unique(id, id + n) - id;
rep(i, n) w[i] = lower_bound(id, id + N, w[i]) - id;
rep(i, n - 1) {
int u = read() - 1, v = read() - 1;
G[u].push_back(v);
G[v].push_back(u);
}
LCA::init();
build(0);
while(m--) work(m);
return 0;
}

-------------------------------------------------------------------------

2588: Spoj 10628. Count on a tree

Time Limit: 12 Sec  Memory Limit: 128 MB
Submit: 2675  Solved: 606
[Submit][Status][Discuss]

Description

给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。

Input

第一行两个整数N,M。
第二行有N个整数,其中第i个整数表示点i的权值。
后面N-1行每行两个整数(x,y),表示点x到点y有一条边。
最后M行每行两个整数(u,v,k),表示一组询问。

Output

 
M行,表示每个询问的答案。

Sample Input

8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2

Sample Output

2
8
9
105
7

HINT

HINT:

N,M<=100000

暴力自重。。。

Source

BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. bzoj 2588 Spoj 10628. Count on a tree(主席树)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  3. bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】

    算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分 #include<iostream> #include<cstdio> ...

  4. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  5. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  6. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

  7. BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 9280  Solved: 2421 ...

  8. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 7669  Solved: 1894[Submi ...

  9. 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree

    题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...

随机推荐

  1. 如何用一个语句判断一个整数是不是二的整数次幂——从一道简单的面试题浅谈C语言的类型提升(type promotion)

    最近招聘季,看JULY大哥的面试100题时,碰到这么一个扩展问题: 如何用一个语句判断一个整数是不是二的整数次幂?(此题在编程之美也有) easy, 2的整数次幂的二进制形式只有一个1,只要用i和i- ...

  2. cocos2dx工程

    1. create-android-project.sh 进入 pro.android/ ln -s ../Resources ./Resources

  3. Week15(12月16日):授课综述1

    Part I:提问 =========================== 1.(   )类提供了一个对Entity Framework的抽象,能够进行数据持久化并接受数据. A.Layout    ...

  4. Web APIs 基于令牌TOKEN验证的实现

    Web APIs 基于令牌TOKEN验证的实现 概述: ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但 ...

  5. 第五节 Code 128 码

    128码开始於1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,128码是较为复杂的条码系统,而其所能支援的字元也相对地比其他一维条码来得多,又有不同的编码方式可供交互运用 ...

  6. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  7. Tomcat免安装版的环境变量配置以及Eclipse下的Tomcat配置和测试

    Tomcat是目前比较流行的开源且免费的Web应用服务器,在我的电脑上第一次安装Tomcat,再经过网上教程和自己的摸索后,将这个过程 重新记录下来,以便以后如果忘记了可以随时查看. 注意:首先要明确 ...

  8. arduino电子琴(2015-11-04)

    前言 这是论坛上一个坛友问的问题,想做一个可变音调的电子琴,想着正好练练手,就顺手做一下. 接线图

  9. Android的回调

    学了两三周的安卓了,最先开始是看mars老师的视频,看了一两天结合慕课网上的一些安卓视频,到现在算是有点入门了. 安卓立用得比较多的回调函数有点不明是怎么实现的,网上找了一些资料,结合自己的实践,总算 ...

  10. PHP给图片加文字水印

    <?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = ...