题目连接

题意:

一条长为N的铜链子,每个结点的价值为1。有两种修改,l,r,z;
z=2:表示把[l,r]区间内链子改为银质,价值为2.
z=3:表示把[l,r]区间内链子改为金质,价值为3.

思路:

线段树,区间重设,求和。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100000+10;
struct node
{
int l;
int r;
int v;//标记该区间内的链条的属性,-1代表有杂色,1,2,3代表为纯色,并且属性值为;
};
node tree[maxn*4];
void maketree(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].v=1;起始为铜
if(r==l)
return ;
int mid=(l+r)>>1;
maketree(i<<1, l, mid);
maketree(i<<1|1, mid+1, r);
}
void update(int i,int l,int r,int v)
{
if(tree[i].v==v) //如果该区域内值一样则不用修改,以保证tree[i].l<=l,r<=tree[i].r;
return ;
if(tree[i].l==l&&tree[i].r==r)//如果区间与change区间一样则直接将tree[i].v=v;
{
tree[i].v=v;
return ;
}
if(tree[i].v!=-1)//修改区间不一致,并且为纯色则,向下延伸
{
tree[i<<1].v=tree[i<<1|1].v=tree[i].v;//颜色向下分配
tree[i].v=-1;//代表杂色
}
int mid=(tree[i].l+tree[i].r)>>1;
if(l>=mid+1)
update(i<<1|1, l, r, v);
else if(r<=mid)
update(i<<1, l, r, v);
else
{
update(i<<1, l, mid, v);
update(i<<1|1, mid+1,r,v);
}
}
int sum(int i)
{
if(tree[i].v!=-1)//叶子节点一定是纯色。
return (tree[i].r-tree[i].l+1)*tree[i].v;
return sum(i<<1)+sum(i<<1|1);
}
int main ()
{
int T;scanf("%d",&T);
int k=1;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
maketree(1,1,n);
int l,r,v;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&l,&r,&v);
update(1, l, r, v);
}
printf("Case %d: The total value of the hook is %d.\n",k++,sum(1));
}
return 0;
}

HDU 1698 <线段树,区间set>的更多相关文章

  1. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  2. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  3. hdu 1698 线段树 区间更新 区间求和

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

  4. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  5. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  6. HDU(1698),线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...

  7. HDU 1698 (线段树 区间更新) Just a Hook

    有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...

  8. hdu 1698 线段树 区间修改

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

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

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  10. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

随机推荐

  1. 二分法经典习题——HDU1969

    #include <iostream>#include <cmath>#include <iomanip>using namespace std; double p ...

  2. Docker私有仓库2

    http://www.cnblogs.com/womars/p/5906410.html 接着上篇,上面为上篇地址. #通过docker tag将该镜像标志为要推送到私有仓库 [root@lh- ~] ...

  3. storm.yaml 配置项

    配置项 配置说明 storm.zookeeper.servers ZooKeeper服务器列表 storm.zookeeper.port ZooKeeper连接端口 storm.local.dir s ...

  4. HDU 1969 Pie(二分搜索)

    题目链接 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pi ...

  5. jquery 动态生成元素 事件

    $(document).on("click",".detail",function () {});

  6. html5游戏开发框架之lufylegend开源库件学习记录

    下载地址http://lufylegend.com/lufylegend 引用 <script type="text/javascript" src="../luf ...

  7. sqlserver 按日、周、月统计方法

    摘自于网络网络:http://blog.csdn.net/wanmdb/article/details/8080636 create table T(日期时间 datetime, 数量 int) in ...

  8. 1001. A+B Format

    Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...

  9. android mediaplayer

  10. iosOC可变数组选择,冒泡排序

    #pragma mark 可变数组的排序 NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1",@&quo ...