Tree line (line staining)

1191 stained axes

Title Description Description

The shaft has a number of N points, namely 1 ~ N. The beginning of all the points have been dyed black. Then
we M operations, the i th operation [Li, Ri] points colored white. Please perform each operation and outputs the
number of remaining black dots.

Enter a description Input Description

A behavioral input N and M. The following two lines each number M Li, Ri

Output Description Output Description

M output lines, the number of black dots remaining after each operation of the.

Sample input Sample Input

10 3
3 3
5 7
2 8

Sample Output Sample Output

9
6
3

Data range and prompt Data Size & Hint

Data limits
of 30% of the data have 1 <= N <= 2000,1 < = M <= 2000
to 100% with a data 1 <= Li <= Ri < = N <= 200000,1 <= M <= 200000

 

This question is of value and can not be accumulated lazy because there is a marker color, instead of the summation;

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#define rep(i , n) for(int i = 0 ; i < (n) ; i++)
using namespace std;
const int N = 1100009 ;
int ans = 0 , flag = 1;
int a[1000009] , b[1000009];

struct Node{
    int  l , r , val , lazy_tag ;
}tree[N * 4];

void build(int l , int r , int root)
{
    tree[root].l = l , tree[root].r = r ;
    tree[root].val = 0 , tree[root].lazy_tag = 0 ;
    if(l == r)
        return ;
    int mid = (l + r) >> 1 ;
    build(l , mid , root*2);
    build(mid + 1 , r , root*2+1);
}

void pushdown(int root)
{
    tree[root*2].lazy_tag = tree[root].lazy_tag;
    tree[root*2+1].lazy_tag = tree[root].lazy_tag ;

    tree[root * 2].val = (tree[root*2].r - tree[root*2].l+1)*tree[root].lazy_tag ;
    tree[root*2+1].val = (tree[root*2+1].r - tree[root*2+. 1 ] .L + . 1 ) * Tree [the root] .lazy_tag; 
    Tree [the root] .lazy_tag = 0 ; 
} 
void Update ( int L, int R & lt, int addval, int the root) 
{ 
    IF (Tree [the root] .L> = Tree && L [the root] .r <= R & lt) 
    { 
        Tree [the root] .lazy_tag = addval; // Note that this question can not be used = +, because 1 represents white, then white is dyed again. 
        Tree [the root] .val = (Tree [the root] .r - Tree [the root] .L + . 1 ) * addval;
         return ; 
    } 
    IF (Tree [the root] .lazy_tag) 
        pushdown (the root);
    int mid = (tree[root].l + tree[root].r) >> 1;
    if(l <= mid)
        update(l , r, addval , root*2);
    if(r > mid)
        update(l , r, addval , root*2+1);
    tree[root].val = tree[root*2].val + tree[root*2+1].val ;
}

void  query(int l,int r  ,int root )
{
    if(tree[root].l >= l && tree[root].r <= r)
    {
        ans = tree[root].val ;
        return ;
    }
    if(tree[root].lazy_tag)
        pushdown(root);
    int mid = (tree[root].r + tree[root].l) >> 1 ;
    if(l <= mid)
        query(l , r , root*2);
    if(r > mid)
        query(l , r , root*2+1);

}



int main()
{
    int n , q ;
    while(~scanf("%d%d" , &n , &q))
    {
        build(1 , n , 1);
        for(int i = 0 ; i < q ; i++)
        {
            int l , r  , value = 1;
            scanf("%d%d" , &l ,&r);
            update(l , r , value , 1) ;
            query(1 , n , 1);
            cout << n - ans << endl ;
            ans = 0 ;
        }
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11260163.html