HDU-1698(线段树_区间更新_java手写)

Just a Hook

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


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
 

Recommend
wangye


郁闷了,java写的线段树超时,亏我还用心的写了一晚上,修改了又修改,百度了又百度,我不想说更多的了,

java是比较慢...

像这种数据结构类型的,我想java还是很难过的.我说怎么没有人用java交题目呢.我想精通java的应该也不少呀...

所以以后写算法题目还是少用java写吧.

然后就是,我们队伍有一个人退出ACM了,虽然说还不算什么队伍,我的求胜心也磨的差不多了,也不知道怎么办了,一次次的打击,

谁都不想在继续搞下去了,谁都会没信心的.但是放弃总不是办法,我想只有还有人坚持我就会坚持,而且我不仅仅是为了拿奖,

当然拿奖是最好的,我学习他是因为我喜欢他(虽然没有什么成绩),但是,这丝毫不影响我喜欢他,

未来的路谁也不知道是什么样子的,所以,我坚持做我喜欢的事.明天计划把笔记本拆了..呼呼...


虽然超时间了,但是还是把代码贴一下,也不枉白写这么长时间吧.


import java.util.Scanner;

class node {
	public int l;
	public int r;
	public int val;
	public int sum;
	public node() {
		val = 1;
		sum = 1;
	}
}//这是一个结点的模型,一个独立的java只能有一个public类,充当接口,但是在项目开发的时候一般都一个java,一个独立的类

public class HDU1698 {
	public static final int maxn = 401111;//相当于c中的全局变量

	public static node[] tree = new node[maxn];//必须声明给多大的空间给他,这就类似,int a[100]了,你不能只声明个名字.
												//你得告诉系统,你要多少的空间,所以后面的new必须存在.
	
	public static void pushUp(int i) {
		tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;//这是向上求和
	}
	
	public static void pushDown(int i) {
		if (tree[i].val != 0) { //这个快纠结死我了!一直在想c中的非0即为true,但是在java中整数1你不能当做boolean类型的true
								//所以你就泪奔了.但是本质我们却得知道,我们干什么非要省事!我们把 a != 0写全怎么了,而且不管
								//java和c逻辑表达式返回的用是true和false你总不能不知道吧..哭泣....
								//这就是成段更新的精华,如果遇到了区间不吻合的话,就及时的把该区间的val下荡给孩子,然后再
								//标记这段已经混乱了,即val = 0;
			tree[i << 1].val = tree[i << 1 | 1].val = tree[i].val;
			tree[i << 1].sum = (tree[i << 1].r - tree[i << 1].l + 1) * tree[i << 1].val; // 其实这样写还是很繁琐的,不如来个区间端点和
																						 //也就是传过来m = (r - l + 1);
																						 //左边的 = m - (m >> 1)
																						 //右边的 = m >> 1;
			tree[i << 1 | 1].sum = (tree[i << 1 | 1].r - tree[i << 1 | 1].l + 1) * tree[i << 1 | 1].val;
			tree[i].val = 0;//已经混乱了
		}
	}
	
	public static void buildTree(int i, int l, int r) {
		tree[i] = new node();//一定要注意,你告诉系统你要多少的内存了,但是还没有完,你还要把这段空间给复制了,
							//要不然,你只要了,却没有初始化.你怎么用???给了你块地,你就想吃米饭?你不种水稻,上哪吃????
		tree[i].l = l;
		tree[i].r = r;
		if (l == r) {
			return ;
		}
		int mid = (l + r) >> 1;
		buildTree(i << 1, l, mid);
		buildTree(i << 1 | 1, mid + 1, r);
		pushUp(i);
	}
	
	public static void update(int i, int l, int r, int val) {
		if (tree[i].l == l && tree[i].r == r) {
			tree[i].val = val;
			tree[i].sum = (r - l + 1) * tree[i].val;
			return ;
		}
		pushDown(i);//如果上面的没有执行,就说明,完蛋,区间没有完全匹配,这段区间要打乱.
		int mid = (tree[i].l + tree[i].r) >> 1;
		if (r <= mid) {
			update(i << 1, l, r, val);
		} else if (l > mid) {
			update(i << 1 | 1, l, r, val);
		} else {
			update(i << 1, l, mid, val);
			update(i << 1 | 1, mid + 1, r, val);
		}
		pushUp(i);
	}
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int T;
		while (input.hasNext()) {
			T = input.nextInt();
			for (int k = 1; k <= T; k++) {
				int N = input.nextInt();
				buildTree(1, 1, N);
				int M = input.nextInt();
				for (int i = 0; i < M; i++) {
					int left = input.nextInt();
					int right = input.nextInt();
					int val = input.nextInt();
					update(1, left, right, val);
				}
				System.out.println("Case " + k + ": The total value of the hook is " + tree[1].sum + ".");
			}
		}
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值