hdu4027 Can you answer these queries?

来源:互联网 发布:阿城 知乎 编辑:程序博客网 时间:2024/05/19 18:42

Can you answer these queries?

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


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



线段树,只要考虑到每个节点最多只能更新不到10次就可以用线段树暴搞了。

因为每个节点开根号到1就不用再更新了。

代码:

#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;typedef struct{    int l,r;    long long sum;}Tree;Tree tree[500005];int n;void Build(int t,int l,int r){    tree[t].l=l;    tree[t].r=r;    if (l==r)    {        scanf("%I64d",&tree[t].sum);        return;    }    int mid=(l+r)/2;    Build(2*t+1,l,mid);    Build(2*t+2,mid+1,r);    tree[t].sum=tree[2*t+1].sum+tree[2*t+2].sum;}void Change(int t,int x,int y){    int l,r,mid;    l=tree[t].l;    r=tree[t].r;    if (l==x && r==y && tree[t].sum==r-l+1) return;    if (l==r)    {        tree[t].sum=sqrt(tree[t].sum*1.0);        return;    }    mid=(l+r)/2;    if (x<=mid) Change(2*t+1,x,min(y,mid));    if (y>mid) Change(2*t+2,max(x,mid+1),y);    tree[t].sum=tree[2*t+1].sum+tree[2*t+2].sum;}long long Count(int t,int x,int y){    int l,r,mid;    l=tree[t].l;    r=tree[t].r;    if (l==x && r==y) return tree[t].sum;    long long ans=0;    mid=(l+r)/2;    if (x<=mid) ans+=Count(2*t+1,x,min(mid,y));    if (y>mid) ans+=Count(2*t+2,max(x,mid+1),y);    return ans;}int main(){    int i,j,t,cnt=1,x,y,z;    while(scanf("%d",&n)!=EOF)    {        Build(0,0,n-1);        scanf("%d",&t);        printf("Case #%d:\n",cnt++);        while(t--)        {            scanf("%d%d%d",&z,&x,&y);            if (z==0)            {                Change(0,min(x-1,y-1),max(x-1,y-1));            }            else            {                printf("%I64d\n",Count(0,min(x-1,y-1),max(x-1,y-1)));            }        }        printf("\n");    }    return 0;}


原创粉丝点击