HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上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 线段树区间修改,区间查询的更多相关文章
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- hdu 1698 线段树 区间修改
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- 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 ...
- HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...
随机推荐
- java抓取网页或者文件的邮箱号码
抓文件的 package reg; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.i ...
- 消除Warning: Using a password on the command line interface can be insecure的提示
最近在部署Zabbix时需要用脚本取得一些MySQL的返回参数,需要是numberic格式的,但是调用脚本时总是输出这一句: Warning: Using a password on the comm ...
- May 27. 2018 Week 22nd Sunday
All things come to those who wait. 苍天不负有心人. It is said that those who are patient can see what their ...
- MySQL高级知识(二)——Join查询
前言:该篇主要对MySQL中join语句的七种情况进行总结. 0.准备 join主要根据两表或多表之间列的关系,从这些表中进行数据的查询. 首先创建两张表:tb_emp(员工表)和tb_dept(部门 ...
- C#事件の事件聚合器(二)
WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...
- springboot设置session超时和session监听
2.0版本以下设置session超时时间 1. springboot 2.0版本以下配置session超时 1.1 application.properties配置文件: spring.sessio ...
- CDB与PDB之间的切换方法
Oracle 12c 开始支持 PLUGGABLE DATABASE,并且提供了一个方法在CDB和PDB之间切换. 1. 使用 show pdbs 可以确认当前有哪些PDB? SQL> show ...
- 【转】Java日志框架:logback详解
为什么使用logback 记得前几年工作的时候,公司使用的日志框架还是log4j,大约从16年中到现在,不管是我参与的别人已经搭建好的项目还是我自己主导的项目,日志框架基本都换成了logback,总结 ...
- AT987 高橋君
AT987 高橋君 给出 \(n,\ k\) ,求 \(\displaystyle\sum_{i=0}^kC_n^k\) , \(T\) 次询问 \(T\leq10^5,\ 0\leq k\leq n ...
- 关于NOIP2018复赛若干巧合的声明
关于NOIP2018复赛若干巧合的声明 导言 参加NOIP2018时本人学龄只有两个月,却斩获了省一等奖,保送了重点中学的重点班,这看上去是个我创造的神话,然而,在我自己心中,我认为这只是个巧合(其实 ...