Can you answer these queries? HDU - 4027 (线段树+区间修改+坑)

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.

InputThe 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.
OutputFor 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

题意:就是给你一串数,让你对一段区间操作,1的时候是询问区间和,0的时候是把这段区间的每个点开根号。

总结:

1.输入的x和y大小不保证x<y   开始疯狂WA
2.注意输出格式,每组数据之后有一个空行
3.如果区间内的所有数都是1的时候就不用更新了,要优化,不然会超时

代码

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%lld",&a) 
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int Maxn = 2e5 + 5;
const double pi = acos(-1.0);
const double eps = 1e-8;

ll a[Maxn];

struct node {
         ll l, r;//区间[l,r]
         ll add;//区间的延时标记
         ll sum;//区间和
}tree[Maxn * 4 + 5];//一定要开到4倍多的空间
    
void pushup(ll rt) { //向上更新
    tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}
void pushdown(ll rt) {//向下更新
    if (tree[rt].add) { //若有标记,则讲标记向下移动一层
        tree[rt << 1].add = tree[rt].add;
        tree[rt << 1 | 1].add = tree[rt].add;
        tree[rt << 1].sum = (tree[rt << 1].r - tree[rt << 1].l + 1) * tree[rt].add;
        tree[rt << 1 | 1].sum = (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1) * tree[rt].add;
        tree[rt].add = 0;  //取消本层标记
    }
}
void BuildTree(ll l, ll r, ll rt) {
    tree[rt].l = l;
    tree[rt].r = r;
    tree[rt].add = 0;
    //tree[rt].sum = r - l + 1; //更新结点rt的值
    if (l == r) {
        //此行根据题意看情况填什么————-
        tree[rt].sum = a[l];
        //————————————————
        return;
    }
    ll mid = (l + r) >> 1;
    BuildTree(l, mid, rt << 1); //递归左子树
    BuildTree(mid + 1, r, (rt << 1) + 1); //递归右子树
    pushup(rt);//向上更新
}
void updata(ll l, ll r, ll rt) { //root为结点 区间更新
     //   cout << l << " " << tree[rt].l << "----" << r << " " << tree[rt].r << endl; // 验证路径
    //if (l <= tree[rt].l && r >= tree[rt].r) {
    if(tree[rt].l == tree[rt].r){
        tree[rt].sum = (ll)sqrt(tree[rt].sum);
        return;
    } /////如果区间内的所有数都是1则不必更新
    if(tree[rt].sum == tree[rt].r - tree[rt].l + 1) return;
    pushdown(rt);
    ll mid = (tree[rt].l + tree[rt].r) >> 1;
    if (l <= mid) {
        updata(l, r, rt << 1);
    }
    if (r > mid) {
        updata(l, r, rt << 1 | 1);
    }
    pushup(rt);//向上更新
}
    
ll querySum(ll l, ll r, ll rt) { //区间求和
    if (l <= tree[rt].l && r >= tree[rt].r) {
        return tree[rt].sum;
    }
    pushdown(rt);
    ll mid = (tree[rt].l + tree[rt].r) >> 1;
    ll ans = 0;
    ll Max = 0;
    ll Min = INF;
    if (l <= mid) {
        ans += querySum(l, r, rt << 1);
    }
    if (r > mid) {
        ans += querySum(l, r, rt << 1 | 1);
    }
    return ans;

}
    
int main()
{
    ll n, m, x, y, z, t, cas = 0;
    while (~sc(n)) {
        FOR(i, 1, n) sc(a[i]);
        BuildTree(1, n, 1);
        sc(m);
        printf("Case #%lld:\n", ++cas);
        while (m--) {
            sc(x), sc(y), sc(z);
            if (y > z) swap(y, z);
            if (x == 0) {
                updata(y, z, 1);
            }
            if (x == 1) {
                printf("%lld\n", querySum(y, z, 1));
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AlexLINS/p/12650936.html