线段树(类似延迟标记) HDU
来源:互联网 发布:臭氧灯能除螨虫知乎 编辑:程序博客网 时间:2024/06/04 08:57
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 15572 Accepted Submission(s): 3654
Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
Input
The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
Sample Output
Case #1:1976
题意:对于一组数列,有两种操作,输入T, X, Y,T==0表示从第X个数到第Y个数的区间中每个数开根,T==1表示查询区间[X, Y]的和。
解题:线段树,难点在于对于区间每个数的操作不是统一的,方法是对于一个区间和,如果sum[rt] == 1,那么这一段区间就不需要更新了,于是可用类似延迟标记的方法标记一下啊,这个小技巧难点在于需要想到到1时时不需要更新了!
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>using namespace std;typedef long long LL;__int64 sum[100010<<2], col[100010<<2];void Build(int l, int r, int rt){ if(l == r) { scanf("%I64d", &sum[rt]); return ; } int mid = (l+r)>>1; Build(l, mid, rt<<1); Build(mid+1, r, rt<<1|1); sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void update(int l, int r, int L, int R, int rt){ if(l == r) { sum[rt] = sqrt(sum[rt]*1.0); if(sum[rt] == 1) col[rt] = 1; return ; } if(L <= l && r <= R && col[rt]) return ; int mid = (l+r)>>1; if(L <= mid) update(l, mid, L, R, rt<<1); if(mid < R) update(mid+1, r, L, R, rt<<1|1); sum[rt] = sum[rt<<1]+ sum[rt<<1|1]; col[rt] = col[rt<<1]&&col[rt<<1|1];}__int64 query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return sum[rt]; int mid = (l + r) >> 1; __int64 res = 0; if(L <= mid) res += query(L, R, l, mid, rt<<1); if(mid < R) res += query(L, R, mid+1, r, rt<<1|1); return res;}int main(){ int n, m, t, x, y; int casee = 1; while(~scanf("%d", &n)) { memset(sum, 0, sizeof(sum)); memset(col, 0, sizeof(col)); Build(1, n, 1); scanf("%d", &m); printf("Case #%d:\n", casee++); while(m--) { scanf("%d%d%d", &t, &x, &y); if(x > y) { int s = x; x = y; y = s; } if(t == 0) update(1, n, x, y, 1); else printf("%I64d\n", query(x, y, 1, n, 1)); } printf("\n"); } return 0;}0 0
- 线段树(类似延迟标记) HDU
- E - Just a Hook HDU 1698 (线段树+类似延迟标记)
- 线段树 延迟标记
- HDU 1689 Just a HooK(线段树+延迟标记)
- hdu 4893 线段树 --- 也是两个变 类似双标记
- HDU1698(线段树+延迟标记)
- HDU 1698 Just a Hook (线段树延迟标记(lazy))
- 【hdu】 Just a Hook (线段树 -成段更新,延迟标记)
- 杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)
- 线段树(成段更新 关于延迟标记) poj3468
- 线段树模板 (poj 3468)延迟标记
- 线段树 (区间修改 区间查询 延迟标记)
- POJ 3667 Hotel(线段树+区间合并+延迟标记)
- POJ 2528 线段树(离散化+延迟标记)
- H - Can you answer these queries? HDU 4027 (线段树+延迟标记+开根号的速度)
- 浅析__线段树延迟标记
- poj 3468 线段树,延迟标记法
- poj 3468 线段树延迟标记
- React-router@4.0版本简易使用教程
- 递归实现单链表的查找
- Linux学习 第十一单元
- tensorflow-mnist手写数字识别
- HDU 4114 Disney's FastPass(floyd+状态压缩DP)
- 线段树(类似延迟标记) HDU
- CvArr、Mat、CvMat、IplImage、BYTE;QPixmap和QImage
- 用python脚本实现自动部署环境(一)
- 二叉树以及遍历算法
- 九大排序之——希尔排序
- ZOJ2975
- mybatis中插入数据自动返回自增长id的配置
- Qt软件开发文档8---登陆界面的绘制及其封装
- IntelliJ IDEA 乱码解决方案 (项目代码、控制台等)