HDU1698 Just a Hook —— 线段树 区间染色

题目链接:https://vjudge.net/problem/HDU-1698

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.

HDU1698 Just a Hook  —— 线段树 区间染色

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. 

InputThe 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. 
OutputFor 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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int sum[MAXN<<], setv[MAXN<<]; void push_up(int u)
{
sum[u] = sum[u*] + sum[u*+];
} void push_down(int u, int l, int r)
{
if(setv[u]>)
{
setv[u*] = setv[u*+] = setv[u];
sum[u*] = (r-l++)/*setv[u];
sum[u*+] = (r-l+)/*setv[u];
setv[u] = ;
}
} void build(int u, int l, int r)
{
if(l==r)
{
setv[u] = ; //每个鱼钩初始化为金属1
sum[u] = ;
return;
} int mid = (l+r)>>;
build(u*, l, mid);
build(u*+, mid+, r);
push_up(u);
setv[u] = ;
} void set_val(int u, int l, int r, int x, int y, int val)
{
if(x<=l && r<=y)
{
sum[u] = (r-l+)*val;
setv[u] = val;
return;
} push_down(u, l, r);
int mid = (l+r)>>;
if(x<=mid) set_val(u*, l, mid, x, y, val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, val);
push_up(u);
} int query(int u, int l, int r, int x, int y)
{
if(x<=l && r<=y)
return sum[u]; push_down(u, l, r);
int ret = ;
int mid = (l+r)>>;
if(x<=mid) ret += query(u*, l, mid, x, y);
if(y>=mid+) ret += query(u*+, mid+, r, x, y);
return ret;
} int main()
{
int T, n, m;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &m); build(, , n);
for(int i = ; i<=m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
set_val(, , n, x, y, z);
} int ans = query(, , n, , n);
printf("Case %d: The total value of the hook is %d.\n", kase, ans);
}
}
上一篇:【网络安全】【02】Windows系统如何屏蔽自动更新


下一篇:关于Node.js安装及环境配置之Windows篇