HDU 1698 【线段树,区间修改 + 维护区间和】

题目链接 HDU 1698

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.

大概题意:

一条长度为N得链子, 有Q次操作,每次操作把区间 X~Y 的金属换成 Z;有金,银,铜三种金属分别对应3,2,1三种价值。

求Q次操作后,求整条链子的总价值。

思路:

成段更新,区间求和,本次用一维数组表示法建线段树

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, k<<1 ///左儿子
#define rson m+1, r, k<<1|1 ///右儿子
using namespace std; const int MAXN = ; int st[MAXN<<]; ///st数组里面记录的是标记 0,1, 2, 3; 0 代表杂色
int flag; void down(int &k)
{
st[k<<] = st[k<<|] = st[k]; ///左儿子和右儿子均设为该颜色
st[k] = ; ///设为杂色 ??避免后面重复操作??
} void build(int l, int r, int k) ///建立线段树,一开始都初始化为 1
{
st[k] = flag;
if(l == r) return;
int m = (l+r)>>;
build(lson);
build(rson);
} void update(int &L, int &R, int l, int r, int k) ///更新L--R,要从1--N, st[1] 开始
{
if(L <= l && R >= r) ///所在区间在更新区间内
{
st[k] = flag; ///整段更新为新颜色
return;
}
if(st[k]) ///如果该结点为纯色,则左右儿子颜色也为该颜色
{
down(k);
}
int m = (l+r)>>;
if(L <= m) update(L, R, lson);
if(R > m) update(L, R, rson);
} int query(int l, int r, int k) ///查询区间颜色之和
{
if(st[k]) return st[k]*(r-l+); ///如果该段为纯色,则成段想乘得结果 int m = (l+r)>>, t1, t2;
t1 = query(lson);
t2 = query(rson);
return t1+t2;
} int main()
{
int T, N, t = ;
int L, R, q;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &q);
flag = ;
build(, N, ); ///建立线段树
while(q--)
{
scanf("%d%d%d", &L, &R, &flag);
update(L, R, , N, );
}
printf("Case %d: The total value of the hook is %d.\n", t++, query(, N, ));
}
return ;
}
上一篇:python 之路,Day27 - 主机管理+堡垒机系统开发


下一篇:HZAU 1207 Candies(线段树区间查询 区间修改)