hdu 4027 Can you answer these queries?

题目链接:点这里

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.

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

【题意】

给你一串数字,然后有两个操作:

0 a b 将[a,b]区间里面所有的数都开方然后向下取整。
1 a b 问你区间[a,b]里面所有数字的和。

其中数字的取值为[1,2^63]。

【分析】

我算是明白了,反正这种区间区间的操作,第一个想到线段树就没错。

开始用lazy标记做的,后来发现实现挺麻烦的。然后就放弃了,直接更新下去就行。下面说一下几个要注意的地方:

首先就是更新的时候可以注意到1开方多少次都是1,所以当一个区间的值变成区间长度的时候就不用往下更新了。

然后就是比较坑爹的一点,不知道题目里面说明了没有,可能是我没有注意吧。就是给你的区间可能会有右边小于左边的情况,你得swap一下。

还有就是每组测试用例输出结束之后要求有一个换行。可能是我英语太水读题不认真,wa了一发。

然后就没啥了。

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define sin1(a) scanf("%d",&(a))
#define sin2(a,b) scanf("%d%d",&(a),&(b))
#define sll(a) scanf("%lld",&(a))
#define sll2(a,b) scanf("%lld%lld",&(a),&(b))
#define sdo(a) scanf("%lf",&(a))
#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))
#define inf 0x3f3f3f3f
#define lson i<<1,l,mid
#define rson ((i<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<typename T>
void read1(T &m)
{
    T x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    m = x*f;
}
template<typename T>
void read2(T &a,T &b)
{
    read1(a);
    read1(b);
}
template<typename T>
void read3(T &a,T &b,T &c)
{
    read1(a);
    read1(b);
    read1(c);
}
template<typename T>
void out(T a)
{
    if(a>9) out(a/10);
    putchar(a%10+'0');
}
template<typename T>
void outn(T a)
{
    if(a>9) out(a/10);
    putchar(a%10+'0');
    puts("");
}
using namespace std;
///-----------------------------------------------------------
const int maxm = 100000;
struct
{
    ll l;
    ll r;
    ll ans;
} tree[maxm<<2];
void build(int ind,int l,int r)
{
    tree[ind].l=l;
    tree[ind].r=r;
    if(l==r)
    {
        read1(tree[ind].ans);
        return;
    }
    int mid=(l+r)>>1;
    build(ind<<1,l,mid);
    build(ind<<1|1,mid+1,r);
    tree[ind].ans=tree[ind<<1].ans+tree[ind<<1|1].ans;
}
void update(int ind,int l,int r)
{
    if(tree[ind].l==l&&tree[ind].r==r&&tree[ind].ans==r-l+1) return ;///全是1
    if(tree[ind].l==tree[ind].r)
    {
        tree[ind].ans=(ll)sqrt((double)tree[ind].ans);
        return;
    }
    int mid=(tree[ind].l+tree[ind].r)>>1;
    if(l>mid) update(ind<<1|1,l,r);
    else if(r<=mid) update(ind<<1,l,r);
    else
    {
        update(ind<<1|1,mid+1,r);
        update(ind<<1,l,mid);
    }
    tree[ind].ans=tree[ind<<1].ans+tree[ind<<1|1].ans;
}
ll cal(int ind,int l,int r)
{
    if(tree[ind].l==l&&tree[ind].r==r)
        return tree[ind].ans;
    int mid = (tree[ind].l+tree[ind].r)>>1;
    if(l>mid) return cal(ind<<1|1,l,r);
    else if(r<=mid) return cal(ind<<1,l,r);
    else
    {
        return cal(ind<<1,l,mid)+cal(ind<<1|1,mid+1,r);
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    int len;
    int T=0;
    while(sin1(len)!=EOF)
    {
        printf("Case #%d:\n",++T);
        build(1,1,len);
        int que;
        read1(que);
        int a,b,c;
        while(que--)
        {
            read3(a,b,c);
            if(b>c) swap(b,c);
            if(a) outn(cal(1,b,c));
            else update(1,b,c);
        }
        puts("");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值