题目大意:给出一棵树,要求维护:1.求出以x为根节点的子树的严格大于y的数量。
2.将一个节点的权值改变。
3.在一个节点下加一个权值为y的节点。
思路:分块这个东西太神了(别找我分析时间复杂度。。树上的分块更神。。。
首先,分块的原则和正常分块一样,把一棵树分成√n块,每一块不超过√n个,然后所有的时间复杂度降到了O(√n),(这个题还有个排序,所以还有一个log(n))。
如何在树上分块。规定根节点在编号为1的块中,然后做一次深搜,如果遍历到的一个节点的时候当前节点所在的块的数量已经达到最大的数量,那么就把遍历的子节点新建一个块,不然的话就把遍历的节点加到这个块中。
至于改值,怎么暴力怎么来,我直接改掉之后快排都能过。
加点的时候,分两种情况讨论,1.如果x节点所在块的数量还没有达到最大值,那就把y节点加进去,然后对整个序列快排。2.如果达到了最大的值,就新建一个块。
最后询问的时候,由于每一次操作之后块里存的数组都是有序的,因此查找只需要二分。写两个递归的Count函数,在不整的块中暴力查找,在整的块中二分查找。
CODE:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 30010 #define MAXP 10000 using namespace std; struct Block{ int num[1000],size; int Ask(int k) { int temp = upper_bound(num + 1,num + size + 1,k) - num - 1; return size - temp; } }block[MAXP]; struct BlockGraph{ int head[MAXP],total; int next[MAXP << 1],aim[MAXP << 1]; void Add(int x,int y) { next[++total] = head[x]; aim[total] = y; head[x] = total; } }graph; int points,asks,block_size,blocks; int head[MAX << 1],total; int next[MAX << 2],aim[MAX << 2]; int father[MAX << 1]; int src[MAX << 1],belong[MAX << 1]; inline void Add(int x,int y) { next[++total] = head[x]; aim[total] = y; head[x] = total; } void DFS(int x,int last) { int from = belong[x]; block[from].num[++block[from].size] = src[x]; father[x] = last; for(int i = head[x]; i; i = next[i]) { if(aim[i] == last) continue; if(block[from].size < block_size) belong[aim[i]] = from; else belong[aim[i]] = ++blocks; if(belong[aim[i]] != from) graph.Add(from,belong[aim[i]]); DFS(aim[i],x); } } int CountBlock(int x,int k) { int re = block[x].Ask(k); for(int i = graph.head[x]; i; i = graph.next[i]) re += CountBlock(graph.aim[i],k); return re; } int Count(int x,int k,int last) { int re = src[x] > k; for(int i = head[x]; i; i = next[i]) { if(aim[i] == last) continue; if(belong[x] == belong[aim[i]]) re += Count(aim[i],k,x); else re += CountBlock(belong[aim[i]],k); } return re; } int main() { cin >> points; for(int x,y,i = 1; i < points; ++i) { scanf("%d%d",&x,&y); Add(x,y),Add(y,x); } for(int i = 1; i <= points; ++i) scanf("%d",&src[i]); cin >> asks; block_size = (int)sqrt(points * log(points) / log(2)); belong[1] = ++blocks; DFS(1,0); for(int i = 1; i <= blocks; ++i) sort(block[i].num + 1,block[i].num + block[i].size + 1); int last_ans = 0; for(int flag,x,y,i = 1; i <= asks; ++i) { scanf("%d%d%d",&flag,&x,&y); x ^= last_ans; y ^= last_ans; if(flag == 0) printf("%d\n",last_ans = Count(x,y,father[x])); else if(flag == 1) { static int from; from = belong[x]; for(int i = 1; i <= block[from].size; ++i) if(block[from].num[i] == src[x]) { block[from].num[i] = src[x] = y; break; } sort(block[from].num + 1,block[from].num + block[from].size + 1); } else { static int from; from = belong[x]; Add(x,++points); src[points] = y; father[points] = x; if(block[from].size < block_size) { block[from].num[++block[from].size] = y; sort(block[from].num + 1,block[from].num + block[from].size + 1); belong[points] = from; } else { graph.Add(from,++blocks); block[blocks].num[++block[blocks].size] = y; belong[points] = blocks; } } } return 0; }
原文地址:http://blog.csdn.net/jiangyuze831/article/details/41445003