【线段树区间修改】fzu2105Digits Count

/*
题意:
给出数组A,有以下几个操作:
1: AND(opn, L, R):把区间[L, R]中的元素A[i]改为A[i] & opn;;;;;;
2: OR(opn, L, R) :把区间[L, R]中的元素A[i]改为A[i] | opn;;;;;;;
3: XOR(opn, L, R):把区间[L, R]中的元素A[i]改为A[i] ^ opn;;;;;;;
4: SUM(L, R)     :对区间[L, R]中的元素求和;;;;
--------------------------------------------------------------------------------
线段树区间修改的题目:
--------------------------------------------------------------------------------
void build(int l, int r, int o)递归建树
{
    tree[o].l = l;结点的左端点
    tree[o].r = r;结点的右端点
    tree[o].num = -1;标记该区间的数是否相同
    if(l == r)叶子结点
    {
        scanf("%d",&tree[o].num);
        return ;
    }
    int M = (l + r)/2;
    build(l, M, lc);
    build(M+1, r, rc);
    if(tree[lc].num != -1 && tree[lc].num == tree[rc].num)向上更新
        tree[o].num = tree[lc].num;
}
------------------------------------------------------------------------------------
1,2,3操作时数组的更新:
int opreate(int op, int opn, int num)对应的操作
{
    if(op == 1) return num & opn;------------AND
    if(op == 2) return num | opn;------------OR
    if(op == 3) return num ^ opn;------------XOR
}

void update(int l, int r, int o, int opn, int op)
{
    if(tree[o].l == l && tree[o].r == r && tree[o].num >= 0)找到该区间时,跟新区间内的元素,,即修改标记
    {
        tree[o].num = opreate(op, opn, tree[o].num);
        return ;
    }
    if(tree[o].num >= 0)标记传递,起作用是把num的值向下传递即:pushdown函数
    {
        tree[lc].num = tree[rc].num = tree[o].num;
        tree[o].num = -1;清除本节点的标记
    }
    int mid = (tree[o].l + tree[o].r)/2;
    if(r <= mid)------------------------------------------------------------?
        update(l, r, lc, opn, op);更新左子树
    else if(l > mid)
        update(l, r, rc, opn, op);更新右子树
    else否则都要更新
    {
        update(l, mid, lc, opn, op);
        update(mid+1, r, rc, opn, op);
    }------------------------------------------------------------------------?
    if(tree[lc].num != -1 && tree[lc].num == tree[rc].num)往上更新
        tree[o].num = tree[lc].num;
}
----------------------------------------------------------------------------------------
LL query(int l, int r, int o)
{
    if(tree[o].l == l && tree[o].r == r && tree[o].num >= 0)
        return tree[o].num*(tree[o].r - tree[o].l+1);
    if(tree[o].num >= 0)
    {
        tree[lc].num = tree[rc].num = tree[o].num;
        tree[o].num = -1;
    }
    int mid = (tree[o].l + tree[o].r)/2;
    if(r <= mid)
        return query(l, r, lc);
    else if( l > mid)
        return query(l, r, rc);
    else
        return query(l, mid, lc)+query(mid+1, r, rc);
     if(tree[lc].num != -1 && tree[lc].num == tree[rc].num)
        tree[o].num = tree[lc].num;
}
--------------------------------------------------------------------------------------------
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define lc o*2
#define rc o*2+1

using namespace std;

const int MAXN = 1000010;
typedef long long LL;

int n, m;
int a[MAXN];

struct node{
    int l, r;
    int num;
}tree[MAXN*4];

void build(int l, int r, int o)
{
    tree[o].l = l;
    tree[o].r = r;
    tree[o].num = -1;
    if(l == r)
    {
        scanf("%d",&tree[o].num);
        return ;
    }
    int M = (l + r)/2;
    build(l, M, lc);
    build(M+1, r, rc);
    if(tree[lc].num != -1 && tree[lc].num == tree[rc].num)
        tree[o].num = tree[lc].num;
}

int opreate(int op, int opn, int num)
{
    if(op == 1) return num & opn;
    if(op == 2) return num | opn;
    if(op == 3) return num ^ opn;
}

void update(int l, int r, int o, int opn, int op)
{
    if(tree[o].l == l && tree[o].r == r && tree[o].num >= 0)
    {
        tree[o].num = opreate(op, opn, tree[o].num);
        return ;
    }
    if(tree[o].num >= 0)
    {
        tree[lc].num = tree[rc].num = tree[o].num;
        tree[o].num = -1;
    }
    int mid = (tree[o].l + tree[o].r)/2;
    if(r <= mid)
        update(l, r, lc, opn, op);
    else if(l > mid)
        update(l, r, rc, opn, op);
    else
    {
        update(l, mid, lc, opn, op);
        update(mid+1, r, rc, opn, op);
    }
    if(tree[lc].num != -1 && tree[lc].num == tree[rc].num)
        tree[o].num = tree[lc].num;
}

LL query(int l, int r, int o)
{
    if(tree[o].l == l && tree[o].r == r && tree[o].num >= 0)
        return tree[o].num*(tree[o].r - tree[o].l+1);
    if(tree[o].num >= 0)
    {
        tree[lc].num = tree[rc].num = tree[o].num;
        tree[o].num = -1;
    }
    int mid = (tree[o].l + tree[o].r)/2;
    if(r <= mid)
        return query(l, r, lc);
    else if( l > mid)
        return query(l, r, rc);
    else
        return query(l, mid, lc)+query(mid+1, r, rc);
     if(tree[lc].num != -1 && tree[lc].num == tree[rc].num)
        tree[o].num = tree[lc].num;
}

int main()
{
    freopen("input.txt","r",stdin);
    char op[5];
    int opn, L, R;
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d", &n,&m);
        build(1, n, 1);
        while(m--)
        {
            scanf("%s",op);
            getchar();
            if(op[0] == 'S')
            {
                scanf("%d%d",&L, &R);
                printf("%lld\n",query(L+1, R+1, 1));
            }
            else
            {
                scanf("%d%d%d",&opn, &L, &R);
                if(op[0] == 'A')
                    update(L+1, R+1, 1, opn, 1);
                if(op[0] == 'O')
                    update(L+1, R+1, 1, opn, 2);
                if(op[0] == 'X')
                    update(L+1, R+1, 1, opn, 3);
            }
        }
    }
    return 0;
}

---------------------------------------------------------------------

wuwuwuwuuuuwuuwuwuuwuwuwuwuuw....................................

【线段树区间修改】fzu2105Digits Count,布布扣,bubuko.com

时间: 2024-08-05 15:01:25

【线段树区间修改】fzu2105Digits Count的相关文章

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

hdu-5023 A Corrupt Mayor&#39;s Performance Art (线段树区间修改)

今天集训队打比赛的一道题,很明显是个线段树,我们队照着lrj蓝书敲了一通,机智的将修改值和加和改成了位运算:|= 但是好像哪里出了点小问题,就是不对,赛后又水了一遍,竟然过了...发现还是lrj的书好啊,市面上的模板一点也不好用,连区间修改都没有 . 等集训完了要静心好好系统的学习一下线段树 . 多看多刷lrj的书 . 细节参见代码: #include<bits/stdc++.h> using namespace std; const int maxn = 1000000 + 5; int n

Wikilo 1191线段树区间修改单点查询

这题也算比较容易的了. 如果哪个区间已经没有黑色的话,就不用update了,就是因为这个原因WA了2发,唉-- #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #incl

线段树区间修改模板

本来打算把大白书第三章一口气攻下来的,但是这个线段树也是卡了好久. 不敢过题太快,怕自己走马观花到头来结果什么都不会. 可也不能再拖了,在做题中也许有更多的体会. 模板一:1 L R v 表示区间[L, R]所有元素都加上v2 L R   表示查询区间[L, R]的sum, min, maxsumv[o]的定义为:如果只执行节点o及其子孙节点的中的add操作,节点o对应区间中所有数之和 1 //线段树区间修改 2 //1 L R v 表示区间[L, R]所有元素都加上v 3 //2 L R 表示

HDU - 1698 Just a Hook (线段树区间修改)

Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook.

线段树区间修改 P3372 【模板】线段树 1

题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值. 接下来M行每行包含3或4个整数,表示一个操作,具体如下: 操作1: 格式:1 x y k 含义:将区间[x,y]内每个数加上k 操作2: 格式:2 x y 含义:输出区间[x,y]内每个数的和 输出格式: 输出包含若干行整

UVA11992 - Fast Matrix Operations ( 线段树 + 区间修改 + 好题 )

UVA11992 - Fast Matrix Operations ( 线段树 + 区间修改 + 好题 ) 这是大白书上的例题,一直放着没有去A掉,这是一道线段树区间修改的好题. 线段树中需要维护三个域 ,max, min, sum,也就是区间最大值,最小值,区间和 题目大意: r 行 c 列 的全0矩阵,支持三个操作 1 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素增加v 2 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素设为v 3 x1 y1

hdu 4902 Nice boat(线段树区间修改,输出最终序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his peopl

杭电 1698 Just a Hook(线段树区间修改)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17322    Accepted Submission(s): 8640 Problem Description In the game of DotA, Pudge's