HDU4027 Can you answer these queries? —— 线段树 区间修改
Posted h_z_cong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4027 Can you answer these queries? —— 线段树 区间修改相关的知识,希望对你有一定的参考价值。
题目链接:https://vjudge.net/problem/HDU-4027
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.
InputThe 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.
OutputFor 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
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const double EPS = 1e-8; 15 const int INF = 2e9; 16 const LL LNF = 2e18; 17 const int MAXN = 1e5+10; 18 19 LL sum[MAXN<<2]; 20 21 void push_up(int u) 22 { 23 sum[u] = sum[u*2] + sum[u*2+1]; 24 } 25 26 void build(int u, int l, int r) 27 { 28 if(l==r) 29 { 30 scanf("%lld", &sum[u]); 31 return; 32 } 33 34 int mid = (l+r)>>1; 35 build(u*2, l, mid); 36 build(u*2+1, mid+1, r); 37 push_up(u); 38 } 39 40 void attack(int u, int l, int r, int x, int y) 41 { 42 if(l==r) 43 { 44 sum[u] = (LL)sqrt(sum[u]); 45 return; 46 } 47 if(x<=l && r<=y && sum[u]==1LL*(r-l+1)) return; 48 49 int mid = (l+r)>>1; 50 if(x<=mid) attack(u*2, l, mid, x, y); 51 if(y>=mid+1) attack(u*2+1, mid+1, r, x, y); 52 push_up(u); 53 } 54 55 LL query(int u, int l, int r, int x, int y) 56 { 57 if(x<=l && r<=y) 58 return sum[u]; 59 60 LL ret = 0; 61 int mid = (l+r)>>1; 62 if(x<=mid) ret += query(u*2, l, mid, x, y); 63 if(y>=mid+1) ret += query(u*2+1, mid+1, r, x, y); 64 return ret; 65 } 66 67 int main() 68 { 69 int n, m, kase = 0; 70 while(scanf("%d", &n)!=EOF) 71 { 72 build(1, 1, n); 73 scanf("%d", &m); 74 printf("Case #%d:\n", ++kase); 75 for(int i = 1; i<=m; i++) 76 { 77 LL op, x, y; 78 scanf("%lld%lld%lld", &op, &x, &y); 79 int xx = min(x, y), yy = max(x, y); 80 if(op==0) attack(1, 1, n, xx, yy ); 81 else printf("%lld\n", query(1, 1, n, xx, yy) ); 82 } 83 printf("\n"); 84 } 85 }
以上是关于HDU4027 Can you answer these queries? —— 线段树 区间修改的主要内容,如果未能解决你的问题,请参考以下文章
HDU 4027 Can you answer these queries? (线段树)
HDU-4027 Can you answer these queries?(线段树区间开方)
HDU 4027 Can you answer these queries?
Can you answer these queries? HDU - 4027