一开始这条链子全都是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. LESS学习笔记1

    个人理解:less是一个可以写函数的css

  2. Java 全半角转换

    * 全角转半角的 转换函数* @return String*/public static final String full2HalfChange(String QJstr){StringBuffer ...

  3. c++ static及const(开发者在线)

    static 是c++中很常用的修饰符,它被用来控制变量的存储方式和可见性,下面我将从 static 修饰符的产生原因.作用谈起,全面分析static 修饰符的实质. static 的两大作用: 一. ...

  4. linux架构图

    / 根目录 │ ├boot/ 启动文件.所有与系统启动有关的文件都保存在这里 │ └grub/ Grub引导器相关的文件 │ ├dev/ 设备文件 ├proc/ 内核与进程镜像 │ ├mnt/ 临时挂 ...

  5. tcp连接管理

    [root@ok etc]# cat /proc/sys/net/core/netdev_max_backlog 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目 ...

  6. Mongoose Schemas中定义日期以及timestamps选项的妙用

    本文转自:http://www.cnblogs.com/jaxu/p/5595451.html 在Node.js中使用MongoDB少不了Mongoose.假设有如下Mongoose Schemas的 ...

  7. Android Studio 获取 sha1-wangfeng520@

    WIN+R   打开“运行”  输入  CMD 回车 2 CD C:\Program Files\Java\jdk1.7.0_71\bin     (JDK安装路径) keytool -list -v ...

  8. Xamarin.Android编译CPU类型选择方式

    Xamarin.Android编译CPU类型选择方式 在Xamarin.Android编译的时候,默认提供了5种CPU类型供大家选择.它们分别为armeabi.armeabi-v7a.arm64-v8 ...

  9. documnent.getElementbyId(‘myId’)和$(‘#myId’)哪种更高效?

    第一种更高效,直接调用javascript引擎.

  10. 【python游戏编程之旅】第一篇---初识pygame

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 一.pygame简介 Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础 ...