HDU 4027 http://acm.hdu.edu.cn/showproblem.php?pid=4027
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.
题意
多组输入,给定一个具有n个数的数字序列,接下来有m步操作,每步操作输入t,x,y;当 t == 0是,区间 [x,y] 内的数字开根号,当 t == 1时,查询区间 [x,y] 的和。
思路
由于是取根号,区间和的根号与根号区间和,他们两个的值的值的不一样的,所以区间修改时,应该改为:
if(tree[k].l==tree[k].r){ //单点修改
tree[k].sum = (ll)sqrt(tree[k].sum);
return;
}
先将区间内的所有数字开根号,然后在求和:
if(tree[k].l==tree[k].r){ //单点更新
tree[k].sum = (ll)sqrt(tree[k].sum);
return;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(ql<=mid) update(k<<1, ql, qr); //左推进
if(qr>mid) update(k<<1|1, ql, qr); //右推进
tree[k].sum = tree[k<<1].sum + tree[k<<1|1].sum; //求和
此时,要注意一点:
就是 sqrt(1) = 1;所以说当数字是1,也就是区间和为区间长度的时候,可以不用修改直接跳过,达到剪枝,防止TL的效果;
if(tree[k].sum == (tree[k].r-tree[k].l+1)) return; //区间和等于区间长度
考虑到求和 maybe 超 int ,就直接用 long long;
然后查询区间和的话,跟原来的板子差不多,只不过要加一个剪枝;
if(tree[k].sum == (tree[k].r-tree[k].l+1)) //说明[l,r]区间内全为 1
return min(tree[k].r, qr)-max(tree[k].l, ql)+1; //找一下[ql,qr]和[l,r]相交的部分
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<limits>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=100005;
struct node
{
int l,r;
ll sum;
}tree[maxn<<2];
int n, m;
ll a[maxn];
void build(int k, int l, int r)
{
tree[k].l = l,tree[k].r = r;
if(l==r){
tree[k].sum=a[l];
return;
}
int mid=(l+r)>>1;
build(k<<1, l, mid);
build(k<<1|1, mid+1, r);
tree[k].sum = tree[k<<1].sum + tree[k<<1|1].sum;
}
void update(int k,int ql, int qr)
{
if(tree[k].sum == (tree[k].r-tree[k].l+1)) return;
if(tree[k].l==tree[k].r){
tree[k].sum = (ll)sqrt(tree[k].sum);
return;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(ql<=mid) update(k<<1, ql, qr);
if(qr>mid) update(k<<1|1, ql, qr);
tree[k].sum = tree[k<<1].sum + tree[k<<1|1].sum;
}
ll query(int k, int ql, int qr)
{
if(tree[k].sum == (tree[k].r-tree[k].l+1)) return min(tree[k].r, qr)-max(tree[k].l, ql)+1;
if(ql<=tree[k].l && qr>=tree[k].r) return tree[k].sum;
int mid=(tree[k].l+tree[k].r)>>1;
ll ans=0;
if(qr>mid) ans += query(k<<1|1, ql, qr);
if(ql<=mid) ans += query(k<<1, ql, qr);
return ans;
}
int main()
{
int p=0;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,1,n);
scanf("%d",&m);
printf("Case #%d:\n", ++p);
while(m--){
int e1,e2,e3;
scanf("%d%d%d", &e1, &e2, &e3);
if(e2>e3) swap(e2, e3);
if(e1)
printf("%lld\n", query(1, e2, e3));
else
update(1, e2, e3);
}
printf("\n"); //注意格式
}
return 0;
}