Just a Hook

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

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.
 
Source
 
 
题目意思:
给一个长度为n的数组,q次操作,每次操作为l, r, w   即把l-r区间数组元素变为w,初始化数组所有元素都为1
 
思路:
线段树区间更新,记录颜色
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 100005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n; struct node{
int l, r, val;
bool f;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].val=;
if(l==r){
return;
}
build(l,mid,ll);
build(mid+,r,rr);
} void update(int l,int r,int val,int root){
if(a[root].val==val) return;
if(a[root].l==l&&a[root].r==r){
a[root].val=val;
return;
}
if(a[root].val){
if(a[root].l!=a[root].r) {
a[ll].val=a[rr].val=a[root].val;
}
}
if(l>=a[rr].l) update(l,r,val,rr);
else if(r<=a[ll].r) update(l,r,val,ll);
else {
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
if(a[ll].val==a[rr].val) a[root].val=a[ll].val;
else a[root].val=;
} int get_sum(int root){
if(a[root].val) return a[root].val*(a[root].r-a[root].l+);
return get_sum(ll)+get_sum(rr);
} main()
{
int q, i, j, k, x, y, z;
int t;
cin>>t;
for(k=;k<=t;k++){
scanf("%d %d",&n,&q);
build(,n,);
while(q--){
scanf("%d %d %d",&x,&y,&z);
update(x,y,z,);
}
printf("Case %d: The total value of the hook is %d.\n",k,get_sum()); }
}

最新文章

  1. printf 的场宽
  2. crawler4j源码学习(2):Ziroom租房网房源信息采集爬虫
  3. javascript笔记---貌似大叔
  4. 一个基本jquery的评论留言模块
  5. Android njava.net.UnknownHostException: Unable to resolve host
  6. 深入了解JavaScript中的for循环
  7. [每日一题] 11gOCP 1z0-052 :2013-09-3 Because of frequent checkpoints...........................A30
  8. wordpress函数技巧
  9. 搭建solr单机版
  10. Struts2基础学习(八)&mdash;Struts2防止表单重复提交
  11. 一周中的后两天 笔记(网路基础 Python基础)
  12. 个人总结-Alpha阶段
  13. 翻译连载 | 第 10 章:异步的函数式(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
  14. 点击table中的某一个td,获得这个tr的所有数据
  15. Flink中的Time
  16. mybatis 一对一关联 association 返回空值
  17. 如何在Anaconda中把python环境更新更高版本
  18. 012 HDFS API 文件读写代码演示
  19. awesomes前端资源库网站
  20. Echarts 修改字体样色 X、Y轴

热门文章

  1. 14 Using Indexes and Clusters
  2. nodejs学习笔记&lt;六&gt;文件处理
  3. (五)stm32工程代码HardFault异常查错调试方法
  4. 发现easyui-accordion一个bug,在ie6、ie7不兼容性问题
  5. go框架
  6. Android入门:绑定本地服务
  7. 企业信息化快速开发平台JeeSite
  8. Android控件之CheckBox(复选框控件)
  9. Asp.NET设置Session过期时间的四种方式
  10. hdu_2089 不要62