1Y了哈~
优化方法和我上午刚做的[PKU] 3468 A Simple Problem with Integers 差不多
在hdu用C++交比G++快了一倍。。
以下是优化过的代码:
2720477 | 2010-07-30 21:09:12 | Accepted | 1698 | 453MS | 4120K | 1731 B | C++ | 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
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 ;
}