Just a Hook(树状数组)
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
题意:一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。
题解:简单的线段树的区间更新。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=2e5+;
typedef long long ll;
#define lson l,m,i<<1
#define rson m+1,r,i<<1|1
typedef struct Node
{
ll l,r;
ll mid()
{
return (l+r)/2.0;
}
ll value;
} Node;
Node node[MAXN<<];
ll sum[MAXN<<];//用来记录每个节点的值(区间和)
ll add[MAXN<<];//延迟标记数组
void push_up(ll i)
{
sum[i]=sum[i<<]+sum[i<<|];//该节点的和等于左儿子加幼儿子的和
}
void Build(ll l,ll r,ll i)
{
node[i].l=l;
node[i].r=r;
node[i].value=;
sum[i]=;
add[i]=;
if(l==r)
{
sum[i]=;
node[i].value=;
return ;
}
ll m=node[i].mid();
Build(lson);//建立左子树
Build(rson);//建立右子树
push_up(i);
}
ll M;
void Push_down(ll i,ll len)//延迟标记下压
{
if(add[i])
{
add[i<<]=add[i];//把该节点的标记赋给他的左儿子
51 add[i<<|]=add[i];//把该节点的标记赋给他的右儿子
sum[i<<]=add[i]*(len-(len>>));//把左儿子更新
sum[i<<|]=add[i]*(len>>);//把右儿子更新
add[i]=;//把该节点的延迟标记删除
}
} void update(ll l,ll r,ll i,ll v)
{
if(node[i].r==r&&node[i].l==l)
{
add[i]=v;//延迟标记
sum[i]=v*(r-l+);//给该节点赋值
return;
}
ll m=node[i].mid();
Push_down(i,node[i].r-node[i].l+);//来判断节点是否有延迟标记,若有则更新
if(r<=m)
update(l,r,i<<,v);//查询区间在该节点的左子树上
else
{
if(l>m)
update(l,r,i<<|,v);///查询区间在该节点的右子树上 else
{
update(l,m,i<<,v);//查询区间在该节点的两颗树上
update(m+,r,i<<|,v);
}
}
push_up(i);//归的第一步,向上更新
}
int main()
{
ll m,n,a,b,T,c;
ll flag=;
scanf("%lld",&T);
while(T--)
{
sum[]=; scanf("%lld%lld",&m,&n);
Build(,m,);//建树
flag++;
while(n--)
{ scanf("%lld%lld%lld",&a,&b,&c);
update(a,b,,c);//更新树
}
printf("Case %lld: The total value of the hook is %lld.\n",flag,sum[]);//sum[1],表示根节点的值即区间的和
}
return ;
}
Just a Hook(树状数组)的更多相关文章
- HZNU-ACM寒假集训Day5小结 线段树 树状数组
线段树 什么时候用线段树 1.统计量可合并 2.修改量可合并 3.通过统计量可直接修改统计量 一句话:满足区间加法即可使用线段树维护信息 理解Lazy Tage 蓝色是要把信息及时维护的节点,红色是本 ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
随机推荐
- android手机尺寸相关p107-p110
1.ldpi-----240x320-----密度120 mdpi-----320x480-----密度160 hdpi-----480x800-----密度240 xhdpi-----720x128 ...
- WebSocket 在烧瓶和龙卷风中的应用
a. 安装 pip3 install gevent-websocket 作用: - 处理Http.Websocket协议的请求 -> socket - 封装Http.Websocket相关数据 ...
- SpringMVC札集(02)——SpringMVC入门完整详细示例(下)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- ROS中使用ABB Yumi IRB14000的一些资料汇总
目前,ABB RobotStudio 已经更新到6.05.01了,可至官网下载. 使用ABB RobotStudio和ROS进行联合调试,请参考下文: http://blog.csdn.net/Zha ...
- Creating a Game with CocosBuilder
Creating a Game with CocosBuilder This tutorial aims to show how you can use CocosBuilder together w ...
- VSCode安装jshint插件报错
Mac电脑上使用VSCode安装jshint插件时提示如下错误: Failed to load jshint library. Please install jshint in your worksp ...
- HDU - 6041:I Curse Myself(Tarjan求环&K路归并)
There is a connected undirected graph with weights on its edges. It is guaranteed that each edge app ...
- 数据结构之最小生成树Kruskal算法
1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...
- ArcGIS Runtime SDK for WPF之SimpleRenderer无法添加、报错“图形符号无法序列化为 JSON”
ArcGIS Runtime SDK for WPF之SimpleRenderer无法添加.报错“图形符号无法序列化为 JSON” 在上一篇博文中如果在 esri:Map 里面是否设置了的UseAcc ...
- Hexo博客网站再配置
这两天整理网站方面的事,本地IIS部署,个人网站,发现我的hexo做的个人网站实在很单调,于是找来资料做进一步的配置. 一.网站图标 看一下hexo\themes\modernist\layout\_ ...