HDU 4107

来源:互联网 发布:linux 查看字符编码 编辑:程序博客网 时间:2024/06/05 17:44

线段树的题目

#include <cstdio>#define LL(x) x << 1#define RR(x) x << 1 | 1const int MAXN = 200000 + 123;struct NODE{    int l, r, rmax, rmin;    int lazy;    int mid()    {        return (l + r) >> 1;    }}tree[MAXN * 3];int p;int c, C;int max(int a, int b){    return a > b ? a : b;}int min(int a, int b){    return a > b ? b : a;}void build(int l, int r, int idx){    tree[idx].l = l;    tree[idx].r = r;    tree[idx].rmax = tree[idx].rmin = 0;    tree[idx].lazy = 0;    if(l == r)  return;    int mid = tree[idx].mid();    build(l, mid, LL(idx));    build(mid + 1, r, RR(idx));}void push_down(int idx){    int l = LL(idx);    int r = RR(idx);    tree[l].lazy += tree[idx].lazy;    tree[l].rmax += tree[idx].lazy;    tree[l].rmin += tree[idx].lazy;    tree[r].lazy += tree[idx].lazy;    tree[r].rmax += tree[idx].lazy;    tree[r].rmin += tree[idx].lazy;    tree[idx].lazy = 0;}void push_up(int idx){    int l = LL(idx);    int r = RR(idx);    tree[idx].rmax = max(tree[l].rmax, tree[r].rmax);    tree[idx].rmin = min(tree[l].rmin, tree[r].rmin);}void update(int l, int r, int idx){    if(l <=  tree[idx].l && tree[idx].r <= r)    {        if(tree[idx].rmax < p)        {            tree[idx].lazy += c;            tree[idx].rmax += c;            tree[idx].rmin += c;            return ;        }        if(tree[idx].rmin >= p)        {            tree[idx].lazy += C;            tree[idx].rmax += C;            tree[idx].rmin += C;            return ;        }    }    push_down(idx);    int mid = tree[idx].mid();    if(r <= mid)  update(l, r, LL(idx));    else if(l > mid)  update(l, r, RR(idx));    else    {        update(l, mid, LL(idx));        update(mid + 1, r, RR(idx));    }    push_up(idx);}bool flag;void query(int idx){    if(tree[idx].l == tree[idx].r)    {        if(flag) printf(" ");        else flag = true;        printf("%d", tree[idx].lazy);        return;    }    int l = LL(idx);    int r = RR(idx);    tree[l].lazy += tree[idx].lazy;    tree[r].lazy += tree[idx].lazy;    query(l);    query(r);}inline void scan(int &u, int &v, int &w){    char c;    while(c = getchar(), c < '0' || c > '9');    u = c - '0';    while(c = getchar(), c <= '9' && c >= '0')    u = u * 10 + c - '0';    while(c = getchar(), c < '0' || c > '9');    v = c - '0';    while(c = getchar(), c <= '9' && c >= '0')    v = v * 10 + c - '0';    while(c = getchar(), c < '0' || c > '9');    w = c - '0';    while(c = getchar(), c <= '9' && c >= '0')    w = w * 10 + c - '0';}int main(){    int n, m;    while(scanf("%d%d%d", &n, &m, &p) != EOF)    {        build(1, n, 1);        while(m--)        {            int a, b;            scan(a, b, c);            C = c * 2;            update(a, b, 1);        }        flag = false;        query(1);        puts("");    }    return 0;}

原创粉丝点击