HDU - 4027 H - Can you answer these queries? 区间开方更新+暴力

Can you answer these queries?

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


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 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
 
   
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

说实话,这题确挺坑的.

为什么更新查询时的x, y可以x > y.题意不是说 between the X-th and Y-th battleship 这句话难道没有默认x <= y?

题解:

op == 0 时 将 [x,y] 中所有的数开平方. 这个操作对于线段树来看似无能为力,因为不能使用lz[]数组延迟标记.

所以更新的话必须更新到叶节点,对于每一个叶节点进行更新.这样的话每次更新的时间复杂度是O(n)

显然会TLE


但是需要注意到的是其中的数最大就是2^63. 而2^63最多经过7次开方后就会变成1,之后就不会变了.

思路1:

当更新操作时,[l,r]区间的和为 l - r + 1 时,表示该区间的值都为1,这个时候就不需要更新叶节点了.

可是这样存在一个问题,当初始时有0的时候,就不能仅仅依靠[l,r]区间的和为l-r+1来判断.

但是这样依旧能过,也许数据中没有为0的情况吧

代码:

#include
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll arr[maxn];
struct SegTree
{
    ll sum[maxn<<2];
    void init() {
        memset(sum,0,sizeof(sum));
    }
    void push_up(int rt) {
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    }
    void build(ll arr[],int l,int r,int rt) {
        if(l == r) {
            sum[rt] = arr[l];
            return ;
        }
        int mid = (l + r) >> 1;
        build(arr,l,mid,rt<<1);
        build(arr,mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void updates(int l,int r,int rt) {
        if(sum[rt] == (r-l+1)) return;
        if(l == r) {
            sum[rt] = ll(sqrt(sum[rt]));
            return ;
        }
        int mid = (l+r)>>1;
        updates(l,mid,rt<<1);
        updates(mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void update(int ql,int qr,int l,int r,int rt) {
        if(ql == l && qr == r) {
            if(sum[rt] != (r-l+1)) updates(l,r,rt);
            return;
        }
        int mid = (l + r)>>1;
        if(qr <= mid) update(ql,qr,l,mid,rt<<1);
        else if(ql > mid) update(ql,qr,mid+1,r,rt<<1|1);
        else {
            update(ql,mid,l,mid,rt<<1);
            update(mid+1,qr,mid+1,r,rt<<1|1);
        }
        push_up(rt);
    }
    ll query(int ql,int qr,int l,int r,int rt) {
        if(ql == l && qr == r) {
            return sum[rt];
        }
        int mid = (l + r) >> 1;
        if(qr <= mid) return query(ql,qr,l,mid,rt<<1);
        if(ql > mid) return query(ql,qr,mid+1,r,rt<<1|1);
        return query(ql,mid,l,mid,rt<<1) + query(mid+1,qr,mid+1,r,rt<<1|1);
    }
}seg;
int main()
{
    int n,cas = 0;
    while(~scanf("%d",&n))
    {
        printf("Case #%d:\n",++cas);
        for(int i=1;i<=n;i++) scanf("%lld",&arr[i]);
        seg.build(arr,1,n,1);
        int q;scanf("%d",&q);
        while(q--) {
            int op,l,r;scanf("%d%d%d",&op,&l,&r);
            if(l > r) l^=r,r^=l,l^=r;
            if(op) 
                printf("%lld\n",seg.query(l,r,1,n,1));
            else 
                seg.update(l,r,1,n,1);
        }
        printf("\n");
    }
    return 0;
}

思路2:

根据其中的数最多经过7次开方变成1后就不会再变的性质.我们只需要用一个数组记录下每个节点被更新多少次,超过7次后我们就不需要向下更新了.显然这是最正确的做法

#include
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll arr[maxn];
struct SegTree
{
    ll sum[maxn<<2],lz[maxn<<2];
    void init() {
        memset(sum,0,sizeof(sum));
        memset(lz,0,sizeof(lz));
    }
    void push_up(int rt) {
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    }
    void build(ll arr[],int l,int r,int rt) {
        if(l == r) {
            sum[rt] = arr[l];
            lz[rt] = 0;
            return ;
        }
        int mid = (l + r) >> 1;
        build(arr,l,mid,rt<<1);
        build(arr,mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void updates(int l,int r,int rt) {
        if(l == r) {
            sum[rt] = ll(sqrt(sum[rt]));
            return ;
        }
        int mid = (l+r)>>1;
        updates(l,mid,rt<<1);
        updates(mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void update(int ql,int qr,int l,int r,int rt) {
        if(ql == l && qr == r) {
            if(lz[rt] <= 7) updates(l,r,rt),lz[rt]++;
            return;
        }
        int mid = (l + r)>>1;
        if(qr <= mid) update(ql,qr,l,mid,rt<<1);
        else if(ql > mid) update(ql,qr,mid+1,r,rt<<1|1);
        else {
            update(ql,mid,l,mid,rt<<1);
            update(mid+1,qr,mid+1,r,rt<<1|1);
        }
        push_up(rt);
    }
    ll query(int ql,int qr,int l,int r,int rt) {
        if(ql == l && qr == r) {
            return sum[rt];
        }
        int mid = (l + r) >> 1;
        if(qr <= mid) return query(ql,qr,l,mid,rt<<1);
        if(ql > mid) return query(ql,qr,mid+1,r,rt<<1|1);
        return query(ql,mid,l,mid,rt<<1) + query(mid+1,qr,mid+1,r,rt<<1|1);
    }
}seg;
int main()
{
    int n,cas = 0;
    while(~scanf("%d",&n))
    {
        printf("Case #%d:\n",++cas);
        for(int i=1;i<=n;i++) scanf("%lld",&arr[i]);
        seg.init();
        seg.build(arr,1,n,1);
        int q;scanf("%d",&q);
        while(q--) {
            int op,l,r;scanf("%d%d%d",&op,&l,&r);
            if(l > r) l^=r,r^=l,l^=r;
            if(op) 
                printf("%lld\n",seg.query(l,r,1,n,1));
            else 
                seg.update(l,r,1,n,1);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(线段树)