CodeForces-915E. Physical Education Lessons線段樹動態開點解法及離散化解法

Physical Education Lessons

題目鏈接https://codeforces.com/contest/915/problem/E

time limit per test :1 second
memory limit per test: 256 megabytes

This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn’t attended a single lesson!

Since Alex doesn’t want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:

There are n days left before the end of the term (numbered from 1 to n), and initially all of them are working days. Then the university staff sequentially publishes q orders, one after another. Each order is characterised by three numbers l, r and k:

If k = 1, then all days from l to r (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days;
If k = 2, then all days from l to r (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.
Help Alex to determine the number of working days left after each order!

Input

The first line contains one integer n, and the second line — one integer q (1 ≤ n ≤ 10^9, 1 ≤ q ≤ 3·10^5) — the number of days left before the end of the term, and the number of orders, respectively.

Then q lines follow, i-th line containing three integers li, ri and ki representing i-th order (1 ≤ li ≤ ri ≤ n, 1 ≤ ki ≤ 2).

Output

Print q integers. i-th of them must be equal to the number of working days left until the end of the term after the first i orders are published.

Example

Input

4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2

Output

2
0
2
3
1
4


題目大意:給定1-n的一段已經填充的區間,每次給出 x y z 的詢問,z == 1 時將 x - y 區間全部空出來,否則全部填充,問最後一共有多少填充的區間。

之前寫了了一篇離散化的寫法,裏面有這題的離散化注意事項,比如說隔板即頭尾處理等:
博客鏈接https://blog.csdn.net/qq_43906000/article/details/101034876

看了之前寫的博客就會發現如果用離散化來寫的話會使這題變得很繁瑣,容易出錯,而且代碼量大,所以我們可以改用簡潔的動態開點。離散化我交了6發左右才過的,動態開點由於開小了RE了一發,第二發就A了。

動態開點的話就沒什麼可以將的了,只需要一個update函數和pushdown函數就沒了,需要注意的是pushdown的時候要判斷它的左右兒子是否存在,如果不存在的話就需要增加節點。爲了簡化代碼,我們最後的答案直接n-空出來的就好了,那麼我們就不需要預處理1-n中已經填充的區間了。

以下是AC代碼:

#include <bits/stdc++.h>
using namespace std;

#define lson l,mid,tree[rt].l
#define rson mid+1,r,tree[rt].r
#define ls tree[rt].l
#define rs tree[rt].r

const int mac=3e5+10;
const int inf=1e9+10;

struct node
{
    int l,r,sum,f;
}tree[mac*50];

int sz=1;

void pushdown(int l,int r,int rt)
{
    int mid=(l+r)>>1;
    if (l!=r){
        if (!tree[rt].l) tree[rt].l=++sz;//左兒子不存在,增加節點
        if (!tree[rt].r) tree[rt].r=++sz;
        if (tree[rt].f==2)
            tree[ls].sum=tree[rs].sum=0;
        else {
            tree[ls].sum=mid-l+1;
            tree[rs].sum=r-mid;
        }
        tree[ls].f=tree[rt].f;tree[rs].f=tree[rt].f;
    }
    tree[rt].f=0;
}

void update(int l,int r,int &rt,int L,int R,int val)
{
    if (!rt) rt=++sz;//虛點->實點
    if (l>=L && r<=R){
        if (val==2) tree[rt].sum=0;
        else tree[rt].sum=r-l+1;
        tree[rt].f=val;
        return;
    }
    if (tree[rt].f) pushdown(l,r,rt);
    int mid=(l+r)>>1;
    if (mid>=L) update(lson,L,R,val);
    if (mid<R) update(rson,L,R,val);
    tree[rt].sum=tree[ls].sum+tree[rs].sum;
}

int main()
{
    int n,q;
    scanf ("%d%d",&n,&q);
    int root=1;
    for (int i=1; i<=q; i++){
        int l,r,id;
        scanf ("%d%d%d",&l,&r,&id);
        update(1,n,root,l,r,id);
        printf ("%d\n",n-tree[1].sum);
    }
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章