线段树之Can you answer these queries?
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 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.
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
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int N=1e5+5; long long a[N]; long long f[N<<2]; int m,n,t,x,y,ans; void build(int root,int left,int right){ if(left==right){ f[root]=a[left]; return; } int mid=(left+right)>>1; int rt=root<<1; build(rt,left,mid); build(rt+1,mid+1,right); f[root]=f[rt]+f[rt+1]; return; } void update(int root,int left,int right,int uleft,int uright){ if(f[root]==right-left+1){ return ; } int rt=root<<1; if(left==right){ f[root]=sqrt(f[root]); return ; } int mid=(left+right)>>1; if(uleft<=mid){ update(rt,left,mid,uleft,uright); } if(uright>mid){ update(rt+1,mid+1,right,uleft,uright); } f[root]=f[rt]+f[rt+1]; return; } long long query(int root,int left,int right,int qleft,int qright){ long long ans=0; if(left>=qleft&&right<=qright){ return f[root]; } int mid=(left+right)>>1; int rt=root<<1; if(qleft<=mid){ ans+=query(rt,left,mid,qleft,qright); } if(qright>mid){ ans+=query(rt+1,mid+1,right,qleft,qright); } return ans; } int main(){ while(~scanf("%d",&m)){ memset(f,0,sizeof(f)); for(int i=1;i<=m;i++){ scanf("%lld",&a[i]); } scanf("%d",&n); printf("Case #%d:\n",++ans); build(1,1,m); while(n--){ scanf("%d %d %d",&t,&x,&y); if(x>y){ swap(x,y); } if(t){ printf("%lld\n",query(1,1,m,x,y)); } else{ update(1,1,m,x,y); } } printf("\n"); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!