HDU - 1698 Just a Hook (线段树区间修改)
https://cn.vjudge.net/problem/HDU-1698
题意
大小为n的数组,数组元素初始值为1,有q次操作,x,y,z表示从第x到第y所有的元素的值变为z,最后问1到n的和。
分析
区间修改,给每个区间打标记。注意这里是直接把整个区间都变为某个数。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ;
int n;
struct ND{
int l,r;
ll sum,lazy;
}tree[maxn<<];
int a[maxn];
void pushup(int rt){
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
void pushdown(int rt,int len){
if(tree[rt].lazy){
tree[rt<<].lazy=tree[rt].lazy;
tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].sum=tree[rt].lazy*(len-(len>>));
tree[rt<<|].sum=tree[rt].lazy*(len>>);
tree[rt].lazy=;
}
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
tree[rt].lazy=;
if(l==r){
tree[rt].sum=;
return;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
pushup(rt);
}
void update(int rt,int L,int R,int val){
if(L<=tree[rt].l&&tree[rt].r<=R){
tree[rt].lazy=val;
tree[rt].sum=1ll*val*(tree[rt].r-tree[rt].l+);
return;
}
pushdown(rt,tree[rt].r-tree[rt].l+);
int mid=(tree[rt].l+tree[rt].r)>>;
if(mid>=L) update(rt<<,L,R,val);
if(mid<R) update(rt<<|,L,R,val);
pushup(rt);
}
ll query(int rt,int L,int R){
if(L<=tree[rt].l&&tree[rt].r<=R) return tree[rt].sum;
pushdown(rt,tree[rt].r-tree[rt].l+);
ll sum=;
int mid=(tree[rt].l+tree[rt].r)>>;
if(mid<R) sum+=query(rt<<|,L,R);
if(mid>=L) sum+=query(rt<<,L,R);
return sum;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
int cas=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
// for(int i=1;i<=n;i++) a[i]=1;
build(,,n);
int q;
scanf("%d",&q);
while(q--){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(,x,y,z);
}
printf("Case %d: The total value of the hook is %lld.\n",cas++,query(,,n));
}
return ;
}
HDU - 1698 Just a Hook (线段树区间修改)的更多相关文章
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
随机推荐
- 【vijos1780】【NOIP2012】开车旅行 倍增
题目描述 有\(n\)个城市,第\(i\)个城市的海拔为\(h_i\)且这\(n\)个城市的海拔互不相同.编号比较大的城市在东边.两个城市\(i,j\)之间的距离为\(|h_i-h_j|\) 小A和小 ...
- 使用页面Cookie做ABTest测试
背景介绍: 公司为了提升网站销售,做了2种不同风格的Bug页面,需要测试哪个页面的销售效果更好,使用了ABTest. 原理: 当用户访问www.website.com/buy.php时,生成一个随机数 ...
- JAVA 获取指定网址的IP地址 实例
如今买票是一大难事,在高峰时段 打开12306网站,慢的像蜗牛,想到以前用修改hosts文件来登录Google(Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址 ...
- [WC2008]游览计划 解题报告
[WC2008]游览计划 斯坦纳树板子题,其实就是状压dp 令\(dp_{i,s}\)表示任意点\(i\)联通关键点集合\(s\)的最小代价 然后有转移 \[ dp_{i,S}=\min_{T\in ...
- luogu1983 车站分级 (拓扑排序)
对每趟车建一个虚点p,对于不停车的x,连边(x,p,1):对于停车的y,连边(p,y,0)有一条边(a,b,l)就是说b-a>=l由于题目保证一定能走,直接拓扑序dp算最大的就行了 #inclu ...
- 通俗讲解MOSFET
一位工程师曾经对我讲,他从来不看MOSFET数据表的第一页,因为“实用”的信息只在第二页以后才出现.事实上,MOSFET数据表上的每一页都包含有对设计者非常有价值的信息.但人们不是总能搞得清楚该如何解 ...
- Codeforces Round #508 (Div. 2) C D
C: C - Gambling 给你两个数列 每一回合A可以选择从第一个序列里面选一个数或者清除第二个序列里面选一个数 同理B能从第二序列里面选数或者清除第一个序列里面一个数 然后 求A所选的数之和 ...
- 逗号分隔的字符串与List互转
将逗号分隔的字符串转换为List // 将逗号分隔的字符串转换为List String str = "a,b,c"; // 1.使用JDK,逗号分隔的字符串-->数组--&g ...
- A1111. Online Map
Input our current position and a destination, an online map can recommend several paths. Now your jo ...
- A1014. Waiting in Line
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...