kuangbin专题七 HDU4027 Can you answer these queries? (线段树)

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.

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


这道题目测区间修改,但是不像加或减,开方不好处理,但是发现一个数开7 8次就变为1了,所以可以修改叶节点,然后求和就可以了。如果区间全是1,就不需要往下更新了



 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <sstream>
13 #include <stack>
14 using namespace std;
15 #define FO freopen("in.txt","r",stdin);
16 #define rep(i,a,n) for (int i=a;i<n;i++)
17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
18 #define pb push_back
19 #define mp make_pair
20 #define all(x) (x).begin(),(x).end()
21 #define fi first
22 #define se second
23 #define SZ(x) ((int)(x).size())
24 #define debug(x) cout << "&&" << x << "&&" << endl;
25 #define lowbit(x) (x&-x)
26 #define mem(a,b) memset(a, b, sizeof(a));
27 typedef vector<int> VI;
28 typedef long long ll;
29 typedef pair<int,int> PII;
30 const ll mod=1000000007;
31 const int inf = 0x3f3f3f3f;
32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
34 //head
35 
36 const int maxn=100010;
37 ll sum[maxn<<2];
38 int n,q;
39 
40 void pushup(int rt) {
41     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
42 }
43 
44 void build(int rt,int L,int R) {
45     if(L==R) {
46         scanf("%lld",&sum[rt]);
47         return;
48     }
49     int mid=(L+R)>>1;
50     build(rt<<1,L,mid);
51     build(rt<<1|1,mid+1,R);
52     pushup(rt);
53 }
54 
55 void updata(int rt,int L,int R,int l,int r) {
56     if(L==R) {
57         sum[rt]=sqrt(sum[rt]);
58         return;
59     }
60     if(L>=l&&R<=r&&sum[rt]==(R-L+1)) return;
61     int mid=(L+R)>>1;
62     if(l<=mid) updata(rt<<1,L,mid,l,r);
63     if(r>mid) updata(rt<<1|1,mid+1,R,l,r); 
64     pushup(rt);
65 }
66 
67 ll query(int rt,int L,int R,int l,int r) {
68     if(L>=l&&R<=r) return sum[rt];
69     ll ans=0;
70     int mid=(L+R)>>1;
71     if(l<=mid) ans+=query(rt<<1,L,mid,l,r);
72     if(r>mid) ans+=query(rt<<1|1,mid+1,R,l,r);
73     pushup(rt);
74     return ans;
75 }
76 
77 int main() {
78     int cur=1;
79     while(~scanf("%d",&n)) {
80         build(1,1,n);
81         scanf("%d",&q);
82         int op,ll,rr;
83         printf("Case #%d:
",cur++);
84         while(q--) {
85             scanf("%d%d%d",&op,&ll,&rr);
86             int l=min(ll,rr),r=max(ll,rr);
87             if(op) printf("%lld
",query(1,1,n,l,r));
88             else updata(1,1,n,l,r);
89         }
90         printf("
");
91     }
92 }


原文地址:https://www.cnblogs.com/ACMerszl/p/9865921.html