hdu 5023 线段树延迟更新+状态压缩

/*
线段树延迟更新+状态压缩
*/
#include<stdio.h>
#define N  1100000
struct node {
 int x,y,yanchi,sum;
}a[N*4];
int lower[31];
void build(int t,int x,int y) {
a[t].x=x;
a[t].y=y;
a[t].yanchi=0;
if(x==y){
     a[t].sum=lower[1];
        return ;
}
int temp=t<<1;
int mid=(x+y)/2;
build(temp,x,mid);
build(temp+1,mid+1,y);
a[t].sum=a[temp].sum|a[temp+1].sum;
}
void inset(int t,int x,int y,int c) {
   //printf("%d %d %d
",a[t].yanchi,a[t].x,a[t].y);
   if(a[t].x==x&&a[t].y==y) {
    a[t].yanchi=lower[c];
    a[t].sum=lower[c];//sum和yanchi都需要改变
  //  printf("%d %d %d
",x,y,lower[c]);
    return ;
   }
   int temp=t*2;
   int mid=(a[t].x+a[t].y)/2;
   if(a[t].yanchi) {//如果区间a[t].x--a[t].y被操作过就赋给子区间
   a[temp].yanchi=a[t].yanchi;
   a[temp].sum=a[t].sum;
   a[temp+1].yanchi=a[t].yanchi;
   a[temp+1].sum=a[t].sum;
   a[t].yanchi=0;
   }
   if(x>mid)inset(temp+1,x,y,c);
   else
    if(y<=mid)inset(temp,x,y,c);
   else {
    inset(temp,x,mid,c);
    inset(temp+1,mid+1,y,c);
   }
  a[t].sum=a[temp].sum|a[temp+1].sum;
   return ;
}
int qury(int t,int x,int y) {
  //   printf("%d %d %d
",a[t].yanchi,a[t].x,a[t].y);
  if(a[t].x==x&&a[t].y==y) {
  // printf("%d
",a[t].sum);
    return a[t].sum;
  }
  int temp=t*2;
  int mid=(a[t].x+a[t].y)/2;
   if(a[t].yanchi) {//如果区间a[t].x--a[t].y被操作过就赋给子区间
   a[temp].yanchi=a[t].yanchi;
   a[temp].sum=a[t].sum;
   a[temp+1].yanchi=a[t].yanchi;
   a[temp+1].sum=a[t].sum;
   a[t].yanchi=0;
   }
  if(x>mid)
    return qury(temp+1,x,y);
  else if(y<=mid)
    return qury(temp,x,y);
  else
    return qury(temp,x,mid)|qury(temp+1,mid+1,y);
  a[t].sum=a[temp+1].sum|a[temp].sum;//更新
}
int main() {
  int n,m,i,j,k,ans;
  char s[10];
  lower[0]=1;
  for(i=1;i<=30;i++)
    lower[i]=lower[i-1]*2;
  while(scanf("%d%d",&n,&m),n||m) {
     build(1,1,n);
    while(m--) {
        scanf("%s",s);
        if(s[0]=='P') {
        scanf("%d%d%d",&i,&j,&k);
         inset(1,i,j,k-1);
        }
        if(s[0]=='Q') {
            scanf("%d%d",&i,&j);
            ans=qury(1,i,j);
         //  printf("%d
",ans);
             k=0;
             for(i=0;i<30;i++)
             if(ans&lower[i]) {
                if(k)
                    printf(" ");
                printf("%d",i+1);
                k=1;
             } printf("
");
        }
    }

  }
return 0;
}

原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410616.html