您的位置:首页 > 其它

hdu 1698 Just a Hook (线段树)

2016-08-28 13:25 267 查看
题目链接:hdu 1698

Just a Hook

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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.

区间更新,明显的线段树。

这也是第一次手写线段树的区间更新,代码不够简洁,仅作参(纪)考(念)用(当然前提是有人看)……

题目直接求的是总和,因此写的时候省略了query函数和pushDown函数,直接输出根节点的值。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
#define PI acos(-1)
#define M 100005

using namespace std;

struct node
{
int l, r, v, sign;

int mid()
{
return l + ((r - l) >> 1);
}
}tree[M * 4];

void build(int l, int r, int x)//建树+初始化。
{
tree[x].l = l;
tree[x].r = r;
tree[x].v = 1;
tree[x].sign = 0;

if(l == r)  return;

int mid = tree[x].mid();
build(l, mid, x << 1);
build(mid + 1, r, x << 1 | 1);

tree[x].v = tree[x << 1].v + tree[x << 1 | 1].v;//把这行代码去掉试了一下,也能过,时间889ms。究其原因是杭电数据没测Q == 0的情况。
}

void update(int l, int r, int x, int change)//区间更新
{
if(l <= tree[x].l && r >= tree[x].r)
{
tree[x].sign = change;
return;
}

if(tree[x].sign)
{
tree[x << 1].sign = tree[x << 1 | 1].sign = tree[x].sign;
tree[x].sign = 0;
}

int mid = tree[x].mid();

if(mid >= l)    update(l, r, x << 1, change);
if(mid < r)     update(l, r, x << 1 | 1, change);

if(tree[x].l != tree[x].r)//pushUp部分
{
tree[x].v = tree[x << 1].v + tree[x << 1 | 1].v;
if(tree[x << 1].sign)
tree[x].v += tree[x << 1].sign * (tree[x << 1].r - tree[x << 1].l + 1) - tree[x << 1].v;
if(tree[x << 1 | 1].sign)
tree[x].v += tree[x << 1 | 1].sign * (tree[x << 1 | 1].r - tree[x << 1 | 1].l + 1) - tree[x << 1 | 1].v;
}
}

int main()
{
int T;//case数
scanf("%d", &T);
for(int i = 1; i <= T; i++)
{
int N, Q, x, y, z;//各字母含义对应题目
scanf("%d %d", &N, &Q);

build(1, N, 1);

while(Q--)
{
scanf("%d %d %d", &x, &y, &z);
update(x, y, 1, z);
}

printf("Case %d: The total value of the hook is %d.\n", i, tree[1].v);
}
return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树 hdu