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. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. Linux解压缩总结

    看文件名的后缀名,不同的后缀的文件解压和压缩的命令都不一样总结如下: *.tar 用 tar –xvf 解压 *.gz 用 gzip -d或者gunzip 解压 *.tar.gz和*.tgz 用 ta ...

  3. 转: seajs手册与文档之--模块定义

    模块定义 define id dependencies factory exports require require.async require.resolve require.load requi ...

  4. 转: css实现垂直居中的方法

    利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 用 CSS 实现垂直居中并不容易.有些方法在一些浏览器中 ...

  5. 蝕刻技術(Etching Technology)

    1. 前言 蚀刻是将材料使用化学反应或物理撞击作用而移除的技术. 蚀刻技术可以分为『湿蚀刻』(wet etching)及『干蚀刻』(dry etching)两类.在湿蚀刻中是使用化学溶液,经由化学反应 ...

  6. Tour(KM算法)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  7. 最全的LBS手机定位技术说明

    随着手机技术的发展定位方式也发生了非常大的变化.获取手机位置有非常多种方式. 第一种:CELL-ID定位原理 通过移动网络获取设备当前所在的Cell信息来获取设备当前位置.当设备位置更新设备会向当前服 ...

  8. javascript特效:会随着鼠标而动的眼睛

    这个特效非常简单,其中眼球和眼珠都是特定的图片.只要掌握好距离坐标就没问题.我就直接贴代码,有兴趣的朋友可以自己复制下来运行一下,下面的眼睛图像就是我的文件用到的图像,比较难看..我就把我的代码贴出来 ...

  9. ExtJS学习第一天 MessageBox

    此文用来记录学习笔记: •学习任何技术,首先都要从Helloworld开始,那么我们首要任务就是写一个简单的HelloWorld程序,带领同学们走进ExtJS的世界. •Ext.onReady:这个方 ...

  10. iOS UIWebView 之 UIProgressView

    之前做等待跳转都是用UIActivityIndicatorView ,后来做webView 加载页面的时候,发现了一个特别好用又超级炫酷的加载提示NJKWebViewProgress,作者巧妙的通过计 ...