HDU 1698 Just a Hook

二次联通门 : HDU 1698 Just a Hook

 

 

/*
    HDU 1698 Just a Hook

    线段树裸题
    
    区间覆盖。。。 
*/
#include <cstdio>

#define Max 1000002

void read (int &now)
{
    now = 0;
    register char word = getchar ();
    while (word < '0' || word > '9')
        word = getchar ();
    while (word >= '0' && word <= '9')
    {
        now = now * 10 + word - '0';
        word = getchar ();
    }
} 

struct Segment
{
    struct Segment_Tree
    {
        int l;
        int r;
        int Ruri;
        int Sum;
        int Mid;
    };
    
    Segment_Tree tree[Max << 3];
    
    Segment_Tree *L, *R;
    
    void Build (int l, int r, int now)
    {
        tree[now].l = l;
        tree[now].r = r;
        tree[now].Ruri = 0;
        if (l == r)
        {
            tree[now].Sum = 1;
            return ;
        }
        tree[now].Mid = (l + r) >> 1;
        Build (l, tree[now].Mid, now << 1);
        Build (tree[now].Mid + 1, r, now << 1 | 1);
        tree[now].Sum = tree[now << 1].Sum + tree[now << 1 | 1].Sum;
    } 
    
    void Change_section (int l, int r, int now, int number)
    {
        if (tree[now].l == l && tree[now].r == r)
        {
            tree[now].Sum = (tree[now].r - tree[now].l + 1) * number;
            tree[now].Ruri = number;
            return ;
        }
        if (tree[now].Ruri)
        {
            L = &tree[now << 1];
            R = &tree[now << 1 | 1];  
            L->Ruri = tree[now].Ruri;
            R->Ruri = tree[now].Ruri;
            L->Sum = (L->r - L->l + 1) * tree[now].Ruri;
            R->Sum = (R->r - R->l + 1) * tree[now].Ruri;
            tree[now].Ruri = 0;
        }
        if (r <= tree[now].Mid)
            Change_section (l, r, now << 1, number);
        else if (l > tree[now].Mid)
            Change_section (l, r, now << 1 | 1, number);
        else
        {
            Change_section (l, tree[now].Mid, now << 1, number);
            Change_section (tree[now].Mid + 1, r, now << 1 | 1, number);
        }
        tree[now].Sum = tree[now << 1].Sum + tree[now << 1 | 1].Sum;
    }
    
    int Query_sum (int l, int r, int now)
    {
        if (tree[now].l == l && tree[now].r == r)
            return tree[now].Sum;
        if (tree[now].Ruri)
        {
            L = &tree[now << 1];
            R = &tree[now << 1 | 1];  
            L->Ruri = tree[now].Ruri;
            R->Ruri = tree[now].Ruri;
            L->Sum = (L->r - L->l + 1) * tree[now].Ruri;
            R->Sum = (R->r - R->l + 1) * tree[now].Ruri;
            tree[now].Ruri = 0;
        }
        tree[now].Sum = tree[now << 1].Sum + tree[now << 1 | 1].Sum;
        if (r <= tree[now].Mid)
            return Query_sum (l, r, now << 1);
        else if (l > tree[now].Mid)
            return Query_sum (l, r, now << 1 | 1);
        else
            return Query_sum (l, tree[now].Mid, now << 1) + Query_sum (tree[now].Mid + 1, r, now << 1 | 1);
    }
};

Segment Tree;

int N, M;

int main (int argc, char *argv[])
{
    int T;
    read (T);
    static int N;
    int M;
    for (int ZlycerQan = 1; ZlycerQan <= T; ZlycerQan++)
    {
        read (N);
        Tree.Build (1, N, 1); 
        read (M);
        int x, y, value;
        while (M--)
        {
            read (x);
            read (y);
            read (value);
            Tree.Change_section (x, y, 1, value);
        }
        printf ("Case %d: The total value of the hook is %d.\n", ZlycerQan, Tree.tree[1].Sum);
    }
    return 0;
}

 

posted @ 2017-04-22 20:12  ZlycerQan  阅读(126)  评论(0编辑  收藏  举报