Can you answer these queries?(线段树,区间更新,更新到点)
来源:互联网 发布:一个矩阵怎么计算 编辑:程序博客网 时间:2024/06/06 10:45
题目 链接:传送门
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.
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 2 63.
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
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6
这道题本来很好写的,但是一直在用区间更新的方法在做,结果一直超时,太过于在意套路(模板)的思路,结果没有自己做出来,太失望了(深刻的教训)。
思路:这回区间更新要更新到每个点,当tree[num]==r-l+1(及所有的数都是1)的时候,就不用继续更新了。
注意:样例 1(或0) x y x可能大于y,要交换一下。
#include<stdio.h>#include<math.h>#include<string.h>#define N 100009#define LL long longLL tree[N*3+10],a[N];void build(int num,int l,int r){ if(l==r) { tree[num]=a[l]; return ; } int mid=(l+r)>>1; build(num<<1,l,mid); build(num<<1|1,mid+1,r); tree[num]=tree[num<<1]+tree[num<<1|1];}void update(int num,int l,int r,int x,int y){ int mid=(l+r)>>1; if(x<=l&&r<=y) { if(tree[num]==r-l+1) return ; if(l==r) { tree[num]=sqrt(1.0*tree[num]); return ; } update(num<<1,l,mid,x,y); update(num<<1|1,mid+1,r,x,y); tree[num]=tree[num<<1]+tree[num<<1|1]; return ; } if(mid>=x) update(num<<1,l,mid,x,y); if(mid<y) update(num<<1|1,mid+1,r,x,y); tree[num]=tree[num<<1]+tree[num<<1|1];}LL query(int num,int l,int r,int x,int y){ if(x<=l&&r<=y) return tree[num]; int mid=(l+r)>>1; LL sum=0; if(mid>=x) sum+=query(num<<1,l,mid,x,y); if(mid<y) sum+=query(num<<1|1,mid+1,r,x,y); return sum;}int main(){ int n,m; int tt=1; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) scanf("%lld",&a[i]); build(1,1,n); scanf("%d",&m); int x,y,z; printf("Case #%d:\n",tt++); while(m--) { scanf("%d%d%d",&x,&y,&z); if(y>z) { int p=y; y=z; z=p; } if(x) printf("%lld\n",query(1,1,n,y,z)); else update(1,1,n,y,z); } printf("\n"); } return 0;}
- Can you answer these queries?(线段树,区间更新,更新到点)
- Can you answer these queries II 线段树(区间的区间更新)
- hdu 4027 Can you answer these queries?(线段树——区间更新)(思路)
- hdu4027——Can you answer these queries?(线段树+区间更新变形)
- HDU 4027 Can you answer these queries? (线段树 区间更新)
- HDU 4027 Can you answer these queries? 题解(思维+线段树区间更新)
- spoj Can you answer these queries I(线段树 单点更新 区间查询)
- HDU4027 Can you answer these queries?(线段树区间更新,区间开根号)
- Can you answer these queries? (区间更新, 开根号)
- HDU4027:Can you answer these queries?(线段树单点更新)
- Can you answer these queries?(线段树之单点更新)
- hdu_4027Can you answer these queries?线段树+区间更新
- HDU 4027 Can you answer these queries?(线段树 区间不等更新)
- HDU4027:Can you answer these queries?(线段树 单点更新 区间求和)
- HDU 4027 Can you answer these queries?【线段树+区间更新】
- hdoj 4027 Can you answer these queries?(线段树+灵活的区间更新)
- HDU 4027 Can you answer these queries?——其实是点更新的区间更新线段树
- Can you answer these queries? 区间更新和查询
- 读前辈的大话设计模式(十)之建造者模式,抒自己的读后感
- 3DSlicer12:风格准则
- leetcode--141. Linked List Cycle&&142. Linked List Cycle II
- bootstrap系列之五表单样式
- web容器的会话机制
- Can you answer these queries?(线段树,区间更新,更新到点)
- Simpler
- 设计模式阅读总结
- 算法题练习系列之(二十二): 旧键盘
- LINUX编程学习路线
- 1.5 递归算法
- Spring boot添加shiro时,添加EhCacheManager出错
- Java并发编程:CountDownLatch
- codeforces 507A Amr and Music