HDU_3974

题意

题目可以抽象为有n(n<=50000)个节点的树,每个节点存储一个整数y(0<=y<=1e9),y的初值是-1。

对某一节点x可以有一下两种操作:

1、更新以x为根的子树上所有节点的y值为另一个值(覆盖原来的值);

2、查询节点x当前的y值是多少。

题解

由于更新和询问的次数m(m<=50000)和节点个数n是一个数量级,直接使用线段树的复杂度是O(n*n*logn),会超时。所以需要添加延迟标记。

但是如果把每个节点的编号直接作为线段树的叶子节点,会导致更新区间不连续。

因此需要按照树的深度优先搜索顺序(dfs)重新编号,使得每次更新区间连续,再添加lazy标记,如此不会超时。

代码

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int E = 2;
const int INF = 0x3f3f3f3f;
const int MAX_N = 50000;

struct Node {
	int left, right;
	int task;
	bool lazy;
	Node(int left=0, int right=0, int task=-1)
		: left(left), right(right), task(task), lazy(false) {}
};

Node node[4*MAX_N + E];
vector<int> child[MAX_N + E];
bool vis[MAX_N + E];
int index;
int first[MAX_N + E], last[MAX_N + E];

void dfs(int now) {
	first[now] = ++index;
	for(int i=0; i<child[now].size(); ++i)
		dfs(child[now][i]);
	last[now] = index;
}

void build(int left, int right, int root=1) {
	node[root].left = left;
	node[root].right = right;
	node[root].task = -1;
	node[root].lazy = false;
	if(left == right) return;
	int mid = (left+right)>>1;
	build(left, mid, 2*root);
	build(mid+1, right, 2*root+1);
}

void down(int root) {
	if(node[root].lazy && node[root].left!=node[root].right) {
		node[root<<1].task = node[root<<1|1].task = node[root].task;
		node[root<<1].lazy = node[root<<1|1].lazy = true;
		node[root].lazy = false;
	}
}

void upDate(int left, int right, int task, int root=1) {
	if(node[root].left == node[root].right) {
		node[root].task = task;
		return;
	}
	if(node[root].left==left && right==node[root].right) {
		node[root].task = task;
		node[root].lazy = true;
		return;
	}
	down(root);
	int mid = (node[root].left + node[root].right)>>1;
	if(mid < left) upDate(left, right, task, root<<1|1);
	else if(mid >= right) upDate(left, right, task, root<<1);
	else {
		upDate(left, mid, task, root<<1);
		upDate(mid+1, right, task, root<<1|1);
	}
}

int query(int pos, int root=1) {
	if(node[root].left == node[root].right)
		return node[root].task;
	down(root);
	int mid = (node[root].left + node[root].right)>>1;
	if(mid < pos) return query(pos, root<<1|1);
	else return query(pos, root<<1);
}

void init(int nVertex) {
	for(int i=1; i<=nVertex; ++i) {
		child[i].clear();
		vis[i] = false;
	}
	index = 0;
	build(1, nVertex);
}

int main() {
	int t;
	scanf("%d", &t);
	for(int th=1; th<=t; ++th) {
		int nVertex;
		scanf("%d", &nVertex);
		init(nVertex);
		for(int i=0; i<nVertex-1; ++i) {
			int father, tempChild;
			scanf("%d%d", &tempChild, &father);
			child[father].push_back(tempChild);
			vis[tempChild] = true;
		}
		for(int i=1; i<=nVertex; ++i)
			if(!vis[i]) {
				dfs(i);
				break;
			}
		int nQuery;
		scanf("%d", &nQuery);
		printf("Case #%d:\n", th);
		while(nQuery--) {
			char ch;
			scanf(" %c", &ch);
			if(ch == 'C') {
				int x;
				scanf("%d", &x);
				printf("%d\n", query(first[x]));
			}
			else {
				int x, y;
				scanf("%d%d", &x, &y);
				upDate(first[x], last[x], y);
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值