Just a Hook

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31 Accepted Submission(s): 27
 
Problem Description
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
1
10
2
1 5 2
5 9 3
 
Sample Output
Case 1: The total value of the hook is 24.
 
 
Source
2008 “Sunline Cup” National Invitational Contest
 

题意:

大小为n的数组,数组元素初始值为1,有q次操作,x,y,z表示从第x到第y所有的元素的值变为z,最后问这串数的和。

代码:

//基础的线段树模板题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=;
int t,n,q;
ll sum[maxn*],add[maxn*];
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int len){
if(add[rt]){
add[rt<<]=add[rt];
add[rt<<|]=add[rt];
sum[rt<<]=add[rt]*(len-(len>>));
sum[rt<<|]=add[rt]*(len>>);
add[rt]=;
}
}
void build(int l,int r,int rt){
add[rt]=;
if(l==r){
//scanf("%I64d",&sum[rt]);
sum[rt]=;
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&R>=r){
add[rt]=c;
sum[rt]=(ll)c*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(L,R,c,l,m,rt<<);
if(R>m) update(L,R,c,m+,r,rt<<|);
pushup(rt);
}
ll querry(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) return sum[rt];
pushdown(rt,r-l+);
int m=(l+r)>>;
ll ans=;
if(L<=m) ans+=querry(L,R,l,m,rt<<);
if(R>m) ans+=querry(L,R,m+,r,rt<<|);
return ans;
}
int main()
{
int x,y,z;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%d%d",&n,&q);
build(,n,);
while(q--){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,,n,);
}
printf("Case %d: The total value of the hook is %I64d.\n",cas,querry(,n,,n,));
}
}

HDU1698 线段树(区间更新区间查询)的更多相关文章

  1. HDU1698 线段树(区间更新区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  2. hdu1698 线段树区间更新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  4. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  5. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  6. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  8. hdu1698线段树区间更新

    题目链接:https://vjudge.net/contest/66989#problem/E 坑爹的线段树照着上一个线段树更新写的,结果发现有一个地方就是不对,找了半天,发现是延迟更新标记加错了!! ...

  9. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  10. CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询

    链接: I - 秋实大哥与花 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. CPU设计学习-流水线

    各种名词 标量流水线 超级流水线 超标量流水线与多发射技术 经典五级流水线 IF |Instruction Fetch,取指 ID |Instruction Decode,译码 EX |Execute ...

  2. leetcode个人题解——#15 3sums

    class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { sor ...

  3. 一次大量TIME_WAIT和Recv-Q 堵塞问题排查思路

    记录一下周末出现问题~     仅自己摘记不做任何参考. 第一天故障: 现象: 公司销售群和售后群炸了,说老后台(1.0版本)崩溃了,因为还有部门的业务没来得及迁移到新后台,我当时正在打农药哈哈~ 后 ...

  4. android4.3 Bluetooth分析之扫描分析

    android4.3中引入了蓝牙低能耗le(low energy),相应的也有一些方法/类.不过代码里,并没有找到初始调用的地方.所以这里还是先只分析下bt普通的扫描流程(类似android 4.2) ...

  5. css滤镜让图片模糊

    .mhblur { filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */ -webkit-filter: blur(53px); /* Ch ...

  6. isset、is_null、empty的区别

    版本:PHP 5.4 1.isset() :检测变量是否存在,测试如下: $a = false; $b = null; $c; $d = 0; $e = true; var_dump(isset($a ...

  7. 每个zone的low memory是怎么计算出来的

    内核都是试图让活动页和不活动页的数量均衡 在分配内存时每次都会唤醒wakeup_swapd,这个函数会在 现在是不是已经没有全局的LRU表了?已经都变成per cgroup级别的LRU表了吗? ina ...

  8. css3 字体渐变

    先看个效果 https://www.bienvillecapital.com/ 然后人家样式这样写的 font-family: Overpass,Helvetica,sans-serif; font- ...

  9. 在select中,载入时默认select为空白,选项内不显示空白项

    有两种办法,一种纯css实现,一种借助js实现. html: <body onload="load()"> <select id="abc" ...

  10. 前端基础:CSS样式选择器

    前端基础:CSS样式选择器 CSS概述 CSS是Cascading Style Sheets的简称,中文意思是:层叠样式表,对html标签的渲染和布局.CSS规则由两个主要的部分组成:1.选择器:2. ...