Physical Education Lessons CodeForces - 915E 线段树+离散化

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 ≤ 109, 1 ≤ q ≤ 3·105) — 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]区间,给你q个操作,每次操作选择一个区间[l,r],若k==1,则把该区间内的整数点全变为0,若k==2,则把该区间内的整数点全变为1。问最终[1,n]中有多少1.

思路:线段树最普通的区间更新,区间查询。但是区间最大为1e9,因此需要离散化。

举个例子:

①更新区间内所有值:设区间为[1,500],更新区间[1,100],[200,300],[400,500]。1,100,200,300,400,500离散化后为1,2,3,4,5,6;则区间[1,100]可写为[1,2];[1,200]可写为[2,3];区间[200,300]可写为[3,4];[300,400]可写为[4,5];[400,500]可写为[5,6],这样一一对应是没有问题的。

②更新区间内的所有整点:设区间为[1,5],更新区间[1,2],[4,5],如果还按照①中离散方法,1,2,4,5离散化后的点为1,2,3,4,区间[1,2]变为了[1,2],[4,5]变为了[3,4],由于只更新整数点,即更新1,2,3,4这4个点,因此相当于更新了[1,4]整个区间,而原区间中3这个点是没被更新的,因此按照①离散化出现了错误。因此此时除了处理操作的点之外,还要处理操作点之间的区间。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=3e5+100;
struct node{
	int l;
	int r;
	int op;
}a[maxn],c[maxn*4];
struct Node{
	int l;
	int r;
	int laz;
	int sum;
}tree[maxn*16];
int b[maxn*4];
void pushup(int cur)
{
	tree[cur].sum=tree[cur<<1].sum+tree[cur<<1|1].sum;
	return ;
}
void pushdown(int cur)
{
	if(tree[cur].laz!=-1)
	{
		tree[cur<<1].laz=tree[cur].laz;
		tree[cur<<1].sum=(tree[cur<<1].r-tree[cur<<1].l+1)*tree[cur].laz;
		tree[cur<<1|1].laz=tree[cur].laz;
		tree[cur<<1|1].sum=(tree[cur<<1|1].r-tree[cur<<1|1].l+1)*tree[cur].laz;
		tree[cur].laz=-1;
	}
}
void build(int l,int r,int cur)
{
	tree[cur].l=c[l].l;
	tree[cur].r=c[r].r;
	tree[cur].laz=-1;
	tree[cur].sum=(c[r].r-c[l].l+1);
	if(l==r) return ;
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1);
	pushup(cur);
}
void update(int L,int R,int val,int cur)
{
	if(L<=tree[cur].l&&tree[cur].r<=R)
	{
		tree[cur].sum=(tree[cur].r-tree[cur].l+1)*val;
		tree[cur].laz=val;
		return ;
	}
	pushdown(cur);
	if(L<=tree[cur<<1].r) update(L,R,val,cur<<1);
	if(R>tree[cur<<1].r) update(L,R,val,cur<<1|1);
	pushup(cur);
}
int main()
{
	int n,q,cnt=0,cont=0;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=q;i++) 
	{
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].op);
		b[++cnt]=a[i].l;
		b[++cnt]=a[i].r;
	}
	b[++cnt]=1;b[++cnt]=n;
	sort(b+1,b+1+cnt);
	cnt=unique(b+1,b+1+cnt)-(b+1);
	for(int i=1;i<=cnt;i++)
	{
		if(b[i]-b[i-1]>1)
		{
			c[++cont].l=b[i-1]+1;
			c[cont].r=b[i]-1; 
		}
		c[++cont].l=b[i];
		c[cont].r=b[i]; 
	}
	build(1,cont,1);
	for(int i=1;i<=q;i++)
	{
		if(a[i].op==1) update(a[i].l,a[i].r,0,1);
		else update(a[i].l,a[i].r,1,1);
		printf("%d\n",tree[1].sum);
	}
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值