In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

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(树状数组)的更多相关文章

  1. HZNU-ACM寒假集训Day5小结 线段树 树状数组

    线段树 什么时候用线段树 1.统计量可合并 2.修改量可合并 3.通过统计量可直接修改统计量 一句话:满足区间加法即可使用线段树维护信息 理解Lazy Tage 蓝色是要把信息及时维护的节点,红色是本 ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  4. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  7. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  8. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  9. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

随机推荐

  1. Struts02---实现struts2的三种方式

    01.创建普通类 /** * 01.普通类 * 写一个execute() 返回String类型值 * */ public class HelloAction01 { public String exe ...

  2. New Concept English three (31)

    35w/m 45 True eccentrics never deliberately set out to draw attention to themselves. They disregard ...

  3. 初识CSS(1)

    1.css的语法 a.位置 <head> <style type="text/css"> //css代码 </style> </head& ...

  4. [Scala]Scala学习笔记三 Map与Tuple

    1. 构造映射 可以使用如下命令构造一个映射: scala> val scores = Map("Alice" -> 90, "Kim" -> ...

  5. Leetcode 1013. Partition Array Into Three Parts With Equal Sum

    简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...

  6. 使用stm32F4Discovery 的stlink v2给其他板子调试

    不适用stm8. 1. 拔掉 CN3 的 跳线帽 2.CN2 的 原理图 3.按照2中的原理图和板子(核心板stm32c8t6),实际上我这边连接使用的结果是: 4. 5. 6.相关资料: 链接:ht ...

  7. 神奇的TextField(1)

    先看一大段测试代码,每个小方法的注释行是输出结果. private var text_content:TextField; private function textFieldDemo():void{ ...

  8. vector释放内存之swap方法

    相信大家看到swap这个词都一定不会感到陌生,就是简单的元素交换.但swap在C++ STL中散发着无穷的魅力.下面将详细的说明泛型算法swap和容器中的swap成员函数的使用! 1. 泛型算法swa ...

  9. 剑指offer-第六章面试中的各项能力(n个骰子的点数)

    题目:把n个骰子扔到地上,骰子之和为S,输入n,打印s所有可能的值出现的概率. 思路:由于骰子的点数为1~6,因此n个骰子之和的大小为n~6n之间.故可以定义一个数组来存放这6n-n+1个数出现的次数 ...

  10. Win32 API中使用定时器的三种方法

    转自:http://blog.csdn.net/fancycow/article/details/6676064 1.SetTimer(HWND,UINT,UINT,TIMERPROC);第一个参数设 ...