这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可。

我自己做的时候太傻逼了。。。把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右的中间值,而不是更新区间的中间值(太菜了)。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxx = 4e5+;
struct node{
int l,r,val,add;
}tree[maxx];
inline L(int r){return r<<;};
inline R(int r){return r<<|;};
inline MID(int l,int r){return (l+r)>>;};
void pushdown(int root){
if (tree[root].add){
tree[L(root)].add=tree[root].add;
tree[R(root)].add=tree[root].add;
tree[L(root)].val=(tree[L(root)].r-tree[L(root)].l+)*tree[root].add;
tree[R(root)].val=(tree[R(root)].r-tree[R(root)].l+)*tree[root].add;
tree[root].add=;
}
}
void buildtree(int root,int l,int r){
tree[root].l=l;
tree[root].r=r;
tree[root].add=;
tree[root].val=;
if (l==r){
tree[root].val=;
return;
}
int mid=MID(l,r);
buildtree(L(root),l,mid);
buildtree(R(root),mid+,r);
tree[root].val=tree[L(root)].val+tree[R(root)].val;
}
void update(int root,int ul,int ur,int c){
int l = tree[root].l,r=tree[root].r;
if (ul<=l && r<=ur){
tree[root].val=(r-l+)*c;
tree[root].add=c;
return;
}
int mid=MID(l,r);//这里一定要注意是取root左右界的中点
if (ul<=mid)
{
pushdown(root);
update(L(root),ul,ur,c);
}
if (ur>mid)
{
pushdown(root);
update(R(root),ul,ur,c);
}
tree[root].val=tree[L(root)].val+tree[R(root)].val;
}
int main(){
int t,n,cas=,q;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
memset(tree,,sizeof(tree));
buildtree(,,n);
int x,y,z;
while(q--){
scanf("%d%d%d",&x,&y,&z);
update(,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",++cas,tree[].val);
}
return ;
}

HDU - 1698 线段树区间修改,区间查询的更多相关文章

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

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

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

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

  3. hdu 1698 线段树 区间修改

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

  4. [线段树]区间修改&区间查询问题

    区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...

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

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

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

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

  7. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  8. SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)

    BGSHOOT - Shoot and kill no tags  The problem is about Mr.BG who is a great hunter. Today he has gon ...

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

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

随机推荐

  1. spring4笔记----报错publicid systemid之间要有空格的解决方法

    <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www. ...

  2. shell编程 学好内功(一)

    shell 背景 什么是shell编程 高大上的解释,往往让人摸不住头脑.一句话概括就是:shell编程就是对一堆Linux命令的逻辑化处理. 为什么要会shell编程 举个简单的例子,我们做java ...

  3. 基数排序python实现

    基数排序python实现 基数排序 基数排序(英语:Radix sort)是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较.由于整数也可以表达字符串(比如名字或 ...

  4. JS 代码中到底加不加分号

    背景 在写自动执行函数时 vm.$watch('datas', function() { console.log(vm.datas); }) (function () { console.log('t ...

  5. 解决ubuntu下,QQ重启后出现个人文件夹已被占用的问题

    首先,是wine QQ的安转教程:Wine安装最新版QQ(8.9.2)的简单教程 - Powered by Discuz! 里面作者也提到了关于重启后出现个人文件夹被占用的情况. 如下: 这里,如果不 ...

  6. ES5-ES6-ES7_async函数

    async 函数概述 async/await应该是目前最简单的异步方案,ES7 中新增了 async/await 两个关键词. async 可以声明一个异步函数,此函数需要返回一个 Promise 对 ...

  7. Numpy 模块的应用

    数据分析三剑客: Numpy, Pandas, Matplotlib NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数 ...

  8. 让你提前认识软件开发(21):C程序中的定时器

    版权声明:本文为博主原创文章.对文章内容有不论什么意见或建议.欢迎与作者单独交流.作者QQ(微信):245924426. https://blog.csdn.net/zhouzxi/article/d ...

  9. Unicode与UTF-8关系

    Unicode字符集合 Unicode 也称为 UCS(Universal Coded Character Set:国际编码字符集合) 是一个字符集合. 对世界上大部分的文字系统进行了整理,编码,使电 ...

  10. 有时间了解一下Spark SQL parser的解析器架构

    1:了解大体架构 2:了解流程以及各个类的职责 3:尝试编写一个