HDU1698 Just a Hook (区间更新)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23644 Accepted Submission(s): 11839
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 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.
each case, print a number in a line representing the total value of the
hook after the operations. Use the format in the example.
10
2
1 5 2
5 9 3
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 110000
int tre[maxn*];
int lazy[maxn*];
void build(int num, int le, int ri)
{
if(le == ri)
{
tre[num] = ;
return;
}
int mid = (le+ri)/;
build(num*, le, mid);
build(num*+, mid+, ri);
tre[num] = tre[num*] + tre[num*+];
}
void pushdown(int num, int le, int mid, int ri)
{
if(lazy[num]!=)
{
lazy[num*] = lazy[num];
lazy[num*+] = lazy[num];
tre[num*] = (mid-le+)*lazy[num*];
tre[num*+] = (ri-mid)*lazy[num*+];
lazy[num] = ;
}
} void update(int num, int le, int ri, int x, int y, int val)
{
if(x<=le && y>=ri)
{
tre[num] = (ri-le+)*val;
lazy[num] = val;
return;
}
int mid = (le+ri)/;
pushdown(num,le,mid,ri);
if(x<=mid)
update(num*,le,mid,x,y,val);
if(y>mid)
update(num*+,mid+,ri,x,y,val);
tre[num] = tre[num*] + tre[num*+];
}
int main()
{
int t;
scanf("%d", &t);
int cas = ;
while(t--)
{
int n;
scanf("%d", &n);
build(,,n);
memset(lazy, , sizeof lazy);
int m;
scanf("%d", &m);
for(int i = ; i < m; i++)
{
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
update(,,n,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n", ++cas, tre[]);
}
return ;
}
HDU1698 Just a Hook (区间更新)的更多相关文章
- hdu1698线段树的区间更新区间查询
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu1698 线段树(区间更新~将区间[x,y]的值替换为z)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook 区间更新 lazy标记
lazy标记 #include <iostream> #include <cstdio> #include <cstring> #include <sstre ...
- HDU1698 线段树(区间更新区间查询)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- HDU1698:Just a Hook(线段树区间更新)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- hdu1698 线段树区间更新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 线段树---成段更新hdu1698 Just a Hook
hdu1698 Just a Hook 题意:O(-1) 思路:O(-1) 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) 题意:给一组棍子染色,不同 ...
随机推荐
- hdu2817之整数快速幂
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 解决IE6下DIV无法实现1px高度问题
2.多加一个line-height:1px的属性,不过得在DIV里多加一个 ,也就是空格,以下为引用的内容: <styletypestyletype="text/css"&g ...
- Python获取并输出当前日期时间
取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年 ...
- 细说php(六) 数组
一.数组概述 1.1 数组是复合类型 1.2 数组中能够存储随意长度的数据, 也能够存储随意类型的数据 二.数组的类型 2.1 索引数组: 下标是顺序整数作为索引 <?php $user[0] ...
- 使用C++11实现无锁stack(lock-free stack)
前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 — 有时候是很多时 ...
- [计算机组成原理][实验十.R-I-J型指令CPU设计实验总结]
总算解决一大心头之患了,比想象中容易,通宵两夜,刷完了十个实验,这个实验就是最后的了.感慨颇多.特地写篇总结. 想做一件事,就立马去做把.你会发现没那么困难,往往最大的困难,是心里的困难. 培养了HD ...
- 【转】深入理解篇UIScrollerView
转自:http://www.mamicode.com/info-detail-1144770.html 接下来,我整理一下自己的思路,深入理解 UIScrollView 基本点 : 1 . UIScr ...
- ajaxFileUpload 注意!
后台context.Response.ContentType = "text/html";
- 加载gif图过渡效果
加载gif图片,过渡效果: 调用: - (id)initWithGifView:(UIView *)view { self = [super initWithView:view]; if (self) ...
- cocos2dx 碰撞检测
//必须 要有float类型的参数 void MainScene::updateFrame(float dt) { if (spriteTest != NULL && spriteTe ...