HDU 4027 Can you answer these queries?

来源:互联网 发布:淘宝怎么开店好赚钱呢 编辑:程序博客网 时间:2024/05/29 14:27
http://acm.hdu.edu.cn/showproblem.php?pid=4027

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9288    Accepted Submission(s): 2127


Problem 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 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 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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
 

Sample Output
Case #1:1976
 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3308 2871 2795 1542 3397 
真是神坑 啊。。好在有题解。
线段树 单点更新 区间查询。。纪念一下

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100010
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
struct node
{
int l,r;
ll sum;
}nod[maxn*3];
int n,m;
ll num[maxn];
void build(int i,int l,int r)
{
nod[i].l=l;
nod[i].r=r;
if(l==r)
{

nod[i].sum=num[l];
return ;
}
int mid=(r+l)/2;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
nod[i].sum=nod[i<<1].sum+nod[i<<1|1].sum;
}

void update(int i,int a,int b)
{
if(nod[i].l==nod[i].r)
{
nod[i].sum=(ll)sqrt(nod[i].sum*1.0);
return ;
}
if(nod[i].sum==nod[i].r-nod[i].l+1)return ;

int mid=(nod[i].l+nod[i].r)/2;
if(b<=mid)update(i<<1,a,b);
else if(a>mid)update(i<<1|1,a,b);
else
{
update(i<<1,a,mid);
update(i<<1|1,mid+1,b);
}
nod[i].sum=nod[i<<1].sum+nod[i<<1|1].sum;
}

ll query(int i,int a,int b)
{
if(nod[i].l==a&&nod[i].r==b)
{
return nod[i].sum;
}
ll ans=0;
int mid=(nod[i].l+nod[i].r)/2;
if(b<=mid)ans+=query(i<<1,a,b);
else if(a>mid)ans+=query(i<<1|1,a,b);
else ans+=(query(i<<1,a,mid)+query(i<<1|1,mid+1,b));
return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,a,b,cnt=0;
while(scanf("%d",&n)!=EOF)
{
cnt++;
for(int i=1;i<=n;i++)
scanf("%I64d",&num[i]);
printf("Case #%d:\n",cnt);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&t,&a,&b);
if(t)
{

printf("%I64d\n",query(1,min(a,b),max(a,b)));
}
else
{
update(1,min(a,b),max(a,b));
}
}
printf("\n");

}
return 0;
}



0 0
原创粉丝点击