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. [javascript]MooTools Selectors(MooTools 选择器) ELEMENT DOM选择

    //ELEMENT DOM选择//on are tag names. //All the divs on the page: $$('div'); //All the divs and paragra ...

  2. [LeetCode]题解(python):058-Length of Last Word

    题目来源: https://leetcode.com/problems/length-of-last-word/ 题意分析: 给出只包括大小写和空格的字符,输出最后一个单词的长度. 题目思路: 从最后 ...

  3. (C)高级排序法

    1.快速排序法 //方法1 从大到小 #include <iostream.h> void run(int* pData,int left,int right) { int i,j; in ...

  4. JQuery 实现返回顶部效果

    首先要里了解一下几个知识 $(window).scrollTop() ---滚动条距顶部距离 fadeIn() 方法使用淡入效果来显示被选元素,假如该元素是隐藏的. fadeOut() 方法使用淡出效 ...

  5. 转:让ie6-8支持部分css3样式的方案

    我们都知道,IE浏览器暂不支持CSS3的一些属性.国外的工程师们,不安于此现状,他们总是尽量使用一些手段使IE浏览器也能支持CSS3属性,我觉得这些都是很有意义,很有价值的工作,可以推动整个技术领域的 ...

  6. Android 网络交互之MD5为什么要加盐

    MD5为什么要加盐 之前面试的时候,遇到一个面试的哥哥.不停的跟我确认我对网络传输过程中的password进行MD5加密的时候,是否加key了. 当时我很纳闷,因为MD5本身已经是不可逆的了,需要破解 ...

  7. mysql merge table

    SELECT COUNT(*) FROM `comment` SHOW CREATE TABLE `comment` CREATE TABLE `comment1` ( `id` INT(8) NOT ...

  8. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  9. C++拷贝构造函数详解

    转自:http://blog.csdn.net/lwbeyond/article/details/6202256 对于一个空类,编译器默认生成四个成员函数:默认构造函数.析构函数.拷贝构造函数.赋值函 ...

  10. ViewPager+Fragment实现支持左右滑动的Tab

    主要思想:顶部标题栏top.xml,中间ViewPager(4个Fragment),底部导航 top.xml和bottom.xml在我之前的两个随笔里有,此处不再赘述. activity_main.x ...