• bzoj 3720 Gty的妹子树 树分块?瞎搞


    Gty的妹子树

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 2149  Solved: 781
    [Submit][Status][Discuss]

    Description

    我曾在弦歌之中听过你,

    檀板声碎,半出折子戏。

    舞榭歌台被风吹去,

    岁月深处尚有余音一缕……


    Gty神(xian)犇(chong)从来不缺妹子……

    他来到了一棵妹子树下,发现每个妹子有一个美丽度……

    由于Gty很哲♂学,他只对美丽度大于某个值的妹子感兴趣。

    他想知道某个子树中美丽度大于k的妹子个数。

    某个妹子的美丽度可能发生变化……

    树上可能会出现一只新的妹子……


    维护一棵初始有n个节点的有根树(根节点为1),树上节点编号为1-n,每个点有一个权值wi。

    支持以下操作:

    0 u x          询问以u为根的子树中,严格大于x的值的个数。(u^=lastans,x^=lastans)

    1 u x          把u节点的权值改成x。(u^=lastans,x^=lastans)

    2 u x          添加一个编号为"当前树中节点数+1"的节点,其父节点为u,其权值为x。(u^=lastans,x^=lastans)

    最开始时lastans=0。

    Input

    输入第一行包括一个正整数n(1<=n<=30000),代表树上的初始节点数。

    接下来n-1行,每行2个整数u,v,为树上的一条无向边。

    任何时刻,树上的任何权值大于等于0,且两两不同。

    接下来1行,包括n个整数wi,表示初始时每个节点的权值。

    接下来1行,包括1个整数m(1<=m<=30000),表示操作总数。

    接下来m行,每行包括三个整数 op,u,v:

    op,u,v的含义见题目描述。

    保证题目涉及的所有数在int内。

    Output

    对每个op=0,输出一行,包括一个整数,意义见题目描述。

    Sample Input

    2
    1 2
    10 20
    1
    0 1 5

    Sample Output

    2

    HINT

     2017.9.28新加数据一组by GXZlegend,未重测

    Source

    传说中的树分块,但是会被暴力卡,我貌似没找出什么方法,

    和根一起一起到达根号n就就分块,这个没什么用,会被菊花图卡掉的。

      1 #include<cstring>
      2 #include<cstdio>
      3 #include<iostream>
      4 #include<algorithm>
      5 #include<cmath>
      6 
      7 #define N 60007
      8 using namespace std;
      9 inline int read()
     10 {
     11     int x=0,f=1;char ch=getchar();
     12     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     13     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
     14     return x*f;
     15 }
     16 
     17 int n,m,block,ans,tot;
     18 int a[N],fa[N],bl[N];
     19 int cnt,hed[N],nxt[N<<1],rea[N<<1];
     20 int Cnt,Hed[N],Nxt[N<<1],Rea[N<<1];
     21 
     22 struct Node
     23 {
     24     int a[207],siz;
     25     void insert(int x){a[++siz]=x;}
     26     void change(int x,int y)
     27     {
     28         int t=upper_bound(a+1,a+siz+1,x)-a;t--;
     29         a[t]=y;
     30         sort(a+1,a+siz+1);
     31     }
     32     int query(int x)
     33     {
     34         int t=upper_bound(a+1,a+siz+1,x)-a;
     35         return siz-t+1;
     36     }
     37 }blo[N];
     38 inline void add(int u,int v)
     39 {
     40     nxt[++cnt]=hed[u];
     41     hed[u]=cnt;
     42     rea[cnt]=v;
     43 }
     44 inline void add_two_way(int u,int v)
     45 {
     46     add(u,v);
     47     add(v,u);
     48 }
     49 inline void ins(int u,int v)
     50 {
     51     Nxt[++Cnt]=Hed[u];
     52     Hed[u]=Cnt;
     53     Rea[Cnt]=v;
     54 }
     55 void dfs(int u)
     56 {
     57     if (blo[bl[fa[u]]].siz==block) 
     58         blo[bl[u]=++tot].insert(a[u]),ins(bl[fa[u]],tot);
     59     else blo[bl[u]=bl[fa[u]]].insert(a[u]);
     60     for (int i=hed[u];i!=-1;i=nxt[i])
     61     {
     62         int v=rea[i];
     63         if (v==fa[u]) continue;
     64         fa[v]=u,dfs(v);
     65     }
     66 }
     67 void block_dfs(int u,int z)
     68 {
     69     ans+=blo[u].query(z);
     70     for (int i=Hed[u];i!=-1;i=Nxt[i])
     71     {
     72         int v=Rea[i];
     73         block_dfs(v,z);
     74     }
     75 }
     76 void solve(int u,int z)
     77 {
     78     if (a[u]>z) ans++;
     79     for (int i=hed[u];i!=-1;i=nxt[i])
     80     {
     81         int v=rea[i];
     82         if (v==fa[u]) continue;
     83         if (bl[v]==bl[u]) solve(v,z);
     84         else block_dfs(bl[v],z);
     85     }
     86 }
     87 int main()
     88 {
     89     memset(hed,-1,sizeof(hed));
     90     memset(Hed,-1,sizeof(Hed));
     91     n=read(),block=sqrt(n);
     92     for (int i=1;i<n;i++)
     93         add_two_way(read(),read());
     94     for (int i=1;i<=n;i++) a[i]=read();
     95     dfs(1);
     96     for (int i=1;i<=tot;i++)
     97         sort(blo[i].a+1,blo[i].a+blo[i].siz+1);
     98     
     99     m=read();
    100     for (int i=1;i<=m;i++)
    101     {
    102         int opt=read(),x=read(),y=read();
    103         x^=ans,y^=ans;
    104         if (opt==0)
    105         {
    106             ans=0;
    107             solve(x,y);
    108             printf("%d
    ",ans);
    109         }
    110         else if (opt==1)
    111         {
    112             blo[bl[x]].change(a[x],y);
    113             a[x]=y;
    114         }
    115         else
    116         {
    117             a[++n]=y,add_two_way(x,n),fa[n]=x;
    118             if (blo[bl[x]].siz==block)
    119                 blo[bl[n]=++tot].insert(y),ins(bl[x],tot);
    120             else
    121             {
    122                 blo[bl[n]=bl[x]].insert(y);
    123                 sort(blo[bl[n]].a+1,blo[bl[n]].a+blo[bl[n]].siz+1);
    124             }
    125         }
    126     }
    127 }
  • 相关阅读:
    公司技术的确定
    数据结构
    如何利用百度ocr实现验证码自动识别
    redis 主从复制
    redis哨兵机制
    redis集群搭建
    webmagic自定义存储(mysql、redis存储)
    redis安装与使用
    maven插件mybatis-generator自动生成代码
    python 中的“集合”(list、tuple、set、dict)
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8659159.html
Copyright © 2020-2023  润新知