[hdu]1698 Just a Hook -- 线段树

1Y了哈~

优化方法和我上午刚做的[PKU] 3468 A Simple Problem with Integers 差不多

在hdu用C++交比G++快了一倍。。

以下是优化过的代码:

27204772010-07-30 21:09:12Accepted1698453MS4120K1731 BC++TJRAC_ACMercy(紫薇)

View Code

Problem : 1698 ( Just a Hook )     Judge Status : Accepted
RunId : 2720477    Language : C++    Author : yueashuxia
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include <cstdio>
#include <cstring>
using namespace std;
const int size = 100001 ;
int tot ;
struct xds
{
    int a, b, lson, rson ;
    int info ;
}tree[size*3] ;

void build(int a, int b)
{
    int now = ++tot ;
    tree[now].a = a ;
    tree[now].b = b ;
    tree[now].info = 1 ;
    int mid = (a + b) >> 1 ;
    if(a == b) {
        tree[now].lson = tree[now].rson = 0; 
         return ;
    }
    tree[now].lson = tot + 1 ;
    build(a, mid) ;
    tree[now].rson = tot + 1 ;
    build(mid + 1, b) ;
}

void update(int p, int a, int b, int info)
{
    if(a == tree[p].a && tree[p].b == b)
    {
        tree[p].info = info ;
         return ;
    }
    if(tree[p].a == tree[p].b) return ;
    int mid = (tree[p].a + tree[p].b) >> 1 ;
    if(tree[p].info != -1)
    {
        tree[tree[p].lson].info = tree[tree[p].rson].info = tree[p].info ;
           tree[p].info = -1 ;
    }
    if(a > mid) update(tree[p].rson, a, b, info) ;
    else if(b <= mid) update(tree[p].lson, a, b, info) ;
    else update(tree[p].lson, a, mid, info), update(tree[p].rson, mid + 1, b, info) ;
}

int query(int p)
{
    if(tree[p].info != -1) 
         return tree[p].info * (tree[p].b - tree[p].a + 1) ;
    else {
        int mid = (tree[p].a + tree[p].b) >> 1 ;
        return query(tree[p].lson) + query(tree[p].rson) ;
       }
}

int main (){
    int T, i, j, k, n, Q, no = 1 ;
    scanf("%d", &T);
    while(T --)
    {
        scanf("%d%d", &n, &Q);
        tot = 0;
        build(1, n) ;
        while(Q --){
             scanf("%d%d%d", &i, &j, &k);
             update(1, i, j, k) ;
        }
        printf("Case %d: The total value of the hook is %d./n", no ++, query(1));
    }
    return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值