线段树的区间更新 hdu 1698
***第一次写的果断超时,所以百度了一下,知道我写的每一次都要递归最底层,这样会花费很多时间,第二次写得线段树的区间更新,因为一个条件写错了,真是让我坑到死,
这样区间相同的不必更新,省了很多时间。。。***
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue> using namespace std;
typedef long long LL;
#define oo 0x3f3f3f3f
#define N 200100 struct node
{
int l, r;
int s;
} tree[N*4]; void build(int l, int r, int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].s=1;
if(l==r)
{
return ;
}
int mid=(l+r)/2;
build(l, mid, rt*2);
build(mid+1, r, rt*2+1);
} void update(int l, int r, int v, int rt)
{
if(tree[rt].s==v)
return ;
if(tree[rt].l==l&&tree[rt].r==r)
{
tree[rt].s=v;
return ;
}
if(tree[rt].s!=-1)
{
tree[rt*2].s=tree[rt*2+1].s=tree[rt].s;
tree[rt].s=-1;
}
int mid=(tree[rt].l+tree[rt].r)/2;
if(r<=mid) update(l, r, v, rt*2);
else if(l>mid) update(l, r, v, rt*2+1);
else
{
update(l, mid, v, rt*2);
update(mid+1, r, v, rt*2+1);
}
} int query(int rt)
{
if(tree[rt].s!=-1)
return (tree[rt].r-tree[rt].l+1)*tree[rt].s;
else
return query(rt*2)+query(rt*2+1);
} int main()
{
int T, n, m, a, b, c, cas=1;
scanf("%d", &T); while(T--)
{
scanf("%d%d", &n, &m);
build(1, n, 1);
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
update(a, b, c, 1);
}
printf("Case %d: The total value of the hook is %d.\n", cas++, query(1));
}
return 0;
}
/*
20
3
2
1 2 2
2 2 3
*/
线段树的区间更新 hdu 1698的更多相关文章
- 线段树 [成段更新] HDU 1698 Just a Hook
成段更新,需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候. 此处建议在纸上模拟一遍. Problem Descript ...
- hdu 1698:Just a Hook(线段树,区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- UVA 12436-Rip Van Winkle's Code(线段树的区间更新)
题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...
- hdu1698线段树的区间更新区间查询
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- zoj3686(线段树的区间更新)
对线段树的区间更新有了初步的了解... A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a ...
- Color the ball (线段树的区间更新问题)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
- 线段树离散化+区间更新——cf1179C好题
绝对是很好的题 把问题转化成当第i个询问的答案是数值x时是否可行 要判断值x是否可行,只要再将问题转化成a数组里>=x的值数量是否严格大于b数组里的>=x的值 那么线段树叶子结点维护对于值 ...
随机推荐
- [CF1364E] X-OR
X-OR 题面翻译 题目描述 本题是交互题. 有一个固定的长度为 \(n\) 的排列 \(P\),其值域为 \([0,n-1]\),你可以进行不超过 \(4269\) 次询问,之后你需要输出这个排列 ...
- JXNU acm选拔赛 最小的数
最小的数 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissi ...
- 流媒体服务器ZLMediaKit与FFmpeg
流媒体服务器ZLMediaKit与FFmpeg overview 关键字:ZLMediaKit.FFmpeg.srt.vlc 如果想快速拥有自己的流媒体服务器,那么可以使用开源项目自己搭建.开源的流媒 ...
- Celery 定义和调用异步任务Task
https://docs.celeryq.dev/en/stable/userguide/tasks.html 使用app.task装饰器定义 需要通过导入celery app,然后使用@app.ta ...
- 华企盾DSC在苹果电脑上申请审批没有通知
由于系统通知这里没有允许DSC通知,开启后即可.系统偏好设置-通知与专注模式-通知
- npm 发布流程
登录npm 查看本地是否登录 # 全局配置源 npm who am i # 官方源 npm who am i --registry https://registry.npmjs.org 注: npm源 ...
- Windows Server 2008 R2 & Windows Server 2012 R2 无法通过update更新的解决方法
windows Server 2008 r2 无法通过update更新的解决方法 注意:目前windows Server系列操作系统已经完全停止支持. 1.安装 SP1补丁 KB976932 点击:微 ...
- C# 多线程 progressbar 界面不卡顿简单用法
多线程进度条的简单使用,界面不卡顿.如下图: 简单源码如下: using System; using System.Collections.Generic; using System.Componen ...
- IPv6规模部署和应用成必然趋势,IPv6监测具有很大市场潜力
日前,中央网信办.国家发展改革委.工业和信息化部联合印发<深入推进IPv6规模部署和应用2021年工作安排>.这更深入的表明,IPv6规模部署和应用是互联网演进升级的必然趋势,是网络技术创 ...
- 解决QObject::moveToThread: Current thread (0x56059f9b0f70) is not the object's t
对 opencv 降级 pip install opencv-python==4.1.2.30