CF1491H Yuezheng Ling and Dynamic Tree(分块)

CF1491H Yuezheng Ling and Dynamic Tree

description

题目链接

solution

非常清新的小分块题了

前提:将序列分成 n \sqrt{n} n 块,每块有 n \sqrt{n} n 个数,记第 i i i个块的左右边界为 L i , R i L_i,R_i Li,Ri,记第 i i i个数所在块为 b l o c k i block_i blocki

定义 t o p i top_i topi节点 i i i的祖先中,与 i i i不在同一个块的最大编号点

【这个真的很妙,可以说是这道题的解法考察点了】

问题是如何在 a l , r a_{l,r} al,r更新时,对某个块 x x x内的被覆盖节点 i ∈ [ l , r ] i\in[l,r] i[l,r] t o p i top_i topi进行更新??

  • 如果更新后的 a i < L x a_i<L_x ai<Lx,则 a i a_i ai成为新的满足 t o p top top定义的最大编号点【 i i i的祖先都已经全变成了 a i a_i ai的祖先,再加个 a i a_i ai本身】, t o p i = a i top_i=a_i topi=ai
  • 如果更新后的 a i a_i ai仍然属于块 x x x内,则直接链接过去, t o p i = t o p a i top_i=top_{a_i} topi=topai

明白了如何维护块内信息,接下来就是对不同块进行操作了

首先考虑修改操作

  • 散块(只覆盖了块内部分点):直接暴力修改点的 a i a_i ai值,然后暴力重构,单次操作时间复杂度 O ( n ) O(\sqrt{n}) O(n )

  • 整块:懒标记 t a g i tag_i tagi表示点 i i i a i a_i ai还没减多少【比真实的 a i a_i ai多了 t a g i tag_i tagi,未真实操作修改】,但这个时候块内某些点的 t o p i top_i topi可能已经发生了变化,没有实时更新就会影响后面的查询操作,而每次暴力修改又需要整个块的遍历,多个整块几乎就是 O ( O( O(修改区间长度 ) ) )的,无法保证时间复杂度

    实际上,发现对于每个块,经过 n \sqrt{n} n 次整块修改操作后, a i a_i ai都至少会变为 a i − n a_i-\sqrt{n} ain 一定在当前块前面的块

    因此,我们可以维护每个块被修改的次数 c n t x cnt_x cntx c n t x > n cnt_x>\sqrt{n} cntx>n ,就不再暴力修改整块,而是直接整块的区间减法,这个时候的 a i a_i ai一定是其的 t o p i top_i topi,否则就暴力修改

    每个块最多修改 n \sqrt{n} n 次,每次需要 O ( n ) O(\sqrt{n}) O(n )遍历修改,一共有 n \sqrt{n} n 个块,总时间复杂度是 O ( n n ) O(n\sqrt{n}) O(nn )

接着考虑询问操作

lca \text{lca} lca肯定是倍增暴力啦!这里直接使用暴力爬山法

考虑询问 u , v u,v u,v两个点的 l c a lca lca

  • b l o c k u ≠ b l o c k v block_u\neq block_v blocku=blockv,则选择对应 b l o c k block block编号较大的点,跳到其 t o p top top

  • b l o c k u = b l o c k v block_u=block_v blocku=blockv t o p u ≠ t o p v top_u\neq top_v topu=topv,则两个节点一起跳对应的 t o p top top

  • b l o c k u = b l o c k v block_u=block_v blocku=blockv t o p u = t o p v top_u=top_v topu=topv,则一直让节点编号大的点跳父亲 a a a,直到两点相同为止

    【注意在跳的时候,一定要减去所在块的 t a g x tag_x tagx标记,才能得到真正的 t o p / a top/a top/a

一个点跳 t o p top top的次数最多为 n n = n \frac{n}{\sqrt{n}}=\sqrt{n} n n=n 次,跳完 t o p top top就是在一个块里面的跳 a a a,最多也是 n \sqrt{n} n 次,所以单次询问时间复杂度为 O ( n ) O(\sqrt{n}) O(n )

本题最终的时间复杂度为 O ( ( n + q ) n ) O((n+q)\sqrt{n}) O((n+q)n )

code

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define maxn 100005
#define maxB 320
int n, Q, B, B_cnt;
int a[maxn], block[maxn], L[maxB], R[maxB], cnt[maxB], top[maxn], tag[maxB];

void pushup( int x ) {
	for( int i = L[x];i <= R[x];i ++ ) a[i] = max( 1, a[i] - tag[x] );
	tag[x] = 0;
	for( int i = L[x];i <= R[x];i ++ ) {
		if( a[i] < L[x] ) top[i] = a[i];
		else top[i] = top[a[i]];
	}
}

void modify( int l, int r, int x ) {
	if( block[l] == block[r] ) {
		for( int i = l;i <= r;i ++ ) a[i] = max( 1, a[i] - x );
		pushup( block[l] );
	}
	else {
		for( int i = l;i <= R[block[l]];i ++ ) a[i] = max( 1, a[i] - x ); pushup( block[l] );
		for( int i = L[block[r]];i <= r;i ++ ) a[i] = max( 1, a[i] - x ); pushup( block[r] );
		for( int i = block[l] + 1;i < block[r];i ++ ) {
			cnt[i] ++, tag[i] = min( n, tag[i] + x );
			if( cnt[i] <= B ) pushup( i );
		}
	}
}

int query_top( int i ) { return max( 1, top[i] - tag[block[i]] ); }

int query_a( int i ) { return max( 1, a[i] - tag[block[i]] ); }

int lca( int x, int y ) {
	while( 1 ) {
		if( block[x] < block[y] ) swap( x, y );
		if( block[x] ^ block[y] ) x = query_top( x );
		else {
			if( top[x] ^ top[y] ) x = query_top( x ), y = query_top( y );
			else break;
		}
	}
	while( x ^ y ) {
		if( x < y ) swap( x, y );
		x = query_a( x );
	}
	return x;
}

int main() {
	scanf( "%d %d", &n, &Q );
	B = sqrt( n ), B_cnt = ( n - 1 ) / B + 1;
	for( int i = 2;i <= n;i ++ ) {
		scanf( "%d", &a[i] );
		block[i] = ( i - 1 ) / B + 1;
	}
	for( int i = 1;i <= block[n];i ++ ) 
		L[i] = ( i - 1 ) * B + 1, R[i] = min( n, i * B ), pushup( i );
	for( int i = 1, opt, l, r, x;i <= Q;i ++ ) {
		scanf( "%d %d %d", &opt, &l, &r );
		if( opt & 1 ) scanf( "%d", &x ), modify( l, r, x );
		else printf( "%d\n", lca( l, r ) );
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值