1. 程式人生 > >poj 2777

poj 2777

else if getc struct urn ini vector getchar scan its

調了一天,囧!第一次寫 lazy 更新線段樹,結果在查詢和 add 時沒有考慮到我用了區間 lazy 更新。後面在增加 lazy 更新處理代碼時,將 bitmap 作為 color 傳進去。導致 add 函數邏輯不正確。
本題還有一個坑,要考慮輸入參數的大小。L 參數可能比 R 參數大!!

# include <iostream>
# include <algorithm>
# include <memory.h>
# include <vector>
# include <assert.h>

using namespace std;

const int maxn = 100010;

struct Num {
  int val;
  int pos;
  bool operator <(const Num &a)const {
    return val < a.val;
  }
}num[maxn];

int n, nT, nQ; //L, T, Q (O is always worse)


int find_index(int num) {
  int sum = 0;
  while (num > 1) {
    sum += 1;
    num = num >> 1;
  }
  return sum;
}


struct Segment {
  // 因為不需要修改
  struct _nod{
    int bitmap, lazy;
    int l, r;
  } TreeNode[maxn << 2]; //bug 之前開到 [maxn <<1] 直接報 runtime error 
  
  void init() {
    memset(TreeNode, 0, sizeof(TreeNode));
  }

  void build(int index, int l, int r) {
    int mid = (l+r) >> 1;
    TreeNode[index].l = l; TreeNode[index].r = r; TreeNode[index].bitmap = 1 << 1; TreeNode[index].lazy = 0;
    if (l == r)
      return;
    build(index <<1, l, mid); build(index << 1|1, mid+1, r);
  }


  void remove_lazy(int index) {
    int mid = (TreeNode[index].l + TreeNode[index].r) >> 1;

    if (TreeNode[index].lazy == 1) {
      add(index<<1, TreeNode[index].l, mid, find_index(TreeNode[index].bitmap)); // 不能將 TreeNode[index].bitmap 傳進去啊 
      add(index<<1|1, mid+1, TreeNode[index].r, find_index(TreeNode[index].bitmap));      
      TreeNode[index].lazy = 0;
    }
    
    /*
    if (TreeNode[index].lazy == 1) {
      TreeNode[index<<1].bitmap = TreeNode[index].bitmap; TreeNode[index<<1|1].bitmap = TreeNode[index].bitmap;
      TreeNode[index<<1].lazy = 1;  TreeNode[index<<1|1].lazy = 1;
      TreeNode[index].lazy = 0;
    }
    */
  }

  
  void add(int index, int cl, int cr, int color) {
    
    int mid = (TreeNode[index].l + TreeNode[index].r) >> 1;
    
    if (TreeNode[index].bitmap == (1<<color)) {
      return;
    }
    
    if (TreeNode[index].l == cl and cr == TreeNode[index].r ) {
      TreeNode[index].bitmap = 1 << color;
      TreeNode[index].lazy = cl == cr? 0: 1 ;
      return;
    }

    remove_lazy(index);
    
    if (mid >= cr) {
      add(index <<1, cl, cr, color);
    }
    
    else if (cl > mid) {
      add(index <<1|1, cl, cr, color);
    }
    else {
      add(index <<1, cl, mid, color);
      add(index <<1|1, mid+1, cr, color);
    }
    
    TreeNode[index].bitmap = TreeNode[index<<1].bitmap | TreeNode[index<<1|1].bitmap;
  }
  
  int query(int index, int cl, int cr) {
    int mid=0;
    /*
    if (TreeNode[index].lazy)
      return TreeNode[index].bitmap;
    */
    
    if (TreeNode[index].l == cl and cr == TreeNode[index].r)
       return TreeNode[index].bitmap;
    
    mid = (TreeNode[index].l + TreeNode[index].r) >> 1;

    remove_lazy(index);
    
    if (mid >= cr)
      return query(index<<1, cl, cr);
    else if (cl> mid)
      return query(index<<1|1, cl, cr);
    else
      return query(index<<1, cl, mid) | query(index<<1|1, mid+1, cr);
  }
    
}T;


int get_bits(int x) {
  int sums = 0;
   while (x >0){
     sums += x%2;
     x = x/2;
   }
   return sums;
}


int main(int argc, char* argv[]){

  int l, r, color;
  char ch = ‘Z‘;
  
  scanf("%d%d%d", &n, &nT, &nQ);
    
  T.init();
  T.build(1, 1, n);
    
  while(nQ--) {
    getchar(); 
    scanf("%c", &ch);
    if (‘C‘ == ch) {
      scanf("%d%d%d", &l, &r, &color);
      if (l > r)
        swap(l, r);
      
      T.add(1, l, r, color);
    }
    if (‘P‘ == ch)  {
      scanf("%d%d", &l, &r);
      if (l > r)
        swap(l, r);
      printf("%d\n", get_bits(T.query(1, l, r))); 
    }
  }
  return 0;
}


poj 2777