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. 第三章 Java内存模型(下)

    锁的内存语义 中所周知,锁可以让临界区互斥执行.这里将介绍锁的另一个同样重要但常常被忽视的功能:锁的内存语义 锁的释放-获取建立的happens-before关系 锁是Java并发编程中最重要的同步机 ...

  2. HDLM命令dlnkmgr详解之五_set

    set命令用来设置HDLM的操作环境及参数. set操作设置的大部分是dlnkmgr view -sys命令中显示的参数值 命令格式 dlnkmgr set { -lb on [ -lbtype { ...

  3. 34款Firefox渗透测试插件

    1:Firebug Firefox的 五星级强力推荐插件之一,不许要多解释 2:User Agent Switcher 改变客户端的User Agent的一款插件 3:Hackbar 攻城师必备工具, ...

  4. spring-初始化bean

    @EnableScheduling@Servicepublic class BaseTask implements InitializingBean, ApplicationContextAware{ ...

  5. sublime3 There are no packages available for installation

    我的是网上下载的绿色版 1.找到sublime\Data\Packages  删除Packages control相关的文件夹和文件 下载https://packagecontrol.io/Packa ...

  6. solr replication原理探究

    原文出自:http://sbp810050504.blog.51cto.com/2799422/1423199 无论是垂直搜索,还是通用搜索引擎,对外提供搜索服务其压力都比较大,经常有垂直电商在做活动 ...

  7. Nginx 正向代理和反向代理

    正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器这个代理服务器呢,他能访问那个我不能访问的网站于是我先连上代 ...

  8. Codeforces #505(div1+div2) B Weakened Common Divisor

    题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...

  9. 02 mybatis环境搭建 【spring + mybatis】

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  10. jqgrid列动态加载

    private void InitGrid(string entityName) { Session["entityName"] = entityName; ArrayList c ...