码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4027 Can you answer these queries?

时间:2014-08-28 14:46:39      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   java   io   strong   

Can you answer these queries?

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

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
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
Source
 
------------
线段树。。。昨天下午现场赛时我用树状数组做,样例以及很多例子都过了,但不知道为什么总是WA...或许是学习树状数组才一天的缘故?上午又调试了很久,还是WA了很多次....
dengyaolong师兄在比赛结束的30s内把这道题AC了....略可惜....= =我们俩特别无语,我不会线段树,他不会树状数组,简直无法交流....不过后来还是解决了。。。
比赛结束之后,我把线段树代码重新敲了一遍。。。1Y~
 
这道题的意思倒是不难理解。输入n个数,接着输入m组操作,如果是0,则把范围内的数变成其开方,但是要取整。 1则是查看范围的值得和。
 
以下是线段树代码,树状数组实现的代码至今不知道错在哪>_<
#include<iostream>
#include<cmath>
#include<cstdio>
#include<memory.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100000+10
#define ll long long

ll sum[maxn<<2];
ll cnt[maxn<<2];
void pushUp(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    cnt[rt] = cnt[rt<<1] && cnt[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l == r)
    {
        scanf("%I64d",&sum[rt]);
        return ;
    }
    int m = (l + r)>>1;
    build(lson);
    build(rson);
    pushUp(rt);
}

void update(int L,int R,int l,int r,int rt)
{
    if(l == r)
    {
        sum[rt] = sqrt(sum[rt]+0.0);
        if(sum[rt] <= 1)
            cnt[rt] = 1;
        return ;
    }
    int m = (l + r)>>1;
    if(L <= m && !cnt[rt<<1]) update(L,R,lson);
    if(R > m && !cnt[rt<<1|1]) update(L,R,rson);
    pushUp(rt);
}

ll query(int L,int R,int l,int r,int rt)
{
    if(L <= l && r <= R)
    {
        return sum[rt];
    }
    ll ret = 0;
    int m = (l + r)>>1;
    if(L <= m) ret += query(L,R,lson);
    if(R > m) ret += query(L,R,rson);
    return ret;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
    int n,m,l,r;
    int step = 1,flag;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        memset(cnt,0,sizeof(cnt));
        build(1,n,1);
        printf("Case #%d:\n",step++);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&flag,&l,&r);
            if(l > r)
                swap(l,r);
            if(flag == 0)
                update(l,r,1,n,1);
            else
                printf("%I64d\n",query(l,r,1,n,1));
        }
        printf("\n");
    }

    return 0;
}

dengyaolong教的不用关重定向的新技能,宏 ~\(≧▽≦)/~ 我要好好努力,开开心心AC~~~~

hdu 4027 Can you answer these queries?

标签:des   style   blog   http   color   os   java   io   strong   

原文地址:http://www.cnblogs.com/imLPT/p/3941501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!