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. Robot Framework学习路线

    0. 官方网站  http://robotframework.org/ 所有资料都来自这里,从这里找到必要的链接,从而深入其中的细节. 1.  Quick Start Guide https://co ...

  2. C++_01_入门

    一.类的定义 Person.h类声明 Person.cpp类实现 main.cpp主函数 二.命名空间的使用 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv ...

  3. Codeforces 57C Array dp暴力找到规律

    主题链接:点击打开链接 的非增量程序首先,计算, 如果不增加的节目数量x, 非减少一些方案是x 答案就是 2*x - n 仅仅需求得x就可以. 能够先写个n3的dp,然后发现规律是 C(n-1, 2* ...

  4. 在CTime类中重载&lt;&lt;和&gt;&gt;

    程序代码: #include <iostream> using namespace std; class CTime//时间类 { private: unsigned short int ...

  5. 学习OpenBlas

    编译 从OpenBlas Home Page 上下载源代码.make, make install 使用 level 1 向量-向量 操作 #include <iostream> #incl ...

  6. java面向对象中的String类中12种常用的方法

    1.字符串与字符数组的转换 字符串可以使用toCharArray()方法变成一个字符数组,也可以使用String类的构造方法把一个字符数组变成一个字符串. public class StringAPI ...

  7. Hadoop Error:Name node is in safe mode的解决方法

    Error:name node is in  safe mode 解决方法:hadoop dfsadmin -safemode leave(见图)

  8. JMS 企业开发流程实现

    关于JMS的一些介绍参见[http://blog.csdn.net/aking21alinjuju/article/details/6051421] [补充] 消息的组成 1. 头(head) 每条J ...

  9. Qt中所有类型之间的转换

    1.char * 与 const char *的转换 char *ch1="hello11";const char *ch2="hello22";ch2 = c ...

  10. poj 2356鸽笼原理水题

    关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...