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. [我的CVE][CVE-2017-15708]Apache Synapse Remote Code Execution Vulnerability

    漏洞编号:CNVD-2017-36700 漏洞编号:CVE-2017-15708 漏洞分析:https://www.javasec.cn/index.php/archives/117/ [Apache ...

  2. linux uid/euid/suid

    Each UNIX process has 3 UIDs associated to it. Superuser/root is UID=0. UID Read UID. It is of the u ...

  3. oracle 密码默认180天过期

    alter profile default limit password_life_time unlimited; alter user username identified by 'pwd';

  4. go 语言介绍

    特点1. 静态类型,编译开源语言 2. 脚本化的语法,支持多种编程范式(函数式,面向对象) 3. 原生,给力的并发支持并发编程 Go语言的优势: 1.脚本化的语法:开发效率高,容易上手 2.静态类型+ ...

  5. 将字符串str1复制为字符串str2的三种方法

    1.自己编写函数,将两个字符串进行复制 #include<iostream> using namespace std; int main(){ char str1[]="I lo ...

  6. SimpleDateFormat-多线程问题

    SimpleDateFormat-多线程问题: SimpleDateFormat类在多线程环境下中处理日期,极易出现日期转换错误的情况 import java.text.ParseException; ...

  7. mysql 5.7.11 源码安装

    mysql5.711安装 1.安装boost包下载地址http://sourceforge.net/projects/boost/files/boost/ 2.解压boost_1_59_0.tar.g ...

  8. centos 6.5安装 redis

    版本:redis-2.8.19.tar.gz 检查下面依赖是否安装,如果没有要先安装,不然会有异常. yum install gcc-c++ yum install -y tcl. .获取安装文件 r ...

  9. JavaScript 学习中

    邮箱的验证: <!DOCTYPE html><html>    <head>        <meta charset="UTF-8"&g ...

  10. 查看类属性和方法---structure