cf 915E Physical Education Lessons

一 原題

 

E. Physical Education Lessons
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
 

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],每個點價值爲1,給定q個操作,每個操作可以把[l, r]內的點價值清0,也可以把[l, r]內的點價值設爲1,問最終[1, n]上的點的價值總和。

數據範圍:n<=1e9,q<=3e5

分析:很裸的線段樹對吧。。但是n範圍好大。。需要離散化,這樣複雜度就只和q相關了。。另外即使關了同步,同樣的代碼用cin/ cout還是會TLE?

 

三 代碼

 

/*
PROB: cf 915E
LANG: c++
AUTHOR: maxkibble
*/

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

// #define local
#define pb push_back
#define L(x) (x << 1)
#define R(x) ((x << 1) + 1)

const int maxq = 6e5 + 10;

struct SegNode {
    int left, right, lazy, val, sz;
    
    SegNode(): lazy(-1) {}
}segTree[maxq << 2];

int n, q, l[maxq], r[maxq], k[maxq], d[maxq];
vector<int> v;

int f(int x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin();
}

inline void setSegNode(int id, int op) {
    segTree[id].lazy = op;
    segTree[id].val = segTree[id].sz * op;
}

inline void pushDown(int id) {
    int ll = segTree[id].left, rr = segTree[id].right;
    int op = segTree[id].lazy;
    if(rr - ll >= 2 && op != -1) {
        setSegNode(L(id), op);
        setSegNode(R(id), op);
    }
    segTree[id].lazy = -1;
}

void update(int x, int y, int op, int id) {
    int ll = segTree[id].left, rr = segTree[id].right;
    if(x <= ll && y >= rr) {
        setSegNode(id, op);
        return;
    }
    if(x >= rr || y <= ll) {
        return;
    }
    
    pushDown(id);
    update(x, y, op, L(id));
    update(x, y, op, R(id));

    segTree[id].val = segTree[L(id)].val + segTree[R(id)].val;
}

void build(int x, int y, int id) {
    segTree[id].left = x;
    segTree[id].right = y;
    if(y - x < 2) {
        segTree[id].sz = d[x];
        return;
    }

    int mid = (x + y) >> 1;
    build(x, mid, L(id));
    build(mid, y, R(id));
    
    segTree[id].sz = segTree[L(id)].sz + segTree[R(id)].sz;
}

int main() {
    #ifdef local
        freopen("e.in", "r", stdin);
    #endif
    int n, q; scanf("%d%d", &n, &q);
    for(int i = 0; i < q; i++) {
        scanf("%d%d%d", &l[i], &r[i], &k[i]);
        l[i]--;
        k[i] %= 2;
        v.pb(l[i]);
        v.pb(r[i]);
    }
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end()) - v.begin());
    for(int i = 0; i < v.size() - 1; i++) {
        d[i] = v[i + 1] - v[i];
    }
    d[v.size() - 1] = n - v.back();

    build(0, v.size(), 1);

    for(int i = 0; i < q; i++) {
        update(f(l[i]), f(r[i]), k[i], 1);
        printf("%d\n", n - segTree[1].val);
    }
    return 0;
}

 

 

 

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