Just a Hook HDU
来源:互联网 发布:thinkphp分销系统源码 编辑:程序博客网 时间:2024/06/05 12:41
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.
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.
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.
11021 5 25 9 3
Case 1: The total value of the hook is 24.
思路 : 样例较多,并且只有更新,因此不能彻底更新线段树,只需要适当修改颜色。
#include <cstdio>#include <queue>#include <cstring>#include <climits>using namespace std;const int maxn = 100100;typedef struct node{ int l,r,c;}node;node T[maxn*4];void build_tree(int l,int r,int i){ T[i].l = l; T[i].r = r; T[i].c = 1; if(l == r) { return; } int mid = (l+r) / 2; build_tree(l,mid,i*2); build_tree(mid+1,r,i*2+1);}void update_tree(int l,int r,int i,int mark){ if(mark == T[i].c) return; // 同色返回。 if(T[i].l == l && T[i].r == r) // 区间吻合,直接修改颜色。 { T[i].c = mark; return; } if(T[i].c>0){ //由上个判断,[l,r]属于[Tl,Tr],必须先把子树c修改,完成延迟标记。 T[i*2].c=T[i*2+1].c=T[i].c; T[i].c=-1; } int mid = (T[i].l + T[i].r) / 2; if(r <= mid) update_tree(l,r,i*2,mark); else if(l > mid) update_tree(l,r,i*2+1,mark); else { update_tree(l,mid,i*2,mark); update_tree(mid+1,r,i*2+1,mark); } if(T[i*2].c == T[i*2+1].c) T[i].c = T[i*2].c; else T[i].c = -1;}int query(int i){ if(T[i].c>0) return (T[i].r-T[i].l+1) * T[i].c; else return query(2*v)+query(2*v+1);}int main(){ int t; int n,q; int k = 1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&q); build_tree(1,n,1); while(q--) { int l,r,mark; scanf("%d%d%d",&l,&r,&mark); update_tree(l,r,1,mark); } int ANS = query(1); printf("Case %d: The total value of the hook is %d.\n",k++,ANS); } return 0;}
阅读全文
1 0
- hdu Just a Hook
- HDU Just a Hook
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook (HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- Just a Hook HDU
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- Hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 just a hook
- hdu 1698 Just a Hook
- linux系统升级python到2.7.13
- 求二叉树叶子节点的个数+求二叉树第k层的节点个数
- jquery源码解析(第3章元素之坐标算法)
- [日常训练] 压缩
- 出差记录
- Just a Hook HDU
- Linux下jenkins改端口、解决内存溢出、版本升级
- OpenStack新组件打包
- 数据结构从零单排1——顺序表
- E
- JavaScript严格模式约束规则
- C++中string类下的begin,end,rbegin,rend的用法
- JSP小项目实战---客户信息管理系统
- python连接MySql