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;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值