注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

东月之神

在单纯的观念里面,生命就容易变得比较深刻!

 
 
 

日志

 
 
关于我

别驻足,梦想要不停追逐,别认输,熬过黑暗才有日出,要记住,成功就在下一步,路很苦,汗水是最美的书!

网易考拉推荐

hdu1698 Just a Hook  

2010-05-08 17:02:03|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2404    Accepted Submission(s): 1045


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.

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.
 

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.
 
题意很好理解。更新+求和。
代码如下:
#include <cstdio>
struct Node
{
 int l, r;
 int ans;
}tree[100000*3+1];
inline void make_tree(int left, int right, int now)
{
 tree[now].l = left, tree[now].r = right;
 tree[now].ans  = 1;
 if(left == right)
  return;
 int mid = (left + right) >> 1;
 make_tree(left, mid, now*2);
 make_tree(mid+1, right, now*2+1);
}
inline void update(int left, int right, int now, int n)
{
 if(tree[now].ans == n) return;
 if(tree[now].l == left && tree[now].r  == right)
 {
  tree[now].ans = n;
  return;
 }
 int mid = (tree[now].l + tree[now].r) >> 1;
 if(tree[now].ans != 0)
  tree[now*2].ans = tree[now*2+1].ans = tree[now].ans;
 tree[now].ans = 0;
 if(right <= mid)
  update(left, right, now<<1, n);
 else if(left > mid)
  update(left, right, (now<<1)+1, n);
 else
 {
  update(left, mid, (now<<1), n);
  update(mid+1, right, (now<<1)+1, n);
 }
}
inline int count(int left, int right, int now)
{
 if(tree[now].ans >= 1) return (right - left + 1) * tree[now].ans;
 return count(left, tree[now*2].r, now*2) + count(tree[now*2+1].l, right, now*2+1);
}
int main()
{
 int t, n, i, m, ii;
  scanf("%d", &t);
  for(ii = 1; ii <= t; ii++)
  {
    scanf("%d%d", &m, &n);
    make_tree(1, m, 1);
    int a, b, v; 
    for(i = 1; i <= n; i++)
    {
      scanf("%d%d%d", &a, &b, &v);
      update(a, b, 1, v);   
    }
    printf("Case %d: The total value of the hook is %d.\n", ii, count(1, m, 1));
  }
return 0;
}
  评论这张
 
阅读(381)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017