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
*/

#includeiostream
#includecstdio
#includevector
#includealgorithm

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];
vectorint 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;
}

 

 

 

最新回复(0)
/jishuPdw_2FnOdSbzzDIadIThgGocMPiqT2MmmDh_2FGdwQ_3D_3D4858287
8 简首页