codevs 1191 数轴染色 区间更新加延迟标记

题目描述 Description

在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。

输入描述 Input Description

输入一行为N和M。下面M行每行两个数Li、Ri

输出描述 Output Description

输出M行,为每次操作后剩余黑色点的个数。

样例输入 Sample Input

10 3
3 3
5 7
2 8

样例输出 Sample Output

9
6
3

数据范围及提示 Data Size & Hint

数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000

思路:把区间的值改成0,求区间和;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - ‘0‘ ;
    while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ )
        res = res * 10 + ( ch - ‘0‘ ) ;
    return res ;
}
struct is
{
    int l,r;
    int num;
    int lazy;
}tree[200010*3];
void build_tree(int l,int r,int pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    tree[pos].lazy=-1;
    if(l==r)
    {
        tree[pos].num=1;
        //scanf("%lld",&tree[pos].num);
        return;
    }
    int mid=(l+r)/2;
    build_tree(l,mid,pos*2);
    build_tree(mid+1,r,pos*2+1);
    tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;
}
void update(int l,int r,int change,int pos)
{
    if(tree[pos].l==l&&tree[pos].r==r)
    {
        tree[pos].lazy=change;
        tree[pos].num=0;
        return;
    }
    if(tree[pos].lazy==0)
    {
        tree[pos*2].num=(tree[pos*2].r+1-tree[pos*2].l)*tree[pos].lazy;
        tree[pos*2+1].num=(tree[pos*2+1].r+1-tree[pos*2+1].l)*tree[pos].lazy;
        tree[pos*2].lazy=tree[pos].lazy;
        tree[pos*2+1].lazy=tree[pos].lazy;
        tree[pos].lazy=-1;
    }
    int mid=(tree[pos].l+tree[pos].r)/2;
    if(r<=mid)
    update(l,r,change,pos*2);
    else if(l>mid)
    update(l,r,change,pos*2+1);
    else
    {
        update(l,mid,change,pos*2);
        update(mid+1,r,change,pos*2+1);
    }
    tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;
}
int query(int l,int r,int pos)
{
    //cout<<l<<" "<<r<<" "<<pos<<endl;
    if(tree[pos].l==l&&tree[pos].r==r)
    return tree[pos].num;
    if(tree[pos].lazy==0)
    {
        tree[pos*2].num=0;
        tree[pos*2+1].num=0;
        tree[pos*2].lazy=0;
        tree[pos*2+1].lazy=0;
        tree[pos].lazy=-1;
    }
    int mid=(tree[pos].l+tree[pos].r)/2;
    if(l>mid)
    return query(l,r,pos*2+1);
    else if(r<=mid)
    return query(l,r,pos*2);
    else
    return query(l,mid,pos*2)+query(mid+1,r,pos*2+1);
}
int main()
{
    int x,q,i,t;
    while(~scanf("%d",&x))
    {
        scanf("%d",&q);
        build_tree(1,x,1);
        while(q--)
        {
            int flag,change=0,l,r;
            scanf("%d%d",&l,&r);
            update(l,r,change,1);
            printf("%d\n",query(1,x,1));
        }
    }
    return 0;
}

时间: 2024-08-01 01:41:08

codevs 1191 数轴染色 区间更新加延迟标记的相关文章

codevs 1191 数轴染色

1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. 输入描述 Input Description 输入一行为N和M.下面M行每行两个数Li.Ri 输出描述 Output Description 输出M行,为每次操作后剩余黑色点的个数. 样例输

codevs 1191 线段树 区间更新(水)

题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. 输入描述 Input Description 输入一行为N和M.下面M行每行两个数Li.Ri 输出描述 Output Description 输出M行,为每次操作后剩余黑色点的个数. 样例输入 Sample Input 10 33 35 72 8 样例输出 Sample Output 963 数据

1191 数轴染色

1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. 输入描述 Input Description 输入一行为N和M.下面M行每行两个数Li.Ri 输出描述 Output Description 输出M行,为每次操作后剩余黑色点的个数. 样例输入 S

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

HDU 1890 Robotic Sort 伸展树的区间反转与延迟标记

延迟标记像极了线段树,不再多说. 区间反转在树伸展到位之后,也变成了简单的递归交换左右儿子. 愈发感觉到伸展树简直太漂亮了,伸展操作更是诱惑到不行 ,总之数据结构太有魅力了. 比较简单,就直接上模板了. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #in

hdu 1698 线段数的区间更新 以及延迟更新

先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; int r=stu[i].r; int mid=(l+r)/2;//二分咯 if(l==r&&r==x)//x为目标id 当左右节点相同的时候 就是找到这个数的时候 { stu[i].maxx=y; return; } if(l<=x&&x<=mid) updata

线段树 (区间修改 区间查询 延迟标记)

hdu 1698 Just a Hook 题意: 给你一链子,这天链子由金银铜三种钩子组成,每种钩子都有自己的价值,起初,这条钩子全部由铜钩子组成,给你两个数n(钩子的个数),Q(操作的个数)每次操作就是将给定区间里的数变成某种钩子,求这条链子的总价值. 分析: 线段树模版题,处理好延迟标记即可. 代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #in

poj 3468 A Simple Problem with Integers 线段树加延迟标记

A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of

codevs 1299 线段树 区间更新查询

1299 切水果 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和数据范围适当修改,避免数据包过大而浪费空间资源 输入描述 Input Description 第1行共包括2个正整数,分别为N,M. 接下来m行每行两个正整数L,R 输出描述