hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17124 Accepted Submission(s): 8547
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
2008 “Sunline Cup” National Invitational Contest
成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候
代码:
1 #include<cstring>
2 #include<cstdio>
3 const int maxn=100005;
4 struct node
5 {
6 int lef,rig,sum;
7 int cnt;
8 int mid(){
9 return lef+(rig-lef>>1);
10 }
11 };
12
13 node sac[maxn*3];
14
15 void Build(int left,int right,int pos)
16 {
17 sac[pos]=(node){left,right,1};
18 if(left==right)return ;
19 int mid=sac[pos].mid();
20 Build(left,mid,pos<<1);
21 Build(mid+1,right,pos<<1|1);
22 sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum;
23 }
24 void Update(int left,int right,int pos,int val)
25 {
26 if(left<=sac[pos].lef&&sac[pos].rig<=right){
27 sac[pos].sum=val*(sac[pos].rig-sac[pos].lef+1);
28 sac[pos].cnt=val;
29 return ;
30 }
31 if(sac[pos].cnt!=0){ //向下更新一次
32 sac[pos<<1].sum=sac[pos].cnt*(sac[pos<<1].rig-sac[pos<<1].lef+1);
33 sac[pos<<1|1].sum=sac[pos].cnt*(sac[pos<<1|1].rig-sac[pos<<1|1].lef+1);
34 sac[pos<<1|1].cnt=sac[pos<<1].cnt=sac[pos].cnt;
35 sac[pos].cnt=0;
36 }
37 int mid=sac[pos].mid();
38 if(mid>=left)
39 Update(left,right,pos<<1,val);
40 if(mid<right)
41 Update(left,right,pos<<1|1,val);
42 sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum;
43 }
44 int main()
45 {
46 int test,n,Q;
47 int a,b,c;
48 scanf("%d",&test);
49 for(int i=1;i<=test;i++){
50 scanf("%d%d",&n,&Q);
51 Build(1,n,1);
52 while(Q--)
53 {
54 scanf("%d%d%d",&a,&b,&c);
55 Update(a,b,1,c);
56 }
57 printf("Case %d: The total value of the hook is %d.n",i,sac[1].sum);
58 }
59 }
- Sqlite向MySql导入数据
- 未来3年,人工智能如何影响法律行业?5位权威专家给出趋势
- Java 常见内存溢出异常与代码实现
- nginx限制上传大小和超时时间设置说明/php限制上传大小
- Unity Application Block 1.2 学习笔记
- 苹果首个自动驾驶专利到底有什么来头?
- 围棋遇上互联网:科技打开优秀传统文化未来之门
- 神经网络开始放飞自我!都是因为架构搜索新算法
- 浏览器缓存问题的解决
- nginx下目录浏览及其验证功能、版本隐藏等配置记录
- Sqlite的多表连接更新
- Enterprise Library 4.1学习笔记6----加密应用程序块
- 浅谈数据库主键策略
- nginx应用总结(1)--基础认识和应用配置
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法