一开始这条链子全都是1

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
using namespace std;
///线段树 区间更新
#define MAX 100050
struct node
{
int left;
int right;
int mark;
int total;
};
node tree[MAX*4];
int build(int root,int left,int right)
{
tree[root].left=left;
tree[root].right=right;
tree[root].mark=1;
if(left==right)
{
return tree[root].total=0;
}
int mid=(left+right)>>1;
tree[root].total=(build(root<<1,left,mid)+build(root<<1|1,mid+1,right)); }
void update_mark(int root)
{
if(tree[root].mark!=0)
{
tree[root].total=(tree[root].right-tree[root].left+1)*tree[root].mark;
if(tree[root].left!=tree[root].right)
{
tree[root<<1].mark=tree[root<<1|1].mark=tree[root].mark;
}
tree[root].mark=0;
}
}
int update(int root,int l,int r,int va)
{
update_mark(root);
if(r<tree[root].left||l>tree[root].right)
return tree[root].total;
if(l<=tree[root].left&&r>=tree[root].right)
{
tree[root].mark=va;
return tree[root].total=(tree[root].right-tree[root].left+1)*va;
}
return tree[root].total=(update(root<<1,l,r,va)+update(root<<1|1,l,r,va));
}
int cal(int root,int l,int r)
{
update_mark(root);
if(r<tree[root].left||l>tree[root].right)
return 0;
if(l<=tree[root].left&&r>=tree[root].right)
{
return tree[root].total;
}
return (root<<1,l,r)+cal(root<<1|1,l,r);
}
int main(){
int t;
scanf("%d",&t);
int tt=0;
while(t--)
{
tt++;
int n;
scanf("%d",&n);
build(1,1,n);
int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;
int val;
scanf("%d%d%d",&l,&r,&val);
update(1,l,r,val);
}printf("Case %d: The total value of the hook is %d.\n",tt,cal(1,1,n ));
}
}

  

HDU 1698 线段树 区间更新求和的更多相关文章

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

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

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

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

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

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

  4. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

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

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

  6. FZU 2171 线段树 区间更新求和

    很模板的题 在建树的时候输入 求和后更新 #include<stdio.h> #include<string.h> #include<algorithm> #inc ...

  7. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

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

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

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

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

随机推荐

  1. 获取4G以上的文件大小

    1.DWORD dwFileSizeHigh;  // 得到文件大小的高位  __int64 qwFileSize = GetFileSize(m_hSrcBigFile, &dwFileSi ...

  2. linux_command_撷叏命令: cut, grep

    [root@www ~]# cut -d'分隑字符' -f fields <==用亍有特定分隑字符[root@www ~]# cut -c 字符区间<==用亍排列整齐癿讯息选项不参数:-d ...

  3. Quartus 软件的使用之PLL的使用

    练习使用Altera FPGA 内的 PLL IP核: 思路:将clk_50M倍频到clk_100M,然后观察100M时钟. 1.生成PLL的核例化文件,然后调用. 点击NEXT之后,等一会会跳出如下 ...

  4. php 增删改查练习

    添加界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  5. PhpStorm Git 配置

    首先需要安装windows下的Git版本,网上有很多我下载的是msysgit反正都差不多,不会的自己百度. 其次打开PhpStorm,点击File ,Settings ,找到Version Contr ...

  6. oracle 10g 学习之多表查询、分组函数(6)

    笛卡尔集 l  笛卡尔集会在下面条件下产生: 省略连接条件 连接条件无效 所有表中的所有行互相连接 l  为了避免笛卡尔集, 可以在 WHERE 加入有效的连接条件. 自连接 select m.las ...

  7. AndroidDev站点推荐-zz

    Android-Dev-Bookmarks 项目目前可以生成书签文件导入到浏览器中了,ADB 站点上的内容也是一样的,方便在线浏览.现在内容更新开始按期发布同时也会有个简短的介绍,内容逐渐增多比较难直 ...

  8. div img居中的方式

    想让div中的img水平和垂直都居中,可以将img放在div中,img的样式:height:100%;width:100%; 外部定义div的宽度和高度,然后定义line-height行高,div外部 ...

  9. js 完全分离 window.onload=

    js 完全分离  window.onload= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ...

  10. Struts2 Convention插件的使用

    转自:http://chenjumin.iteye.com/blog/668389 1.常量说明 struts.convention.result.path="/WEB-INF/conten ...