又长又臭又慢的线段树 模板题hdu1698 Just a Hook

Just a Hook

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

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.

练练手熟悉一下写法

不过写完才发现这个直接输出val[1]就好了

贴代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int t=0,n=0,val[400010],mark[400010],m=0,x=0,y=0,z=0;
 7
 8 void build(int pos,int ll,int rr){
 9     if(ll==rr){
10         val[pos]=1;
11         mark[pos]=0;
12         return;
13     }
14     else{
15         int mid=(ll+rr)/2;
16         build(2*pos,ll,mid);
17         build(2*pos+1,mid+1,rr);
18         val[pos]=val[2*pos]+val[2*pos+1];
19     }
20 }
21
22 void down(int pos,int num){
23     if(mark[pos]){
24         val[2*pos]=mark[pos]*(num-num/2);
25         val[2*pos+1]=mark[pos]*(num/2);//attention!
26         mark[2*pos]=mark[2*pos+1]=mark[pos];
27         mark[pos]=0;
28     }
29 }
30
31 void change(int pos,int ll,int rr){
32     if(x>rr||y<ll) return;
33     if(x<=ll&&y>=rr){
34         val[pos]=z*(rr-ll+1);
35         mark[pos]=z;
36         return;
37     }
38     down(pos,rr-ll+1);
39     int mid=(ll+rr)/2;
40     change(2*pos,ll,mid);
41     change(2*pos+1,mid+1,rr);
42     val[pos]=val[2*pos]+val[2*pos+1];
43 }
44
45 int main(){
46     scanf("%d",&t);
47     for(int i=1;i<=t;i++){
48         memset(val,0,sizeof(val));
49         memset(mark,0,sizeof(mark));
50         scanf("%d",&n);
51         build(1,1,n);
52         scanf("%d",&m);
53         for(int j=1;j<=m;j++){
54             scanf("%d%d%d",&x,&y,&z);
55             change(1,1,n);
56         }
57         printf("Case %d: The total value of the hook is %d.\n",i,val[1]);
58     }
59     return 0;
60 }
时间: 2024-10-13 12:52:05

又长又臭又慢的线段树 模板题hdu1698 Just a Hook的相关文章

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; int n,q,tot,a[110000]; in

LA 2191电位计(线段树模板题)

线段树模板题,没啥好说的.....注意输出是case之间空一行就行.........之前一直没注意,一直wa 代码如下: #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #includ

1270. 数列区间最大值(climits用法+线段树模板题)

题目链接: https://www.acwing.com/problem/content/1272/ 题解: 线段树模板题,单点求和.区间查询都可 AC代码: #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <climits> using namespace std; const int N = 1e5+10; int a

P1243~P1247 线段树模板题总结

前言 这几天刚刚刷了5道线段树(水)题,现在来总结一下. 首先是犯的不少错误: 1.建树.更新函数没有return.这是最气的,每次最后程序错误查了半天也没查出来,最后发现是没有return.递归边界要return,递归边界要return,递归边界要return,重要的事情说三遍. 2.判断查找区间于线段的变量写反.听说这个是常犯错误. 然后说一下学线段树的收获.线段树的代码量的确多,很能练自己的思维,而且学的过程中简单的理解了一下#define的用处.线段树用来解决区间查询,区间修改都很方便,

HDU 1166 线段树模板题

坐了3天的火车,终于到外婆家了(┬_┬).这次北京之旅颇有感触,虽然学到的东西不是很多(主要是自己的原因,没有认真学,并不是老师讲的不好),不过出去见见世面也是好的.最后一场比赛印象颇深,被虐的有点惨....记得当时有道题感觉自己能过,想用数组模拟链表水过,可是无论怎么优化一直超时@[email protected]后面比完后听朋友说那题应该用线段树做,顿时恍然大悟,然并卵,线段树的模板早忘了.....今天做道线段树的模板题先复习一下,过会再想想那道题. 敌兵布阵 Time Limit: 200

poj2823 线段树模板题 点修改(也可以用单调队列)

这道题吧 没计算时间 因为给了那么多 一算还可以 就直接写了线段树,刘汝佳那本模板 然后!poj的g++比C++慢大约500ms.......g++tle,C++就过了 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67576   Accepted: 19163 Case Time Limit: 5000MS Description An array of size n ≤ 106 is

权值线段树模板题

array Time Limit: 1500ms Memory Limit: 256M Description You are given an array . Initially, each element of the array is unique.Moreover, there are  instructions. Each instruction is in one of the following two formats: 1. ,indicating to change the v

HDU1754 线段树模板题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目分析:对于给出的一个很长的区间,对其进行单点更新值和区间求最大值的操作,由于查询的区间很大,且查询次数多,这里用线段树求解将是十分合适的 注意点:1.对于存放线段树的数组大小需要开大一些 2.对于c语言的字符输入%c之前需要加一个空格保证输入准确 1 #include<iostream> 2 #include<string.h> 3 using namespace std;

[bzoj1568]李超线段树模板题(标志永久化)

题意:要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第i条被插入的线段的标号为i. 2.给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. 解题关键:注意标志的作用 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cmath> 6 #include<iostrea