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.

将一段区间设置为同一个数,然后区间求和。自己敲得时候,还是发现了一点细节问题。这个和修改有区别,pushdown的左右孩子的sum是= 而不是 += 可以理解 
还有最重要的一点!!! 以后的lazy还是不要直接写了,应该先判断一下,就因为这个,找了好长时间bug 还有一个问题是 区间求和时的 Pushdown和Pushup 不是必要的???不是太懂。
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=;
int sum[maxn<<],lazy[maxn<<],_,n,q,l,r,val; void Pushup(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|];
} void Pushdown(int rt,int x) {
if(lazy[rt]) {//!!!
lazy[rt<<]=lazy[rt<<|]=lazy[rt];
sum[rt<<]=(x-(x>>))*lazy[rt];//!!! =
sum[rt<<|]=(x>>)*lazy[rt];
lazy[rt]=;
}
} void build(int rt,int L,int R) {
lazy[rt]=;
if(L==R) {
sum[rt]=;
return;
}
int mid=(L+R)>>;
build(rt<<,L,mid);
build(rt<<|,mid+,R);
Pushup(rt);
} void Updata(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) {//核心
lazy[rt]=val;
sum[rt]=(R-L+)*val;
return;
}
Pushdown(rt,R-L+);
int mid=(L+R)>>;
if(l<=mid) Updata(rt<<,L,mid,l,r);
if(r>mid) Updata(rt<<|,mid+,R,l,r);
Pushup(rt);
} int Query(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) return sum[rt];
Pushdown(rt,R-L+);
int ans=;
int mid=(L+R)>>;
if(l<=mid) ans+=Query(rt<<,L,mid,l,r);
if(r>mid) ans+=Query(rt<<|,mid+,R,l,r);
Pushup(rt);
return ans; } int main() {
int cur=;
for(scanf("%d",&_);_;_--) {
scanf("%d",&n);
build(,,n);
scanf("%d",&q);
while(q--) {
scanf("%d%d%d",&l,&r,&val);
Updata(,,n,l,r);
}
printf("Case %d: The total value of the hook is %d.\n",cur++,Query(,,n,,n));
}
}

kuangbin专题七 HDU1698 Just a Hook (区间设值 线段树)的更多相关文章

  1. kuangbin专题七 HDU4027 Can you answer these queries? (线段树)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  2. kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  3. 2019牛客多校第七场E Find the median 权值线段树+离散化

    Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...

  4. BZOJ 3110 ZJOI 2013 K大数查询 树套树(权值线段树套区间线段树)

    题目大意:有一些位置.这些位置上能够放若干个数字. 如今有两种操作. 1.在区间l到r上加入一个数字x 2.求出l到r上的第k大的数字是什么 思路:这样的题一看就是树套树,关键是怎么套,怎么写.(话说 ...

  5. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

  6. 【bzoj3110】[Zjoi2013]K大数查询 权值线段树套区间线段树

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  7. 【bzoj3065】带插入区间K小值 替罪羊树套权值线段树

    题目描述 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间k小值.他每次向它的随从伏特提出 ...

  8. 【BZOJ3065】带插入区间K小值 替罪羊树+权值线段树

    [BZOJ3065]带插入区间K小值 Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理 ...

  9. 线段树:CDOJ1597-An easy problem C(区间更新的线段树)

    An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

随机推荐

  1. 第十一章 Tomcat的系统架构与设计模式(待续)

    Tomcat总体设计 Tomcat中的设计模式

  2. laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl

    laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl laravel 4.2 用composer 安装任何包都会报这个错,通过谷歌找到 ...

  3. javascript——对象的概念——创建对象与销毁对象

    一.创建对象 1.创建空对象 方式一: var o ={};o; //Object {} typeof(o); //"object" 方式二: var o=new Object() ...

  4. 安卓读取SD卡的容量

    在开发中,我们经常会用到SD卡,那么在对SD卡进行读写的时候,我们经常需要判断SD卡的剩余容量是否足够.因此,这次我们来写写获取SD卡容量的程序. 该注意的地方,我都在程序里面有注明了.看程序基本就懂 ...

  5. 5-EasyNetQ之Publish(黄亮翻译)

    EasyNetQ支持的最简单的消息模式是发布/订阅.这个模式是一个极好的方法用来解耦消息提供者和消费者.消息发布者只要简单的对世界说,"这里有些事发生" 或者 "我现在有 ...

  6. java GC是在什么时候,对什么东西,做了什么事情?

    1.新生代有一个Eden区和两个survivor区,首先将对象放入Eden区,如果空间不足就向其中的一个survivor区上放,如果仍然放不下就会引发一次发生在新生代的minor GC,将存活的对象放 ...

  7. CSS代码重构与优化之路(转)

    CSS代码重构与优化之路   阅读目录 CSS代码重构的目的 CSS代码重构的基本方法 CSS方法论 我自己总结的方法 写CSS的同学们往往会体会到,随着项目规模的增加,项目中的CSS代码也会越来越多 ...

  8. springMVC 返回json乱码问题

    多次遇见过这个问题,springMVC下返回给前端的json字符串,中文总是乱码,每次都要去翻一下之前的代码来看解决办法,有必要做个笔记记一下这个问题了. 解决方法: 在方法注解中加入如下: @Req ...

  9. conda 添加bioconda源,创建/删除/重命名环境

    1.conda安装 在https://repo.continuum.io/miniconda/选择conda版本 wget "https://repo.continuum.io/archiv ...

  10. p4301 [CQOI2013]新Nim游戏

    传送门 分析 通过nim游戏我们可以知道我们现在的任务就是通过两轮之后使得剩余的几堆异或和为非0数 所以我们只需要在第一步使得剩余集合的任意非空子集的异或和非0即可 于是我们考虑线性基 我们知道线性基 ...