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 ...
随机推荐
- windows 改路径有小差异
https://jingyan.baidu.com/article/5552ef473e2df6518ffbc916.html cmd是windows下一个非常常用的工具,但是它默认的地址却是不变的. ...
- Python探索记(16)——Python的可变类型与不可变类型
# @Time : 2017/7/8 17:49 # @Author : 原创作者:谷哥的小弟 # @Site : 博客地址:http://blog.csdn.net/lfdfhl # @DESC : ...
- iOS开发之Documentation.build/Script-BC552B3A15.sh:
/Users/hbbhao/Library/Developer/Xcode/DerivedData/AWLive-dmbegyhgamayzudqqdentwngdpkr/Build/Intermed ...
- 3天学习完AngularJS基础内容小结
简介:AngularJS 是一个 JavaScript 框架.它是一个以 JavaScript 编写的库. 一.AngularJS大致功能模块 二.页面交互变得简单 1.示例:计算价格 <htm ...
- Windows10重启之后总是将默认浏览器设置为IE
换了一台电脑之后,发现系统重启之后总是会把我的默认浏览器设置为IE,而自从用上了Chrome,我对他爱不释手. 上网找了不少文章,都建议使用系统自带的设置进行默认浏览器的设置,试了三四次,完全不起任何 ...
- C++ 资源管理 —— RAII
RAII:在构造函数中申请资源,在析构函数中释放资源. 1. RAII 自动实现锁资源的释放 void bad() { m.lock(); f(); if (COND) return; m.unloc ...
- bzoj 2282 消防
Written with StackEdit. Description 某个国家有\(n\)个城市,这\(n\)个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为\(z_i(z_i ...
- 【转】C# Socket编程(5)使用TCP Socket
[转自:https://www.cnblogs.com/IPrograming/archive/2012/10/18/CSharp_Socket_5.html] TCP 协议(Transmission ...
- 集群/分布式环境下,Session处理策略
前言 在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理.如果不做任何处理的话,用户将出现频繁登录的现象.比如集中中存在A.B两台服务器,用户在第一次访问网站是,Ngin ...
- 关于OPC自动化接口编程(OPCDAAuto.dll)几点注意问题
为了能够在工作中方便的应用OPC和充分的理解OPC的开发流程.内部机制,这两天正在研究开发OPC客户端程序,一般我们开发OPC客户端程序有以下几种方式: (1) 使用OPCNetAPI,需 ...