说多了都是泪啊...调了这么久..

离线可以搞 , 树链剖分就OK了...

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )
#define REP( x ) for( edge* e = head[ x ] ; e ; e = e -> next )
#define mod( x ) ( ( x ) %= MOD )
#define M( l , r ) ( ( l + r ) >> 1 )
#define L( x ) ( ( x ) << 1 )
#define R( x ) ( L( x ) ^ 1 )
 
using namespace std;
 
const int maxn = 50000 + 5;
const int maxnode = 140000;
const int MOD = 201314;
 
int n;
  
struct edge {
int to;
edge* next;
};
 
edge* pt , EDGE[ maxn ];
edge* head[ maxn ];
 
inline void add_edge( int u , int v ) {
pt -> to = v;
pt -> next = head[ u ];
head[ u ] = pt++;
}
 
void edge_init() {
pt = EDGE;
clr( head , 0 );
}
 
 
int top[ maxn ] , fa[ maxn ] , son[ maxn ] , size[ maxn ];
 
void dfs( int x ) {
size[ x ] = 1;
REP( x ) {
int to = e -> to;
dfs( to );
size[ x ] += size[ to ];
if( son[ x ] == -1 || size[ to ] > size[ son[ x ] ] )
   son[ x ] = to;
}
}
 
int id[ maxn ] , id_cnt = 0;
int TOP;
void DFS( int x ) {
id[ x ] = ++id_cnt;
top[ x ] = TOP;
if( son[ x ] != -1 )
   DFS( son[ x ] );
REP( x ) if( e -> to != son[ x ] )
   DFS( TOP = e -> to );
}
 
void DFS_init() {
clr( son , -1 );
dfs( 0 );
DFS( TOP = 0 );
}
 
 
int add[ maxnode ] , sum[ maxnode ];
int L , R , query_ans;
 
inline void maintain( int x , int l , int r ) {
sum[ x ] = add[ x ] * ( r - l + 1 );
if( r > l )
   sum[ x ] += sum[ L( x ) ] + sum[ R( x ) ];
}
 
void update( int x , int l , int r ) {
if( L <= l && r <= R )
   add[ x ]++;
else {
int m = M( l , r );
if( L <= m ) update( L( x ) , l , m );
if( m < R) update( R( x) , m + 1 , r );
}
maintain( x , l , r );
}
inline void Update( int l , int r ) {
L = l , R = r;
update( 1 , 1 , n );
}
 
void query( int x , int l , int r , int Add ) {
if( L <= l && r <= R ) 
   query_ans += sum[ x ] + Add * ( r - l + 1 );
else {
Add += add[ x ];
int m = M( l , r );
if( L <= m ) query( L( x ) , l , m , Add );
if( m < R ) query( R( x ) , m + 1 , r , Add );
}
}
inline int Query( int l , int r ) {
query_ans = 0;
L = l , R = r;
query( 1 , 1 , n , 0 );
return query_ans;
}
 
int Q( int x ) {
int res = 0;
while( top[ x ] != top[ 0 ] ) {
mod( res += Query( id[ top[ x ] ] , id[ x ] ) );
x = fa[ top[ x ] ];
}
return ( res + Query( id[ 0 ] , id[ x ] ) ) % MOD;
}
 
void modify( int x ) {
while( top[ x ] != top[ 0 ] ) {
Update( id[ top[ x ] ] , id[ x ] );
x = fa[ top[ x ] ];
}
Update( id[ 0 ] , id[ x ] );
}
 
 
struct data {
int x , num , s;
bool operator < ( const data &rhs ) const {
return x < rhs.x;
}
};
 
data A[ maxn << 1 ];
int z[ maxn ];
int ans[ maxn ];
 
int read() {
int ans = 0;
char c = getchar();
while( ! isdigit( c ) ) c = getchar();
while( isdigit( c ) ) {
ans = ans * 10 + c - '0';
c = getchar();
}
return ans;
}
 
int main() {
freopen( "test.in" , "r" , stdin );
n = read();
int q = read();
edge_init();
Rep( i , n - 1 ) {
   fa[ i ] = read();
   add_edge( fa[ i ] , i );
}
rep( i , q ) {
data &a = A[ i ] , &b = A[ i + q ];
a.x = read() , b.x = read();
*( z + i ) = read();
a.x--;
a.s = -1 , b.s = 1;
a.num = b.num = i;
}
DFS_init();
clr( sum , 0 );
q <<= 1;
sort( A , A + q );
int cur = 0;
clr( ans , 0 );
for( ; cur < q && A[ cur ].x < 0 ; cur++ );
rep( i , n ) {
modify( i );
for( ; cur < q , A[ cur ].x == i ; cur++ )
   mod( mod( ans[ A[ cur ].num ] += Q( z[ A[ cur ].num ] ) * A[ cur ].s ) += MOD );
}
q >>= 1;
rep( i , q )
   printf( "%d\n" , ans[ i ] );
return 0;
}

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

