java-HDU1698(线段树的区间更新,和区间查询)
HDU1698:
题目意思:
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 49065 Accepted Submission(s): 23200
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.
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.
package Combat.com;import java.util.Arrays; import java.util.Scanner;public class Main {static final int MAX = 100005;static int lazy[] = new int[4*MAX];static int node[] = new int[4*MAX]; public static void main(String []args){Scanner cin = new Scanner(System.in);int T = cin.nextInt();for(int i = 1; i <= T; i++){int n = cin.nextInt();Arrays.fill(lazy, -1);buildBinaryTree(1,n,1);int q = cin.nextInt();for(int j = 0; j < q; j++){int x = cin.nextInt();int y = cin.nextInt();int z = cin.nextInt();update(x,y,z,1,n,1);}System.out.println("Case " + i + ": The total value of the hook is " + node[1]+".");}}static void update(int x,int y,int z,int L,int R,int num){if(x <= L && R <= y){node[num] = (R-L+1)*z;lazy[num] = z;return;}int mid = (L+R)>>1;pushDown(L,R,num);if(x <= mid){update(x,y,z,L,mid,num<<1);}if(y > mid){update(x,y,z,mid+1,R,num<<1|1);}node[num] = node[num<<1] + node[num<<1|1];} static void pushDown(int L,int R,int num){if(lazy[num] != -1){int mid = (L+R)>>1;lazy[num<<1] = lazy[num];lazy[num<<1|1] = lazy[num];node[num<<1] = (mid-L+1)*lazy[num];node[num<<1|1] = (R-mid)*lazy[num];lazy[num] = -1;}}static void buildBinaryTree(int L,int R,int num){if(L == R){node[num] = 1;return;}int mid = (L+R)>>1;buildBinaryTree(L,mid,num<<1);buildBinaryTree(mid+1,R,num<<1|1);node[num] = node[num<<1]+node[num<<1|1];} }
做这道题目时,我主要遇到的问题是:1.数组开得太小了,直接报错。2.lazy数组的用法,没有理解好!
总之,这些都是模板题,做多点就容易上手了。
转载于:https://www.cnblogs.com/674001396long/p/10809779.html
java-HDU1698(线段树的区间更新,和区间查询)相关推荐
- 线段树(区间修改,区间查询)
线段树的区间修改 本题如果用单点修改的思想会T,所以需要引入一个数组lazylazylazy , 优秀程序员必备 lazylazylazy定义 此为偷懒 该数组意在储存 treetreetree 数组 ...
- (转)线段树的区间更新
原文地址:http://blog.csdn.net/zip_fan/article/details/46775633 写的很好,昨天刚刚开始写线段树,有些地方还不是很明白,看了这篇博文,学会了数组形式 ...
- 【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)
题干: There is a company that has N employees(numbered from 1 to N),every employee in the company has ...
- poj 2528 Mayor's posters(线段树 离散化 区间更新 贴海报)
这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路 搞清楚,才能谈的上调bug,否则 ...
- 【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)
题干: You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type o ...
- 【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)
题干: DZY has a sequence a[1..n]a[1..n]. It is a permutation of integers 1∼n1∼n. Now he wants to perfo ...
- POJ 3468 A Simple Problem with Integers(线段树:区间更新)
http://poj.org/problem?id=3468 题意: 给出一串数,每次在一个区间内增加c,查询[a,b]时输出a.b之间的总和. 思路: 总结一下懒惰标记的用法吧. 比如要对一个区间范 ...
- HDU 4267 线段树 离散点区间更新, 自叶子节点至根单点查询
题意: n个数字 下面n个数字表示数列 2个操作 1 [u, v] k add [u,v ]区间 (u点要计算)每隔k个位置,该数字+add 2 pos 询问 pos下标的值(下标从1开始) 思路 ...
- Find the median(线段树离散化+区间更新)
题目链接:https://ac.nowcoder.com/acm/contest/887/E 链接:https://ac.nowcoder.com/acm/contest/887/E 来源:牛客网 F ...
最新文章
- 企业网络推广网站排名首页但确保用户体验也是企业网络推广的关键
- 局域网抢答器_基于童芯派的抢答器V1.0
- 【HDU - 3440】House Man(差分约束)
- java.lang.math.trunc,java – JPA/Hibernate返回BigDecimal不长
- Dijkstra算法图文详解和C++代码
- 使用Angularjs的ng-cloak指令避免页面乱码
- android开源数据库,Android Hawk数据库 github开源项目
- 软件流程和管理(四):PMP Stakeholder Management
- c语言学生成绩及格率,c语言百分制输入学生的考试分数统计学生及格率
- 那些Explain参数代表着什么?
- 百万邮做邮件营销的邮箱配置
- 程序设计基础—什么是逻辑与、或、非关系?
- 解决笔记本电脑外接显示器后声音播放不了的问题
- 仿写携程旅游手机浏览器页面
- python 不区分大小写的字典实现
- C语言——初识关键字、static、#define定义、指针
- SwitchHosts使用详解 (转)
- Android 自定义圆形进度条(圆环刻度)View
- 达人评测 iPad mini 5和mini 6 选哪个好
- 如何在Ubuntu 14.04上使用NSD——一套仅权威DNS服务器