poj 2777
Posted tmortred
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2777相关的知识,希望对你有一定的参考价值。
调了一天,囧!第一次写 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的主要内容,如果未能解决你的问题,请参考以下文章