3626: [LNOI2014]LCA

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 855  Solved: 291
[Submit][Status][Discuss]

Description

给出一个n个节点的有根树(编号为0到n-1,根节点为0)。一个点的深度定义为这个节点到根的距离+1。
设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先。
有q次询问,每次询问给出l r z,求sigma_{l<=i<=r}dep[LCA(i,z)]。
(即,求在[l,r]区间内的每个节点i与z的最近公共祖先的深度之和)

Input

第一行2个整数n q。
接下来n-1行,分别表示点1到点n-1的父节点编号。
接下来q行,每行3个整数l r z。

Output

输出q行,每行表示一个询问的答案。每个答案对201314取模输出

Sample Input

5 2
0
0
1
1
1 4 3
1 4 2

Sample Output

8
5

HINT

共5组数据,n与q的规模分别为10000,20000,30000,40000,50000。

Source

BZOJ 3626: [LNOI2014]LCA( 树链剖分 + 离线 )的更多相关文章

  1. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  2. BZOJ 3626: [LNOI2014]LCA 树链剖分 线段树 离线

    http://www.lydsy.com/JudgeOnline/problem.php?id=3626 LNOI的树链剖分题没有HAOI那么水,学到的东西还是很多的. 我如果现场写,很难想出来这种题 ...

  3. bzoj 3626 : [LNOI2014]LCA (树链剖分+线段树)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...

  4. BZOJ 3626 [LNOI2014]LCA ——树链剖分

    思路转化很巧妙. 首先把询问做差分. 然后发现加入一个点就把路径上的点都+1,询问的时候直接询问到根的路径和. 这样和原问题是等价的,然后树链剖分+线段树就可以做了. #include <map ...

  5. 洛谷 P4211 [LNOI2014]LCA (树链剖分+离线)

    题目:https://www.luogu.org/problemnew/solution/P4211 相当难的一道题,其思想难以用言语表达透彻. 对于每个查询,区间[L,R]中的每个点与z的lca肯定 ...

  6. bzoj3626: [LNOI2014]LCA (树链剖分+离线线段树)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. ...

  7. [LNOI2014]LCA 树链剖分 离线 前缀和 思维题

    题目描述:给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. 有q次询问,每 ...

  8. [BZOJ3626] [LNOI2014]LCA(树链剖分)

    [BZOJ3626] [LNOI2014]LCA(树链剖分) 题面 给出一棵N个点的树,要求支持Q次询问,每次询问一个点z与编号为区间[l,r]内的点分别求最近公共祖先得到的最近公共祖先深度和.N, ...

  9. BZOJ3626[LNOI2014]LCA——树链剖分+线段树

    题目描述 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q次询问,每次询 ...

随机推荐

  1. Mongodb PHP开发类库

    <?php /** * Mongodb 基本操作API,支持基本类似关系统型数据库的操作接口 * * @version 1.0 * * [说明] * * 1:该版本API实现了 Mongodb ...

  2. [LeetCode]题解(python):077-Combinations

    题目来源: https://leetcode.com/problems/combinations/ 题意分析: 给定一个n和k,输出1到n的所有k个数的组合.比如n = 4,k=2 [ [2,4], ...

  3. 从基因组可视化工具——circos说起,circos安装

      这是博客改版的第一篇博文,选择最近使用的生物信息学软件——circos开始写起.circos是用perl写的生物软件,从发表的文章来看 学习circos主要是熟悉配置文件的编辑方法,搞清楚其中的标 ...

  4. java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp

    http://stackoverflow.com/questions/13269564/java-lang-classcastexception-oracle-sql-timestamp-cannot ...

  5. 基于Visual C++2013拆解世界五百强面试题--题3-打印螺旋数组

    请用C语言实现 输入N,打印N*N矩阵 比如 N = 3, 打印: 1 2 3 8 9 4 7 6 5 N = 4, 打印 1   2    3   4 12  13   14  5 11  16   ...

  6. BZOJ 1874 取石子游戏 (NIM游戏)

    题解:简单的NIM游戏,直接计算SG函数,至于找先手策略则按字典序异或掉,去除石子后再异或判断,若可行则直接输出. #include <cstdio> const int N=1005; ...

  7. HDU 3466 Proud Merchants

    题目大意:现在给出商品,有三个参数,记为pi,qi,vi,vi是商品的在你心里价值,pi是商品的价格,qi是你要买商品的时候至少需要的钱然后求可得的最大价值. 单词积累:Merchants 商人  t ...

  8. leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

    这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...

  9. bulk insert data into database with table type .net

    1. Create Table type in Sqlserver2008. CREATE TYPE dbo.WordTable as table ( [WordText] [nchar]() NUL ...

  10. C++对C语言的非面向对象特性扩充(2)

    上一篇随笔写了关于C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较,也得到了几位学习C++朋友们的帮助讲解,十分感谢,我也希望欢迎有更多学习C++的朋友一起 ...