题意:给你个n,表示区间【1,n】,价值初始为1,给你m段区间和价值,更新区间,区间价值以最后更新为准,问更新后区间价值总和为多少

思路:两种方法,可以先存下来,倒过来更新,一更新节点马上跳出,比较快

线段树

#include <iostream>

using namespace std;

int data[100005][3];

int main()
{
int t,q,n,i,j,sum,k,v;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d%d",&n,&q);
for(j=1;j<=q;j++)
scanf("%d%d%d",&data[j][0],&data[j][1],&data[j][2]);
sum=0;
for(k=1;k<=n;k++)
{
v=1;
for(j=q;j>=1;j--)
if(data[j][0]<=k && k<=data[j][1])//寻找k所在的更新区间,若存在则更新
{
v=data[j][2]; //若找的最后面的更新区间,则停止
break;
}
sum+=v;
}
printf("Case %d: The total value of the hook is %d.\n",i,sum);
}
return 0;
}

#include <iostream>
#include<cstdio>
using namespace std;
#define N 300010
struct node{
int value;
int l,r;
}tree[N];
void Build(int l,int r,int id){
tree[id].l=l;
tree[id].r=r;
tree[id].value=1;
if(l!=r){
int mid=(l+r)>>1;
Build(l,mid,id<<1);
Build(mid+1,r,id<<1|1);
}
}
void update(int l,int r,int value,int id){
int mid=(tree[id].l+tree[id].r)>>1;
if(tree[id].l==l&&tree[id].r==r){
tree[id].value=value;
return;
}
if(tree[id].value==value)
return;
if(tree[id].value!=-1){
tree[id<<1].value=tree[id<<1|1].value=tree[id].value;
tree[id].value=-1;
}
if(r<=mid)
update(l,r,value,id<<1);
else if(l>mid)
update(l,r,value,id<<1|1);
else{
update(l,mid,value,id<<1);
update(mid+1,r,value,id<<1|1);
}
}
int query(int id){
if(tree[id].value!=-1)
return (tree[id].r-tree[id].l+1)*tree[id].value;
return query(id<<1)+query(id<<1|1);
}
int main(int argc, char** argv) {
int t,n,m,x,y,k,Cas=1;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
Build(1,n,1);
scanf("%d",&m);
while(m--){
scanf("%d%d%d",&x,&y,&k);
update(x,y,k,1);
}
printf("Case %d: The total value of the hook is %d.\n",Cas++,query(1)); }
return 0;
}

hdu 1698 Just a Hook_线段树的更多相关文章

  1. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  2. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  3. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  5. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  7. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  9. HDU 1698 Just a Hook(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

随机推荐

  1. 如何从Eclipse导入github上的项目源码

    1.首先在github.com上申请一个账号,比如笔者的账号为puma0072.Eclipse需要安装egit插件,在Eclipse中选择help->Marketplace,在search中输入 ...

  2. Combination Sum II 解答

    Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...

  3. Pascal's Triangle 解答

    Question Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  4. MongoDBAuth

    1,mogoDB 认证登陆

  5. [破解] DRM-内容数据版权加密保护技术学习(中):License预发放实现

    在上一篇文章里实现了对媒体文体的DRM加密,现在一起来实现License的预发放. 所谓预发放就是在播放媒体文件之前先获取到License,License获取成功后,可直接在电脑上进行媒体文件的播放. ...

  6. MySQL数据库如何解决大数据量存储问题

    利用MySQL数据库如何解决大数据量存储问题? 各位高手您们好,我最近接手公司里一个比较棘手的问题,关于如何利用MySQL存储大数据量的问题,主要是数据库中的两张历史数据表,一张模拟量历史数据和一张开 ...

  7. Android 关于屏幕适配

    android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 转自:http://www.c ...

  8. 【初级坑跳跳跳】[NULLException] findViewById() id 引用错误,导致空指针

    在学习Intent页面切换,几个页面切换,导致view id 写错,写成另一个xml里的id去了,导致空指针异常 setContentView(R.layout.activity_second); B ...

  9. HDU 3306 - Another kind of Fibonacci

    给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2). 求 S(N) = A(0) 2 +A(1) 2+……+ ...

  10. weblogic上部署应用程序

    weblogic上部署应用程序有三种方法: 一:修改配置文件config.xml在文件中加入如下代码片段: <app-deployment> <name>FAB</nam ...