您的位置:首页 > 其它

HDU 1698 Just a Hook(区间修改,求累加和)

2016-05-14 18:49 330 查看
F - Just a Hook
Time Limit:2000MS Memory Limit:32768KB 64bit
IO Format:
%I64d & %I64u

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.


题目大意,一个区间,现在有金银铜三个牌,现在要去一段区间内的进行统一发奖牌,发过 n 次后 求总共奖牌的总价值

所以就是 线段树,区间修改,求累加和的…………。。。。。。。。。。。。。。。。。

线段树单点修改和区间修改的区别;

对于点修改和区间修改区别其实并不大,在点修改的时候,我们是找到该点,然后对该点进行加减操作,然后再一次递推修改其他的点,然后在区间修改中,有两种修改情况,一种就是和点修改一样的,找到点,修改点,递推其他点,但是,这只是其中的一点修改,这种修改方式只占用小部分。占大部分的还是对整个区间进行修改,如果该结点所表示的区间在要更改的区间内,那么就对该点直接修改,该节点加的值为
该区间每个结点要加的值得总和

代码的函数名称的功能

● PushUP(int rt)是把当前结点的信息更新到父结点

● PushDown(int rt)是把当前结点的信息更新给儿子结点

● build(int l,int r,int rt) 建立线段树

● rt表示当前子树的根(root),也就是当前所在的结点

这道题和 poj 3468 比较相同,两个代码使用同一个模板就行,并且这道题求的结果是总共的和,所以,直接求 结点 1 的值,也就是 区间 1 - n 的值就行了,这篇的注释我写的少,附个 poj 3468 的链接,这个上面的注释写的比较多

POJ 3468 链接 : /article/7677713.html 点击打开链接

附上代码:

#include <cstdio>
#include <algorithm>
using namespace std;
#define LL long long
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 111111;
int h , w , n;
int col[maxn<<2];
int sum[maxn<<2];
void PushUp(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m) {
if (col[rt]) {
col[rt<<1] = col[rt<<1|1] = col[rt];
sum[rt<<1] = (m - (m >> 1)) * col[rt];
sum[rt<<1|1] = (m >> 1) * col[rt];
col[rt] = 0;
}
}
void build(int l,int r,int rt) {
col[rt] = 0;
sum[rt] = 1;
if (l == r) return ;
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt) {
if (L <= l && r <= R) {
col[rt] = c;
sum[rt] = c * (r - l + 1);
return ;
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L , R , c , lson);
if (R > m) update(L , R , c , rson);
PushUp(rt);
}

LL query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {  //如果该点的区间在所求区间内,直接返回该值
return sum[rt];
}
//PushDown(rt , r - l + 1);  //根据该点的信息  递推修改儿子节点的信息
int m = (l + r) >> 1;
LL ret = 0;
if (L <= m) ret += query(L , R , lson);
if (m < R) ret += query(L , R , rson);
return ret;
}

int main() {
int T , n , m;
scanf("%d",&T);
for (int cas = 1 ; cas <= T ; cas ++) {
scanf("%d%d",&n,&m);
build(1 , n , 1);
while (m --) {
int a , b , c;
scanf("%d%d%d",&a,&b,&c);
update(a , b , c , 1 , n , 1);
}
printf("Case %d: The total value of the hook is %d.\n",cas , query(1,n,1,n,1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: