博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Just a Hook HDU - 1698
阅读量:4641 次
发布时间:2019-06-09

本文共 3567 字,大约阅读时间需要 11 分钟。

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

11021 5 25 9 3

Sample Output

Case 1: The total value of the hook is 24. 最简单的区间更新。
1 #pragma warning(disable:4996) 2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 9 #define lson root<<110 #define rson root<<1|111 #define ll long long 12 13 const int maxn = 100005;14 15 int n, q, T;16 17 struct node { int l, r, sum, add; } Tree[4 * maxn];18 19 void Pushup(int root) {20 Tree[root].sum = Tree[lson].sum + Tree[rson].sum;21 }22 23 void Pushdown(int l,int r,int root) {24 int mid = (l + r) >> 1;25 Tree[lson].add = Tree[root].add;26 Tree[rson].add = Tree[root].add;27 Tree[lson].sum = Tree[root].add * (mid - l + 1);28 Tree[rson].sum = Tree[root].add * (r - mid);29 Tree[root].add = 0;30 }31 32 void Build(int l, int r, int root) {33 Tree[root].l = l;34 Tree[root].r = r;35 Tree[root].add = 0;36 if (l == r) {37 Tree[root].sum = 1;38 return;39 }40 int mid = (l + r) >> 1;41 Build(l, mid, lson);42 Build(mid + 1, r, rson);43 Pushup(root);44 }45 46 void Update(int L, int R, int l, int r, int root, int x) {47 if (l > R || r < L) return;48 if (L <= l && r <= R) {49 Tree[root].add = x;50 Tree[root].sum = x * (r - l + 1);51 return;52 }53 if (Tree[root].add) Pushdown(l, r, root);54 int mid = (l + r) >> 1;55 Update(L, R, l, mid, lson, x);56 Update(L, R, mid + 1, r, rson, x);57 Pushup(root);58 }59 60 61 62 int main()63 {64 scanf("%d", &T);65 for (int t = 1; t <= T; t++) {66 scanf("%d", &n);67 Build(1, n, 1);68 scanf("%d", &q);69 for (int i = 1; i <= q; i++) {70 int x, y, d;71 scanf("%d%d%d", &x, &y, &d);72 Update(x, y, 1, n, 1, d);73 }74 printf("Case %d: ", t);75 printf("The total value of the hook is %d.\n", Tree[1].sum);76 }77 return 0;78 }

 

转载于:https://www.cnblogs.com/zgglj-com/p/8847261.html

你可能感兴趣的文章
Sql Server 排序规则字符集的冲突问题
查看>>
七大内排序
查看>>
安装rcssmin方法
查看>>
创建、删除表
查看>>
CI接收post与get传输数据
查看>>
禁用缓存
查看>>
显示和编辑注解
查看>>
ArcGIS AddIN之工具不可用
查看>>
配置错误,无法识别的配置节 system.serviceModel 解决方案
查看>>
oc学习
查看>>
Java生鲜电商平台-Linux服务器常用升级的基础包
查看>>
聊天室(C++客户端+Pyhton服务器)2.基本功能添加
查看>>
swift中try
查看>>
缓存框架Ehcache相关
查看>>
用Instant client批量安装Oracle客户端-安装配置
查看>>
Fireworks为枝繁叶茂的树木图片抠底
查看>>
iphone 开发学习整理
查看>>
自我介绍
查看>>
Prof. Dr. Ligang Liu (刘利刚) 中国科技大学
查看>>
centos 升级openssl
查看>>