hdu 4027 Can you answer these queries? 求区间N次根的和 线段树
2011-09-13 16:07
441 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define bignum long long
const int maxn=110000;
struct Trie
{
int left,right;
bignum sum;
int pw;
};
Trie tree[maxn*4];
bignum a[maxn];
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id].pw=1;
if(l!=r)
{
int mid=(l+r)>>1;
buildtree(id<<1,l,mid);
buildtree((id<<1)|1,mid+1,r);
tree[id].sum=tree[id<<1].sum+tree[(id<<1)|1].sum;
}
else
{
tree[id].sum=a[l];
}
}
void update(int id,int l,int r)//[l,r]pw*2,sum reset
{
if(tree[id].pw>64) return ;//important
if(tree[id].left==tree[id].right)
{
tree[id].sum=sqrt(tree[id].sum+0.0);
tree[id].pw*=2;
return ;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) update(id<<1,l,r);
else if(l>=mid+1) update((id<<1)|1,l,r);
else
{
update(id<<1,l,mid);
update((id<<1)|1,mid+1,r);
}
if(tree[id<<1].pw==tree[(id<<1)|1].pw) tree[id].pw=tree[id<<1].pw;
else tree[id].pw=-1;
tree[id].sum=tree[id<<1].sum+tree[(id<<1)|1].sum;
}
bignum query(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
return tree[id].sum;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) return query(id<<1,l,r);
else if(l>=mid+1) return query((id<<1)|1,l,r);
else
{
return query(id<<1,l,mid)+query((id<<1)|1,mid+1,r);
}
}
int main()
{
int n,pl=1;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
buildtree(1,1,n);
int q;scanf("%d",&q);
printf("Case #%d:\n",pl++);
while(q--)
{
int d,l,r;scanf("%d%d%d",&d,&l,&r);
if(l>r) swap(l,r);
if(d==0)
{
update(1,l,r);
}
else
{
bignum cnt=query(1,l,r);
printf("%I64d\n",cnt);
}
}
printf("\n");
}
return 0;
}
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case #1: 19 7 6
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define bignum long long
const int maxn=110000;
struct Trie
{
int left,right;
bignum sum;
int pw;
};
Trie tree[maxn*4];
bignum a[maxn];
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id].pw=1;
if(l!=r)
{
int mid=(l+r)>>1;
buildtree(id<<1,l,mid);
buildtree((id<<1)|1,mid+1,r);
tree[id].sum=tree[id<<1].sum+tree[(id<<1)|1].sum;
}
else
{
tree[id].sum=a[l];
}
}
void update(int id,int l,int r)//[l,r]pw*2,sum reset
{
if(tree[id].pw>64) return ;//important
if(tree[id].left==tree[id].right)
{
tree[id].sum=sqrt(tree[id].sum+0.0);
tree[id].pw*=2;
return ;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) update(id<<1,l,r);
else if(l>=mid+1) update((id<<1)|1,l,r);
else
{
update(id<<1,l,mid);
update((id<<1)|1,mid+1,r);
}
if(tree[id<<1].pw==tree[(id<<1)|1].pw) tree[id].pw=tree[id<<1].pw;
else tree[id].pw=-1;
tree[id].sum=tree[id<<1].sum+tree[(id<<1)|1].sum;
}
bignum query(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
return tree[id].sum;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) return query(id<<1,l,r);
else if(l>=mid+1) return query((id<<1)|1,l,r);
else
{
return query(id<<1,l,mid)+query((id<<1)|1,mid+1,r);
}
}
int main()
{
int n,pl=1;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
buildtree(1,1,n);
int q;scanf("%d",&q);
printf("Case #%d:\n",pl++);
while(q--)
{
int d,l,r;scanf("%d%d%d",&d,&l,&r);
if(l>r) swap(l,r);
if(d==0)
{
update(1,l,r);
}
else
{
bignum cnt=query(1,l,r);
printf("%I64d\n",cnt);
}
}
printf("\n");
}
return 0;
}
相关文章推荐
- HDU 4027 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? (线段树:区间set变形,区间求和)
- HDU 4027 Can you answer these queries? 题解(思维+线段树区间更新)
- 【线段树区间单点更新加约束条件】HDU - 4027 Can you answer these queries?
- Can you answer these queries? (HDU - 4027)(线段树区间更新-区间查询)
- HDU - 4027 Can you answer these queries? (线段树区间开根)
- HDU 4027 Can you answer these queries? (线段树 - 区间开根取整)
- Can you answer these queries? HDU - 4027 线段树 区间更新
- HDU - 4027 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?【线段树+区间更新】
- HDU 4027—— 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?——其实是点更新的区间更新线段树
- HDU 4027 Can you answer these queries?(线段树求区间和)
- HDU 4027 Can you answer these queries? 线段树 区间修改 区间查询