首页 > 其他 > 详细

Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

时间:2020-02-11 10:08:59      阅读:73      评论:0      收藏:0      [点我收藏+]
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+10;
struct node{
    int l,r;
    int sum;
    int add;
}tr[N*4];
void pushdown(int root)
{
    if (tr[root].add)
    {
        tr[root<<1].add=tr[root].add;
        tr[root<<1|1].add=tr[root].add;
        int mid=(tr[root].l+tr[root].r)/2;
        tr[root<<1].sum=tr[root].add*(mid-tr[root].l+1);
        tr[root<<1|1].sum=tr[root].add*(tr[root].r-mid);
        tr[root].add=0;
    }
}
void pushup(int u)
{
    tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}
void build(int root, int l, int r)
{
    tr[root].l=l;
    tr[root].r=r;
    tr[root].add=0;
    tr[root].sum=0;
    if(l==r)
    {
        tr[root].sum = 1;
        return ;
    }
    int mid=l+r>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    pushup(root);
}
void update(int root,int ql,int qr,int c)
{
    if (ql>tr[root].r||qr<tr[root].l)
        return;
    if (ql<=tr[root].l&&tr[root].r<= qr)
    {
        tr[root].sum=(tr[root].r-tr[root].l+1)*c;
        tr[root].add=c;
    }
    else
    {
        pushdown(root);
        int mid=tr[root].l+tr[root].r>>1;
        update(root<<1,ql,qr,c);
        update(root<<1|1,ql,qr,c);
        pushup(root);
    }
}
int main()
{
    int t,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        build(1,1,n);
        while(q--)
        {
            int l,r,op;
            scanf("%d%d%d",&l,&r,&op);
            update(1,l,r,op);
        }
        printf("Case %d: The total value of the hook is %d.\n", ++cnt, tr[1].sum);
    }
}
 

 

Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

原文:https://www.cnblogs.com/QingyuYYYYY/p/12293795.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!