CodeForces-915E. Physical Education Lessons(離散化+線段樹)及離散化詳解

E. Physical Education Lessons

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

time limit per test               memory limit per test

1 second                            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 lr 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 liri 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 區間全部空出來,否則全部填充,問最後一共有多少填充的區間。

很顯然,n的範圍是1e9。。。只有3e5個區間,那麼很容易想到離散化。這裏離散化需要注意的是

1.隔板,將原來分開的兩個數中間插入一個數。比如對於數據

7
10
5 7 1
5 6 2
7 7 2
6 7 2
5 5 1
3 6 2
1 3 2
5 6 1
1 3 1
6 7 1

就是第三測試點,把所有的數據跳出來的話就是1 3 5 6 7。那麼我們如果將其離散化後就是1 2 3 4 5。但如果我們將5到6的區間刪空了的話,也就是說3-4沒了,我們得到的答案是區間1-2的值,也就是最終答案3。。。QAQ所以要加入隔板,對於3 5 這種數據加一個4的隔板就好了,但對於像54   1212121 這種數,我們就需要加上55 和 1212120 兩個隔板:

sort(b+1,b+1+cnt);
int p=b[1],tot=1;
len[1]=b[1];
for (int i=2; i<=cnt; i++) {
	if (b[i]!=p) {
		if(b[i]-b[i-1]==2) {//加一個隔板
			len[++tot]=p+1;
		} 
		else if (b[i]-b[i-1]>2) {//加兩個隔板
			len[++tot]=b[i-1]+1;
			len[++tot]=b[i]-1;
		}
		p=b[i];
		q[p]=++tot;
		len[tot]=b[i];
	}
}

2.注意頭和尾,它的區間中可能並沒有1,所以我們需要對頭減一作爲最小值,尾巴的話就是N了:

int mi=inf;
for (int i=1; i<=m; i++) {
	int l,r,id;
	in(l);in(r);in(id);
	a[i].l=l;a[i].r=r;a[i].id=id;
	b[++cnt]=l;b[++cnt]=r;
	mi=min(min(l,r),mi);
}
if (mi>1) b[++cnt]=mi-1;
b[++cnt]=n;

那麼接下來就是線段樹的操作了,我們的先初始化線段樹葉子結點全部爲它的區間大小(表示已經填充好了)然後往上加:

void build(int l,int r,int rt)
{
    f[rt]=-1;
    if (l==r) {
    	tree[rt]=len[r]-len[l-1];
    	return;
	}
    int mid=(l+r)>>1;
    build(lson);build(rson);
    tree[rt]=tree[ls]+tree[rs];
}

接下來就是和區間染色一樣的無腦更新了,答案我們直接輸出tree[1]就好了。注意離散化別用unordered_map或者map,會T。。。

以下是AC代碼:
 

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

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1

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

int tree[mac<<2],f[mac<<2];
int len[mac];//保留每個離散化數據原始的離1的距離
unordered_map<int,int>q;

void build(int l,int r,int rt)
{
    f[rt]=-1;
    if (l==r) {
    	tree[rt]=len[r]-len[l-1];
    	return;
	}
    int mid=(l+r)>>1;
    build(lson);build(rson);
    tree[rt]=tree[ls]+tree[rs];
}

void down(int l,int r,int rt)
{
    int mid=(l+r)>>1;
    tree[ls]=f[rt]*(len[mid]-len[l-1]);tree[rs]=f[rt]*(len[r]-len[mid]);
    f[ls]=f[rs]=f[rt];
    f[rt]=-1;
}

void update(int l,int r,int rt,int L,int R,int val)
{
    if (l>=L && r<=R){
        f[rt]=val;
        tree[rt]=val*(len[r]-len[l-1]);
        return;
    }
    if (f[rt]!=-1) down(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]=tree[ls]+tree[rs];
}

void in(int &x)
{
    int f=0;
    char ch=getchar();
    while (ch<'0' || ch>'9') ch=getchar();
    while (ch>='0' && ch<='9') f=(f<<3)+(f<<1)+ch-'0',ch=getchar();
    x=f;
}

struct node
{
    int l,r,id;
}a[mac];
int b[mac];

int main()
{
    int n,m;
    in(n);in(m);
    int cnt=0;
    int mi=inf;
    for (int i=1; i<=m; i++){
        int l,r,id;
        in(l);in(r);in(id);
        a[i].l=l;a[i].r=r;a[i].id=id;
        b[++cnt]=l;b[++cnt]=r;
        mi=min(min(l,r),mi);
    }
    if (mi>1) b[++cnt]=mi-1;
    b[++cnt]=n;
    sort(b+1,b+1+cnt);
    int p=b[1],tot=1;
    len[1]=b[1];
    for (int i=2; i<=cnt; i++){
        if (b[i]!=p){
        	if(b[i]-b[i-1]==2) {
        		//q[p+1]=++tot;
        		len[++tot]=p+1;
			}
			else if (b[i]-b[i-1]>2) {
				//q[b[i-1]+1]=++tot;
				len[++tot]=b[i-1]+1;
				//q[b[i]-1]=++tot;
				len[++tot]=b[i]-1;
			}
			p=b[i];q[p]=++tot;
            len[tot]=b[i];
        }
    }
    build(1,tot,1);
    for (int i=1; i<=m; i++){
    	int l=lower_bound(len+1,len+1+tot,a[i].l)-len;
    	int r=lower_bound(len+1,len+1+tot,a[i].r)-len;
        if (a[i].id==1) update(1,tot,1,l,r,0);
        else update(1,tot,1,l,r,1);
        printf ("%d\n",tree[1]);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章