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. oracle .bash_profile

    [oracle@redhat4 ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashr ...

  2. 8天学通MongoDB——第四天 索引操作

    这些天项目改版,时间比较紧,博客也就没跟得上,还望大家见谅. 好,今天分享下mongodb中关于索引的基本操作,我们日常做开发都避免不了要对程序进行性能优化,而程序的操作无非就是CURD,通常我们 又 ...

  3. IIS与ASP.NET中的队列

    一.IIS:应用程序池队列(Application pool queue,位于HTTP.SYS) 这是请求到达IIS后遇到的第一个队列,http.sys收到请求后会将请求放入对应的应用程序池队列,这样 ...

  4. [转] jQuery Infinite Ajax Scroll(ias) 分页插件介绍

    原文链接:http://justflyhigh.com/index.php/articlec/index/index.php?s=content&m=aticle&id=91 Infi ...

  5. 任E行M1端口比特率特征码

    分辨率:800x480gps端口:com1比特率:4800设备特征码:01D1D008内存:128M

  6. busybox filesystem add ldd function

    /******************************************************************** * busybox filesystem add ldd f ...

  7. acdream 1210 Chinese Girls' Amusement (打表找规律)

    题意:有n个女孩围成一个圈从第1号女孩开始有一个球,可以往编号大的抛去(像传绣球一样绕着环来传),每次必须抛给左边第k个人,比如1号会抛给1+k号女孩.给出女孩的人数,如果他们都每个人都想要碰到球一次 ...

  8. DataTables ajax重新加载数据

    传数据给后台返回数据,最开始的办法是 重新生成一个datatable对象,但是在每次点击刷新时都会有闪动的现象,而且代价很高.理想中应该仅仅更新数据. 最后在文档中查到一个插件fnReloadAjax ...

  9. Mysql,SqlServer,Oracle主键自动增长的设置

    1.把主键定义为自动增长标识符类型 MySql 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: )); insert into customers ...

  10. 编译安装lnmp

    http://wenku.baidu.com/view/ec45d5dd28ea81c758f578cc.html