Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29106    Accepted Submission(s): 14396

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
 
Recommend
wangye

#include<bits/stdc++.h>
#define N 100005
#define lson i*2,l,m
#define rson i*2+1,m+1,r
#define ll long long
using namespace std;
long long sum[N*];
long long setv[N*];
void pushup(int i)
{
sum[i]=sum[i*]+sum[i*+];
} void pushdown(int i,int num)
{
if(setv[i])
{
sum[i*]=setv[i]*(num-num/);
sum[i*+]=setv[i]*(num/);
setv[i*]=setv[i];
setv[i*+]=setv[i];
setv[i]=;
}
} void build(int i,int l,int r)
{
setv[i]=;
sum[i]=;
if(l==r)
return ;
int m=(l+r)/;
build(lson);
build(rson);
pushup(i);
} void update(int ql,int qr,int val,int i,int l,int r)
{
if(ql<=l&&r<=qr)
{
setv[i]=val;
sum[i]=val*(r-l+);
return ;
}
pushdown(i,r-l+);
int m=(l+r)/;
if(m>=ql) update(ql,qr,val,lson);
if(m<qr) update(ql,qr,val,rson);
pushup(i);
} ll query(int ql,int qr,int i,int l,int r)
{
//cout<<"l="<<l<<" r="<<r<<endl;
if(ql<=l&&r<=qr)
{
return sum[i];
}
pushdown(i,r-l+);
int m=(l+r)/;
ll cur=;
if(m>=ql) cur+=query(ql,qr,lson);
if(m<qr) cur+=query(ql,qr,rson);
//cout<<cur<<endl;
return cur;
} int t,n,q;
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
scanf("%d",&t);
for(int Case=;Case<=t;Case++)
{
scanf("%d",&n);
build(,,n);
//for(int i=1;i<18;i++)
// cout<<sum[i]<<" ";
//cout<<endl;
int a,b,c;
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
//cout<<a<<" "<<b<<" "<<c<<endl;
update(a,b,c,,,n);
}
printf("Case %d: The total value of the hook is %lld.\n",Case,query(,n,,,n));
}
return ;
}

Just a Hook(区间set)的更多相关文章

  1. HDU1698 Just a Hook (区间更新)

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

  2. HDU 1698 Just a Hook 区间更新 lazy标记

    lazy标记 #include <iostream> #include <cstdio> #include <cstring> #include <sstre ...

  3. 线段树入门&lazy思想

    线段树将区间分成若干个子区间,子区间又继续分,直到区间为一个点(区间左值等于右值) 对于父区间[a,b],其子区间为[a,(a+b)/2]和[(a+b)/2+1,b] 用于求区间的值,如区间最值.区间 ...

  4. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  5. HDU 1698 Just a Hook(线段树/区间更新)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description In the g ...

  6. HDU1698Just a Hook(线段树 + 区间修改 + 求和)

    题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...

  7. hdu 1698:Just a Hook(线段树,区间更新)

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

  8. HDU 1698 Just a Hook(线段树区间替换)

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

  9. HDU 1698 Just a Hook (线段树区间更新)

    题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...

  10. HDU 1698 (线段树 区间更新) Just a Hook

    有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...

随机推荐

  1. GCD之Apply

    dispatch_apply函数是dispatch_sync函数和dispatch_group的结合体.该函数将按指定的次数将指定的block追加到指定的dispatch queue中,并等待全部处理 ...

  2. 翻译 | 一行 JavaScript 代码的逆向工程

    原文地址:Reverse Engineering One Line of JavaScript 原文作者:Alex Kras 译者:李波 校对者:冬青.小萝卜 几个月前,我看到一个邮件问:有没有人可以 ...

  3. xml跟sql查找

    xml小白笔记 ....... <sql id="wDishesColumns"> a.id AS "id", a.pid AS "pid ...

  4. 树状数组初步_ZERO

    原博客:树状数组 1 一维树状数组 1 什么是树状数组        树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组A[1..n],那么查询A[1]+-+A[n]的时,间是log级 ...

  5. SSM之整合Redis

    Redis安装与使用 第一步当然是安装Redis,这里以Windows上的安装为例. 首先下载Redis,可以选择msi或zip包安装方式 zip方式需打开cmd窗口,在解压后的目录下运行redis- ...

  6. 超级密码 hdu1226 bfs

    超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. PBOC圈存时用到3DES加密解密以及MAC计算方法

    最近在做PBOC圈存时用到了3DES的加密解密以及MAC计算问题,在网上了解一些知识,复制了一些demo学习,我这里没有深入研究,只是把我用到的和了解的做个总结,便于以后使用和学习. 3DES分双倍长 ...

  8. JS - Function 之 Arguments

    Arguments 函数的参数构成的数组 描述 只定义在函数体内,函数体内arugments指代Arguments对象,该对象是类数组对象,有数组属性可以当做数组使用,含有传入该函数的所有参数,aru ...

  9. 选择排序的3种语言实现方法(C java python)

    1.选择排序的思路是:遍历数组,第一遍找出所有成员的最小值,放到数组下标为0的位置,第二遍从剩余内容中,再次找出最小值,放到数组下标为1的位置,以此类推,遍历完成所有的数组内容,最后结果就是:数组是按 ...

  10. DevOps之域名

    唠叨话 关于德语噢屁事的知识点,仅提供精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. 域名系统DNS(Domain Name System) 关于域名,知识与技能的层次(知道.理解.运用) ...