HDU1698 Just a Hook

来源:互联网 发布:floyd算法流程 编辑:程序博客网 时间:2024/05/18 03:55

题目:

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

11021 5 25 9 3

Sample Output

Case 1: The total value of the hook is 24.

因为本题初始化比较简单,所以没有用build函数

代码:

#include<iostream>using namespace std;int n;int sum[400000];int lazy[400000];void down(int key, int low, int high){int mid = (low + high) / 2;sum[key * 2] = (mid - low + 1)*lazy[key];lazy[key * 2] = lazy[key];sum[key * 2 + 1] = (high - mid)*lazy[key];lazy[key * 2 + 1] = lazy[key];lazy[key] = 0;}void update(int key, int low, int high, int x, int y, int value){if (low == x && high == y){sum[key] = (high - low + 1)*value;lazy[key] = value;return;}if (lazy[key])down(key, low, high);int mid = (low + high) / 2;if (mid < x)update(key * 2 + 1, mid + 1, high, x, y,value);else if (mid >= y)update(key * 2, low, mid, x, y, value);else{update(key * 2, low, mid, x, mid, value);update(key * 2 + 1, mid + 1, high, mid + 1, y, value);}sum[key] = sum[key * 2] + sum[key * 2 + 1];}int query(int key, int low, int high, int x, int y){if (low == x && high == y)return sum[key];down(key, low, high);int mid = (low + high) / 2;if (mid < x)return query(key * 2 + 1, mid + 1, high, x, y);if (mid >= y)return query(key * 2, low, mid, x, y);   return query(key * 2, low, mid, x, mid) + query(key * 2 + 1, mid + 1, high, mid + 1, y);}int main(){int t, q, x, y, laz;cin >> t;for (int cas = 1; cas <= t; cas++){cin >> n >> q;sum[1] = n;lazy[1] = 1;for (int i = 1; i <= q; i++){scanf("%d%d%d", &x, &y, &laz);update(1, 1, n, x, y, laz);}printf("Case %d: The total value of the hook is %d.\n", cas,query(1,1,n,1,n));}return 0;}
因为这个题目的查询比较特殊,所以query其实也不需要。


2 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 使用虚假广告词被工商查到怎么办 为什么手机淘宝店关注不了怎么办 淘宝买东西付钱后卖家不发货怎么办 微信销售群没人买东西怎么办 淘宝想开2个店铺怎么办 拼多多没钱交保证金怎么办 差评一个月后怎么办 淘宝买家投诉成立后怎么办 天猫投诉成立了怎么办 新开实体店铺没生意怎么办 新开淘宝没流量怎么办 白色板鞋有青草弄的绿色怎么办 淘宝租衣服不退押金怎么办 c盘不可以扩展怎么办 实践教学管理平台忘记账号怎么办 淘宝申请售后店家不处理怎么办 全民k歌地区限制怎么办 网络电视hdp无法下载怎么办 淘宝店铺销量为零怎么办 快递号填错卖家拒绝退款申请怎么办 淘宝店代购 售假怎么办 淘宝申请售后卖家没钱怎么办 淘宝禁止评价一个月怎么办 被卖家电话骚扰怎么办 恶意骚扰扣12分怎么办 新店开张交保证金被骗了怎么办 支付宝蚂蚁花呗逾期怎么办 被注销的微信怎么办 花呗有些不能用怎么办 实体店生意不好做怎么办 电器实体店生意越来越差怎么办 开业第一天不吉利怎么办 美容店开业第一天没人怎么办 淘宝店铺没有人访问怎么办 淘宝店铺没有人问怎么办 淘宝申请退款后店铺关闭怎么办 宝贝详情怎么改不了怎么办 改详情页后被删除宝贝怎么办 淘宝网商贷生意不好还不了怎么办 英国遗失在酒店物品怎么办 班福法则首位是0怎么办