hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和
目录
题目连接
Description
Input
Output
Sample Input
Sample Output
HINT
Can you answer these queries?
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5195
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
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
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
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
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
HINT
题意
线段树 维护区间开根号,查询区间和
题解:
一开始太傻,不会做,除了暴力啥也不会,然后看题解恍然大悟。
long long范围内的数,最多开7次根号,所以每次修改暴力就好了。
应该没有人像我一样认为一个数开方开到最后等于零吧,菜是原罪。
这题比较坑的一点是竟然有x>y的情况,出题人什么心态。原来 between X-th and Y-th 是这个意思吗?
最后提供一组数据:
input:
7
81 12 3 7 9 6 26
5
0 3 5
0 3 1(就是3 1 没有错)
1 1 5
0 2 3
1 1 7
output:
18
48
代码:

1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define N 200050 5 int n,m,cas; ll w[N]; 6 struct Tree{int l,r;ll sum;}tr[N<<2]; 7 template<typename T>void read(T&x) 8 { 9 int k=0;char c=getchar(); 10 x=0; 11 while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar(); 12 if (c==EOF)exit(0); 13 while(isdigit(c))x=x*10+c-'0',c=getchar(); 14 x=k?-x:x; 15 } 16 void push_up(int x) 17 { 18 tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum; 19 } 20 void bt(int x,int l,int r) 21 { 22 tr[x].l=l; tr[x].r=r; 23 if (l==r) 24 { 25 tr[x].sum=w[l]; 26 return; 27 } 28 int mid=(l+r)>>1; 29 bt(x<<1,l,mid); 30 bt(x<<1|1,mid+1,r); 31 push_up(x); 32 } 33 void update(int x,int l,int r) 34 { 35 if (l>r)return; 36 if (tr[x].sum==tr[x].r-tr[x].l+1)return; 37 if (tr[x].l==tr[x].r) 38 { 39 tr[x].sum=(ll)(sqrt(tr[x].sum)); 40 return; 41 } 42 int mid=(tr[x].l+tr[x].r)>>1; 43 if (l<=mid)update(x<<1,l,r); 44 if (mid<r)update(x<<1|1,l,r); 45 push_up(x); 46 } 47 ll query(int x,int l,int r) 48 { 49 if (l<=tr[x].l&&tr[x].r<=r) 50 return tr[x].sum; 51 ll ans=0,mid=(tr[x].l+tr[x].r)>>1; 52 if (l<=mid)ans+=query(x<<1,l,r); 53 if (mid<r)ans+=query(x<<1|1,l,r); 54 return ans; 55 } 56 void work() 57 { 58 read(n); 59 printf("Case #%d:\n",++cas); 60 for(int i=1;i<=n;i++)read(w[i]); 61 bt(1,1,n); 62 read(m); 63 for(int i=1;i<=m;i++) 64 { 65 int id,x,y; 66 read(id); read(x); read(y); 67 if (x>y)swap(x,y); 68 if (id==0)update(1,x,y); 69 if (id==1)printf("%lld\n",query(1,x,y)); 70 } 71 printf("\n"); 72 } 73 int main() 74 { 75 #ifndef ONLINE_JUDGE 76 freopen("aa.in","r",stdin); 77 #endif 78 while(1) 79 { 80 //clear(); 81 work(); 82 } 83 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?