codevs 1191 樹軸染色 線段樹區間定值,求和

codevs 1191 樹軸染色

Time Limit: 1 Sec  Memory Limit: 256 MBide

題目鏈接

http://www.codevs.cn/problem/1191/

Description

在一條數軸上有N個點,分別是1~N。一開始全部的點都被染成黑色。接着
咱們進行M次操做,第i次操做將[Li,Ri]這些點染成白色。請輸出每一個操做執行後
剩餘黑色點的個數。

Input

輸入一行爲N和M。下面M行每行兩個數Li、Ri

Output


輸出M行,爲每次操做後剩餘黑色點的個數。

Sample Input

10 3
3 3
5 7
2 8

Sample Output

9
6
3

HINT

數據限制
對30%的數據有1<=N<=2000,1<=M<=2000
對100%數據有1<=Li<=Ri<=N<=200000,1<=M<=200000

題意spa

題解:code

區間更新,把黑色當成1,白色當成0,而後搞一搞就行了

代碼:ip

 

#include <stdio.h>
#include <string.h>

const int MAXN = 400000;
int sum[MAXN<<2];
int lazy[MAXN<<2];

void pushup(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void pushdown(int rt, int x)
{
    if(lazy[rt] != -1) {
        lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
        sum[rt<<1] = (x-(x>>1))*lazy[rt];///!!!
        sum[rt<<1|1] = (x>>1)*lazy[rt];///!!!
        lazy[rt] = -1;
    }
}

void creat(int l, int r, int rt)
{
    lazy[rt] = -1, sum[rt] = 0;
    if(l == r) return;
    int mid = (l+r)>>1;
    creat(l, mid, rt<<1);
    creat(mid+1, r, rt<<1|1);
    pushup(rt);
}

void modify(int l, int r, int x, int L, int R, int rt)
{
    if(l <= L && r >= R) {
        lazy[rt] = x;
        sum[rt] = x*(R-L+1);///!!!
        return;
    }
    pushdown(rt, R-L+1);///!!!
    int mid = (L+R)>>1;
    if(l <= mid) modify(l, r, x, L, mid, rt<<1);
    if(r > mid) modify(l, r, x, mid+1, R, rt<<1|1);
    pushup(rt);
}

int main()
{
    int i, j, k = 0;
    int n, T, q;
    int x, y, w;
    //while(scanf("%d", &T) != EOF)
    //while(T--)
    //{
        scanf("%d %d", &n, &q);
        creat(1, n, 1);

        while(q--) {
            scanf("%d %d", &x, &y);
            modify(x, y, 1, 1, n, 1);
            printf("%d\n",n-sum[1]);
        }

        //printf("Case %d: The total value of the hook is %d.\n", ++k, sum[1]);
    //}
    return 0;
}
相關文章
相關標籤/搜索