Just a Hook

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

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.
 题意:输入t代表有t组测试数据,然后一个数字n代表n个英雄,接下来数字q代表接下来有q行,每行三个整数a,b,c,代表将区间a到b的值全部换为c,最后问总分数,注意  初始分数为1
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 100100
#define INF 0x3f3f3f
using namespace std;
int sum[MAX<<2];
int change[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int m)
{
if(change[o])
{
change[o<<1]=change[o<<1|1]=change[o];
sum[o<<1]=change[o]*(m-(m>>1));
sum[o<<1|1]=change[o]*(m>>1);
change[o]=0;
}
}
void gettree(int o,int l,int r)
{
sum[o]=1;change[o]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int L,int R,int v)
{
if(L<=l&&R>=r)
{
change[o]=v;
sum[o]=v*(r-l+1);
return ;
}
pushdown(o,r-l+1);
int mid=(r+l)>>1;
if(L<=mid)
update(o<<1,l,mid,L,R,v);
if(R>mid)
update(o<<1|1,mid+1,r,L,R,v);
pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return sum[o];
}
pushdown(o,r-l+1);
int ans=0;
int mid=(r+l)>>1;
if(L<=mid)
ans+=find(o<<1,l,mid,L,R);
if(R>mid)
ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
int main()
{
int t,k,i,j;
int n,m;
k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int a,b,c;
gettree(1,1,n);
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}
printf("Case %d: The total value of the hook is ",k++);
printf("%d.\n",find(1,1,n,1,n));
}
return 0;
}

  

hdoj 1698 Just a Hook【线段树区间修改】的更多相关文章

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

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

  2. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  3. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  4. [HDU] 1698 Just a Hook [线段树区间替换]

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

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

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  6. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

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

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

  8. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  9. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  10. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

随机推荐

  1. NFS,FTP

    一. NFS1. NFS简介NFS全称是network file systemNFS允许一个系统在网络上与他人共享目录和文件.通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件. 假 ...

  2. AMAZON PRICE TRACKER, AMAZON PRICE HISTORY, AMAZON PRICE DROP ALERT | DROPGG.COM

    DropGG.com is the destination for savvy shoppers looking to save money by buying smart. DropGG.com a ...

  3. grub命令来引导linux

    由于对linux系统的好奇,想按在机器上玩玩.昨天忙活了一晚上,最终才把linux安装好.但高兴的有点太早了,我还以为进linux就像进 windows那么简单哪,没有想到却蹦出来一个引导命令(gru ...

  4. 在 IIS MIME 类型中添加 md 扩展名

    最近在了解 Knowledge Base (知识库)的内容,对两个平台比较感兴趣,一个是 Raneto,一个是 MDwiki,两者都是使用md文件作为内容存储. 需要注意的是,使用IIS部署网站后,需 ...

  5. 类集对enum的支持。

    1,EmumMap public class EnumMap<K extends Enum<K>,V>extends AbstractMap<K,V>impleme ...

  6. 23个.NET开源项目

    Castle是.NET里走过了三年的开源框架,下载地址如:http://www.castleproject.org/index.html ,当然如果你是从事过JAVA开发并用过spring,hiber ...

  7. nump中的为随机数产生器的seed

    在python的程序中,发现了如下的伪随机数产生的代码 rng = numpy.random.RandomState(23355) arrayA = rng.uniform(0,1,(2,3)) 该段 ...

  8. struts2文件下载出现Can not find a java.io.InputStream with the name的错误

    今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...

  9. POJ_2446_Chessboard

    题目意思就是一个M*N的有洞棋盘棋盘上,用1*2的板子去覆盖没有洞的地方,要求板子不能重叠,最终能否将棋盘完整覆盖. 代码: #include<stdio.h> #include<s ...

  10. linux使用ps1设置命令行提示符

    要自定义命令行提示,需要ps1来设置.完成自定义需要以下几个步骤: 1.编辑~/.bashrc文件 vi ~/.bashrc 2.在.bashrc文件中添加以下一行自定义内容 export PS1=& ...