http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

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

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.
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
#define Lson root<<1, L, tree[root].Mid()
#define Rson root<<1|1, tree[root].Mid() + 1, R using namespace std; struct Tree
{
int L, R, e, sum;
int Mid()
{
return (L + R) / ;
}
} tree[N * ]; void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
tree[root].e = ;
if(L == R)
{
tree[root].sum = ;
return ;
} Build(Lson);
Build(Rson); tree[root].sum = tree[root<<].sum + tree[root<<|].sum;
} void Update(int root, int L, int R, int e)
{
if(tree[root].L == L && tree[root].R == R)
{
tree[root].e = e;
tree[root].sum = (tree[root].R - tree[root].L + ) * tree[root].e;
return ;
}
if(tree[root].e != )
{
tree[root<<].sum = (tree[root<<].R - tree[root<<].L + ) * tree[root].e;
tree[root<<|].sum = (tree[root<<|].R - tree[root<<|].L + ) * tree[root].e; tree[root<<].e = tree[root<<|].e = tree[root].e;
tree[root].e = ;
}
if(R <= tree[root].Mid())
Update(root<<, L, R, e);
else if(L > tree[root].Mid())
Update(root<<|, L, R, e);
else
{
Update(Lson, e);
Update(Rson, e);
}
tree[root].sum = tree[root<<].sum + tree[root<<|].sum;
} int main()
{
int t, n, m, a, b, c, x = ;
scanf("%d", &t);
while(t--)
{
x++;
scanf("%d", &n);
Build(, , n);
scanf("%d", &m);
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
Update(, a, b, c);
}
printf("Case %d: The total value of the hook is %d.\n", x, tree[].sum);
}
return ;
}

hdu 1689 Just a Hook的更多相关文章

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

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

  2. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  3. HDU 1689 Just a Hook (线段树区间更新+求和)

    Just a Hook Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible ...

  4. (线段树)Just a Hook -- hdu -- 1689

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 思路: 我的想法很简单,像上一题一样从后面向前面来算,前面已经覆盖的,后面自然不能再来计算了,具体 ...

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

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

  6. HDU 1698 Just a Hook(线段树成段更新)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目: Problem Description   In the game of DotA, P ...

  7. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和. 思路: 区间更新题目 ...

  9. HDU 1698 just a hook - 带有lazy标记的线段树(用结构体实现)

    2017-08-30 18:54:40 writer:pprp 可以跟上一篇博客做个对比, 这种实现不是很好理解,上一篇比较好理解,但是感觉有的地方不够严密 代码如下: /* @theme:segme ...

随机推荐

  1. 面试题_103_to_124_关于 OOP 和设计模式的面试题

    这部分包含 Java 面试过程中关于 SOLID 的设计原则,OOP 基础,如类,对象,接口,继承,多态,封装,抽象以及更高级的一些概念,如组合.聚合及关联.也包含了 GOF 设计模式的问题. 103 ...

  2. git cheat sheet,git四张手册图

  3. poj 1986 Distance Queries(LCA:倍增/离线)

    计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...

  4. BMP图像格式

    BMP(全称Bitmap)是Window操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),使用非常广.它采用位映射存储格式,除了图像深度可选以外,不采用其他任 ...

  5. (5)Quartz学习

    原文:http://blog.csdn.net/zxl315/article/details/10879927 介绍Quartz Quartz是一个开源的任务调度系统,它能用来调度很多任务的执行. 运 ...

  6. linux中waitpid及wait的用法

    wait(等待子进程中断或结束) 表头文件      #include<sys/types.h>      #include<sys/wait.h> 定义函数 pid_t wa ...

  7. 判断DataSet是否有数据

    if (data1.Tables[0].Rows.Count>0)        {            MessageInfoText.Text = data1.Tables[0].Rows ...

  8. HDU 5432 Pyramid Split

    题意:有n个底面是正方形的四棱锥,用一个水平截面将所有四棱锥分成两半,要求上一半体积的和等于下一半,求水平截面的高度,输出整数部分. 解法:二分截面高度.比赛的时候二分写不明白了orz…… 代码: # ...

  9. C++ Primer学习_第1章

    源文件后缀 在大多数的系统中,源文件的名字以一个后缀为结尾,后缀是由一个句点后接一个或多个字符组成的.后缀告诉系统这个文件是一个C++程序.不同编译器使用不同的后缀命名约定,最常见的包括.cc..cx ...

  10. Eclipse “Invalid Project Description” when creating new project from existing source

    1) File>Import>General>Existing Project into Workspace2) File>Import>Android>Exist ...