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 ...
随机推荐
- Dockerfile 模版
最近一直在用docker,总结了一个靠谱的模版,分享给大家. From ubuntu:14.04 MAINTAINER pidong.li@genetronhealth.com RUN echo de ...
- 10.Linux网卡的配置及详解
1.网卡配置文件在/etc/sysconfig/network-scripts/下: [root@oldboy network-scripts]# ls /etc/sysconfig/network- ...
- 程序try-catch的绝对健壮性之嵌套
写程序的过程中,我们对try-catch在熟悉不过了,捕获异常进行处理,以保证程序的健壮性. 今日突发一想,如果我们catch中的代码异常了怎么办?我们做以下一种假设 static void Main ...
- ASP调用存储过程访问SQL Server
ASP调用存储过程访问SQL Server 2011-02-15 10:22:57 标签:asp 数据库 sQL 存储过程 Server ASP和存储过程(Stored Procedures)的文章 ...
- Python 字符串相似性的几种度量方法
字符串的相似性比较应用场合很多,像拼写纠错.文本去重.上下文相似性等. 评价字符串相似度最常见的办法就是:把一个字符串通过插入.删除或替换这样的编辑操作,变成另外一个字符串,所需要的最少编辑次数,这种 ...
- 转载:TCP连接的状态详解以及故障排查
FROM:http://blog.csdn.net/hguisu/article/details/38700899 该博文的条理清晰,步骤明确,故复制到这个博文中收藏,若文章作者看到且觉得不能装载,麻 ...
- 【MFC】VC界面绘制双缓存
VC界面绘制双缓存 转载请注明原文网址: http://www.cnblogs.com/xianyunhe/archive/2011/11/20/2255811.html 1.闪屏的问题在GDI的绘图 ...
- 完全卸载session 所需要的函数
session_unset() 删除内存当中的session数据:必须放在session_destroy的前边.因为应用session_destory后session_id();就会消失. 删除se ...
- flash游戏服务器安全策略
在网页游戏开发中,绝大多数即时通信游戏采用flash+socket 模式来作为消息数据传递.在开发过程中大多数开发者在开发过程中本地没有问题,但是一旦部署到了网络,就存在连接上socket服务器.究 ...
- Proposition
提供 \(k\) 个变量 \((k\leq 4)\) 可独立取值为 \(0,1\),两种运算分别等价于 \(\neg a\) 和 \(\neg a \lor b\) . 你需要恰好使用 \(n\) 个 ...