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 ...
随机推荐
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 条款25:考虑写出一个不抛出异常的swap函数
首先说下标准库的swap算法: namespace std{ template<typename T> void swap(T & a, T & b) { T tmp = ...
- //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- FlashDevelop安装配置
1.下载 FlashDevelop,flash sdk,flash sdk debug,.net framework,java sdk(32位,不管机器是多少位,否则不能单步调试的) 2. 安装Fl ...
- postgresql recovery.conf文件内容说明
在配置PG主备流复制.HA时,需要用到recovery.conf文件,这里根据自己的了解做个记录: standby_mode = 'on' #说明自己是备库 primary_conninfo = 'u ...
- SVG 总结
//文件名:11.svg<?xml version="1.0" encoding="UTF-8" ?> <!--XML NameSpace:名 ...
- 在Wifi网络中嗅探明文密码(HTTP POST请求、POP等)
全世界,现在大约50%的网站没有使用SSL加密,天朝尤其多. 我们都知道通过HTTP发送的数据都是明文,没有使用任何加密,即使是在数据敏感的登录页面. 本文的目的是:如果你在不熟悉的网络环境中,要注意 ...
- python学习之控制语句
#if statement number=int(input("please input a number")); if number<10 : print("is ...
- 剑指offer-第四章解决面试题的思路(从上往下打印二叉树)
题目:从上往下打印二叉树的每一个节点,同一层的节点按照从左到右的顺序打印 思路:这是一个层序遍历的问题,因此要借用到队列.我们可以在打印第一个节点的同时将这个节点的左右子节点都放入队列,同样打印左右子 ...
- Hexo博客网站再配置
这两天整理网站方面的事,本地IIS部署,个人网站,发现我的hexo做的个人网站实在很单调,于是找来资料做进一步的配置. 一.网站图标 看一下hexo\themes\modernist\layout\_ ...