HDU5709 : Claris Loves Painting

对于每个点维护两棵线段树$T1[x],T2[x]$:

$T1[x]$维护$x$子树内,深度在$[l,r]$内的点数,同种颜色有多个的话,保留深度最小的那个。

$T2[x]$维护$x$子树内每种颜色的最小深度。

从底向上合并线段树,先合并$T1$,然后合并$T2$的时候,发现有重复点,那么在$T1$里删去深度大的那个,查询直接在$T1$里区间求和即可。

时间复杂度$O((n+m)\log n)$。

#include<cstdio>
const int N=100010,M=10000000;
int Case,n,m,i,x,y,ans,a[N],f[N],d[N],T1[N],T2[N],tot,l[M],r[M],v[M];
int ins(int x,int a,int b,int c,int p){
  int y=++tot;v[y]=v[x]+p;
  if(a==b)return y;
  int mid=(a+b)>>1;
  if(c<=mid)l[y]=ins(l[x],a,mid,c,p),r[y]=r[x];
  else l[y]=l[x],r[y]=ins(r[x],mid+1,b,c,p);
  return y;
}
int merge1(int x,int y,int a,int b){
  if(!x||!y)return x+y;
  int z=++tot;
  v[z]=v[x]+v[y];
  if(a==b)return z;
  int mid=(a+b)>>1;
  l[z]=merge1(l[x],l[y],a,mid);
  r[z]=merge1(r[x],r[y],mid+1,b);
  return z;
}
int merge2(int x,int y,int a,int b,int p){
  if(!x||!y)return x+y;
  int z=++tot;
  if(a==b){
    if(v[x]<v[y])v[z]=v[x],T1[p]=ins(T1[p],1,n,v[y],-1);
    else v[z]=v[y],T1[p]=ins(T1[p],1,n,v[x],-1);
    return z;
  }
  int mid=(a+b)>>1;
  l[z]=merge2(l[x],l[y],a,mid,p);
  r[z]=merge2(r[x],r[y],mid+1,b,p);
  return z;
}
int ask(int x,int a,int b,int d){
  if(b<=d)return v[x];
  int mid=(a+b)>>1,t=ask(l[x],a,mid,d);
  if(d>mid)t+=ask(r[x],mid+1,b,d);
  return t;
}
int main(){
  scanf("%d",&Case);
  while(Case--){
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=2;i<=n;i++)scanf("%d",&f[i]);
    for(i=1;i<=n;i++)d[i]=d[f[i]]+1;
    for(i=1;i<=n;i++){
      T1[i]=ins(0,1,n,d[i],1);
      T2[i]=ins(0,1,n,a[i],d[i]);
    }
    for(i=n;i>1;i--){
      T1[f[i]]=merge1(T1[f[i]],T1[i],1,n);
      T2[f[i]]=merge2(T2[f[i]],T2[i],1,n,f[i]);
    }
    while(m--){
      scanf("%d%d",&x,&y);x^=ans,y^=ans;
      y+=d[x];
      if(y>n)y=n;
      printf("%d\n",ans=ask(T1[x],1,n,y));
    }
    ans=tot=0;
  }
  return 0;
}

  

时间: 2024-10-18 11:48:14

HDU5709 : Claris Loves Painting的相关文章

&quot;巴卡斯杯&quot; 中国大学生程序设计竞赛 - 女生专场(重现)解题思路

此文章可以使用目录功能哟↑(点击上方[+]) 经过这么一次女生赛,告诉我们千万不要小瞧女生,不然会死得很惨,orz... 链接→"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现)  Problem 1001 Solving Order Accept: 0    Submit: 0 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit : 32768/32768 K (Java/Others)  Problem Descri

HDU - 4810 Wall Painting(组合数学)

Description Ms.Fang loves painting very much. She paints GFW(Great Funny Wall) every day. Every day before painting, she produces a wonderful color of pigments by mixing water and some bags of pigments. On the K-th day, she will select K specific bag

hdu-5661 Claris and XOR(贪心)

题目链接: Claris and XOR Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Claris loves bitwise operations very much, especially XOR, because it has many beautiful features. He gets four positive int

Claris and XOR(模拟)

Claris and XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 332    Accepted Submission(s): 135 Problem Description Claris loves bitwise operations very much, especially XOR, because it has ma

Cf 444C DZY Loves Colors(线段树)

DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of thei-th unit of the ribbon is i at first. It is colorful enough, but w

CodeForces 445E DZY Loves Colors

DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 445E64-bit integer IO format: %I64d      Java class name: (Any) DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a

Cf 444C DZY Loves Colors(段树)

DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of thei-th unit of the ribbon is i at first. It is colorful enough, but w

codeforces - 444c DZY Loves Colors(线段树+染色)

C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they

HDU 4365——Palindrome graph——————【规律+快速幂】

Palindrome graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1727    Accepted Submission(s): 525 Problem Description In addition fond of programing, Jack also loves painting. He likes to dra