Just a Hook

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u


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.
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Node{
int l,r;
int lazy,sum;
}a[MAXN*]; void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=;
if(l==r){
a[rt].sum=;
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} void PushDown(int rt)
{
int mid=(a[rt].l+a[rt].r)>>;
a[rt<<].lazy=a[(rt<<)|].lazy=a[rt].lazy;
a[rt<<].sum=a[rt].lazy*(mid-a[rt].l+);
a[(rt<<)|].sum=a[rt].lazy*(a[rt].r-mid);
a[rt].lazy=;
} void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].lazy=val;
a[rt].sum=val*(r-l+);
return ;
} if(a[rt].lazy) PushDown(rt); int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid) update(rt<<,l,r,val);
else if(mid<l) update((rt<<)|,l,r,val);
else{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int n;
scanf("%d",&n);
build(,,n);
int m;
scanf("%d",&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",cas,a[].sum);
}
return ;
}

HDU1698(线段树入门题)的更多相关文章

  1. hdu 1166敌兵布阵(线段树入门题)

    >>点击进入原题测试<< 思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记.入门题 cin,cout会超时,记得加std::ios::sync_wit ...

  2. [poj2104]可持久化线段树入门题(主席树)

    解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序 ...

  3. 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40687   Accepted: 19137 ...

  4. HDU1698 线段树入门之区间修改/查询(lazy标记法)

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

  5. hiho1079 - 数据结构 线段树(入门题,离散化)

    题目链接 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho所在的学校举办社团文化节,各大社团都在宣传栏上贴起了海报,但是贴来贴去 ...

  6. Mosaic HDU 4819 二维线段树入门题

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  7. A Simple Problem with Integers(线段树入门题)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. POJ3264(线段树入门题)

    Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     ...

  9. hiho1080 - 数据结构 线段树(入门题,两个lazy tag)

    题目链接 维护区间和,两个操作:一个是将某个区间设置成一个值,一个是将某个区间增加一个固定值 /**************************************************** ...

随机推荐

  1. php本周、本月的第一天 / 最后一天的时间

    //week $time1 = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, ...

  2. 【python】-- 装饰器、迭代器、生成器

    装饰器 装饰器本质是函数,是用来装饰其他函数,顾名思义就是,为其他的函数添加附件功能的. 一.装饰器原则: 不能修改被装饰函数的源代码 不能修改被装饰函数的调用方式 def logging(): pr ...

  3. ASP向上取整

    <%Function Ceil(value)    Dim return    return = int(value)    Cei2=value-return    if Cei2>0 ...

  4. 洛谷 4035 [JSOI2008]球形空间产生器

    题目戳这里 一句话题意 给你 n+1 个 n 维点,需要你求出这个n维球的球心.(n<=10) Solution 这个题目N维的话确实不好想,反正三维就已经把我搞懵了,所以只好拿二维类比. 首先 ...

  5. HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 ...

  6. pg 和sql server 分别如何新建二进制数据库字段以及插入二进制数据的sql语句

    PG create table demo ( id int, name bytea ); Insert into demo (id,name)values(256,pg_read_binary_fil ...

  7. 关于js中undefined的判断

    在开发中遇到一个情景,当添加用户的时候不需要传入用户id,如果是修改那么需要传入id,因为用的是angular框架,参数是早就定义好了的,那么在新增用户的时候就会出现undefined的情况,之前我一 ...

  8. HTML5离线存储和本地缓存

    一.离线存储 有一个web应用有三个文件index.html,a.js,b.css,现在需要把js和css文件缓存起来 1.在index.html里加上<html manifest=" ...

  9. oracle字符串函数总结

    字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分 返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库 ...

  10. HIVE 总结

    http://blog.csdn.net/wisgood/article/details/17186181 常见错误 http://blog.csdn.net/sunnyyoona/article/d ...