bzoj 3720: Gty的妹子树

树分块(是不是应该叫块状树了??)

先分块,然后把块之间连起来,重构树,然后对块内排序,二分查询,块外的话,一边搜索,就找出来了。

在新加点的时候,要注意块>=B之后,就开新块,修改之后也要在排序,,,各种麻烦233

(然而感觉还是数论之类的玩意坑爹2333)

 1 #include<bits/stdc++.h>
 2 #define N 150005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 using namespace std;
 6 inline int ra()
 7 {
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
10     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
11     return x*f;
12 }
13 int head[N],HEAD[N],a[N],fa[N],belong[N];
14 int n,m,cnt,tot,CNT,B,ans;
15 struct node{
16     int size,a[505];
17     void insert(int x){a[++size]=x;}
18     void change(int x, int y)
19     {
20         int t=lower_bound(a+1,a+size+1,x)-a;
21         a[t]=y;
22         sort(a+1,a+size+1);
23     }
24     int query(int x)
25     {
26         int t=upper_bound(a+1,a+size+1,x)-a;
27         return size-t+1;
28     }
29 }block[10005];
30 struct data{int to,next;}e[N],E[N];
31 void INSERT(int x, int y){E[++CNT].to=y; E[CNT].next=HEAD[x]; HEAD[x]=CNT;}
32 void insert(int x, int y){e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt;}
33 void dfs(int x)
34 {
35     if (block[belong[fa[x]]].size>=B) 
36         belong[x]=++tot,block[tot].insert(a[x]),INSERT(belong[fa[x]],tot);
37     else belong[x]=belong[fa[x]],block[belong[x]].insert(a[x]);
38     for (int i=head[x];i;i=e[i].next)
39     {
40         if (e[i].to==fa[x]) continue;
41         fa[e[i].to]=x; dfs(e[i].to);
42     }
43 }
44 void dfs1(int x, int y)
45 {
46     ans+=block[x].query(y);
47     for (int i=HEAD[x];i;i=E[i].next)
48         dfs1(E[i].to,y);
49 }
50 void query(int x, int y)
51 {
52     if (a[x]>y) ans++;
53     for (int i=head[x];i;i=e[i].next)
54     {
55         if (e[i].to==fa[x]) continue;
56         if (belong[x]==belong[e[i].to]) query(e[i].to,y);
57         else dfs1(belong[e[i].to],y);
58     }
59 }
60 int main()
61 {
62     n=ra(); B=sqrt(n);
63     for (int i=1; i<n; i++)
64     {
65         int x=ra(),y=ra();
66         insert(x,y); insert(y,x);
67     }
68     for (int i=1; i<=n; i++) a[i]=ra(); 
69     dfs(1);
70     for (int i=0; i<=tot; i++)
71         sort(block[i].a+1,block[i].a+block[i].size+1);
72     m=ra();
73     while (m--)
74     {
75         int opt=ra(),x=ra()^ans,y=ra()^ans;
76         if (opt==0)
77         {
78             ans=0; 
79             query(x,y); printf("%d\n",ans);
80         }
81         if (opt==1)
82         {
83             block[belong[x]].change(a[x],y);
84             a[x]=y;
85         }
86         if (opt==2)
87         {
88             a[++n]=y; fa[n]=x; insert(x,n);
89             if (block[belong[x]].size==B)
90                 belong[n]=++tot,block[tot].insert(y),INSERT(belong[x],tot);
91             else {
92                 belong[n]=belong[x]; block[belong[n]].insert(y);
93                 sort(block[belong[x]].a+1,block[belong[x]].a+block[belong[x]].size+1);
94             }
95         }
96     }
97     return 0;
98 }

 

posted @ 2017-02-23 21:00  ws_ccd  阅读(160)  评论(0编辑  收藏  举报