HDU - 1698 Just a Hook (线段树)

来源:互联网 发布:手机淘宝没有收藏按钮 编辑:程序博客网 时间:2024/05/03 22:55

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1698

题解:

做的我不想说啥了…..此题使用cin的话会超时,结构体tr得开原来数据范围的4倍左右

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define lc (d<<1)#define rc (d<<1|1)#define mid ((l+r)>>1)using namespace std;int n,m;struct node{    int sum, lz;}tr[400040];void build(int d, int l, int r){    tr[d].lz = 0;    tr[d].sum = 1;    if(l == r)    {        return;    }    build(lc, l, mid);    build(rc, mid+1, r);}void lazy(int d, int l, int r){    int x = r - l + 1;    if(tr[d].lz != 0)    {        tr[lc].lz = tr[d].lz;        tr[rc].lz = tr[d].lz;        tr[lc].sum = tr[d].lz * (x - (x>>1));//这个括号贼关键,不用就wa        tr[rc].sum = tr[d].lz * (x>>1);//这里错了好久。。忘记除2tr[d].lz = 0 ;    }}void update(int d, int l, int r, int L, int R, int k){    if(l >= L && r <= R)    {        tr[d].lz = k;        tr[d].sum = k * (r - l + 1);        return;    }    lazy(d,l,r);    if(L <= mid) update(lc,l,mid,L,R,k);    if(R > mid) update(rc,mid+1,r,L,R,k);    tr[d].sum = tr[lc].sum + tr[rc].sum;}int main(){    int t,cnt = 1;    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        build(1,1,n);        scanf("%d", &m);        while(m--)        {            int l,r,k;            scanf("%d%d%d", &l,&r,&k);            update(1,1,n,l,r,k);        }        printf("Case %d: The total value of the hook is %d.\n",cnt++,tr[1].sum);    }    return 0;}
0 0