博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 1698 Just a Hook 线段树+区间更新
阅读量:3905 次
发布时间:2019-05-23

本文共 2599 字,大约阅读时间需要 8 分钟。

题目:

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.

Input

The 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.

Output

For 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

11021 5 25 9 3

Sample Output

Case 1: The total value of the hook is 24.

思路:

线段树的区间更新。

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=1e5+5;int n,t;int tree[maxn<<2];int mark[maxn<<2];void push_up(int rt){ tree[rt]=tree[rt<<1]+tree[rt<<1|1];}void push_down(int rt,int ln,int rn){ if(mark[rt]) { mark[rt<<1]=mark[rt]; mark[rt<<1|1]=mark[rt]; tree[rt<<1]=ln*mark[rt]; tree[rt<<1|1]=rn*mark[rt]; mark[rt]=0; }}void build (int l,int r,int rt){ if(l==r) { tree[rt]=1; return; } int m=(l+r)>>1; build(l,m,rt<<1); build (m+1,r,rt<<1|1); push_up(rt);}void update (int L,int R,int x,int l,int r,int rt){ if(l>=L&&r<=R) { tree[rt]=(r-l+1)*x; mark[rt]=x; return; } int m=(l+r)>>1; push_down(rt,m-l+1,r-m); if(m>=L) update (L,R,x,l,m,rt<<1); if(m

 

转载地址:http://qzoen.baihongyu.com/

你可能感兴趣的文章
【分享】手把手教你使用U盘安装Ubuntu系统
查看>>
Ubuntu下adb无法识别android设备的解决方法
查看>>
使用U盘安装Ubuntu系统的实践小结
查看>>
编译cscope-15.8a遇到的问题与解决方案
查看>>
ubuntu下海信Hisense E920 usb连接不上的处理与adb的连接
查看>>
findbugs的ant脚本实践
查看>>
Ubuntu 12.04 安装 Subversion 1.7
查看>>
scp port 22: Connection refused
查看>>
ubuntu12.04命令行下安装RabbitVCS
查看>>
自定义cscope-index
查看>>
(ubuntu)在andorid andk工程中使用ccache加速编译速度
查看>>
android graphics system学习资料汇总
查看>>
GDB
查看>>
Oracle RAC Failover 详解
查看>>
[转载]Oracle RAC客户端连接不稳定的解决方法
查看>>
ORA RAC ORA-12545:因目标主机或对象不存在,连接失败!
查看>>
证明两节点能正常failover的sql
查看>>
oracle10g rac 报ora-12545错误的解决方案 转载
查看>>
Linux配置Xmanager
查看>>
IP地址正则表达式
查看>>