HDU 5306 Gorgeous Sequence

Gorgeous Sequence


Problem Description
There is a sequence  a  of length  n . We use  ai  to denote the  i -th element in this sequence. You should do the following three types of operations to this sequence.

0 x y t : For every  xiy , we use  min(ai,t)  to replace the original  ai 's value.
1 x y : Print the maximum value of  ai  that  xiy .
2 x y : Print the sum of  ai  that  xiy .
 

Input
The first line of the input is a single integer  T , indicating the number of testcases. 

The first line contains two integers  n  and  m  denoting the length of the sequence and the number of operations.

The second line contains  n  separated integers  a1,,an  ( 1in,0ai<231 ).

Each of the following  m  lines represents one operation ( 1xyn,0t<231 ).

It is guaranteed that  T=100 n1000000, m1000000 .
 

Output
For every operation of type  1  or  2 , print one line containing the answer to the corresponding query.
 

Sample Input
  
  
1 5 5 1 2 3 4 5 1 1 5 2 1 5 0 3 5 3 1 1 5 2 1 5
 

Sample Output
  
  
5 15 3 12
Hint
Please use efficient IO method

这个输入外挂真的是懵逼了。。。。。

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define endl "\n"
#define lson(u) (u << 1)
#define rson(u) (u << 1 | 1)
const int MAXN = 1000000 + 5;

/*******************************
 *  输入外挂,只适合数字
 *******************************/
/*********** input *************/
char *ch, *ch1, buf[40*1024000+5], buf1[40*1024000+5];
void read(int &x)   {
    for (++ch; *ch <= 32; ++ch);
    for (x = 0; '0' <= *ch; ch++)    x = x * 10 + *ch - '0';
}
/******************************/

struct segNode{
    int L, R, Max, T, Cnt;
    ll Sum;
}node[MAXN << 2];

inline void PushUp(int u) {
    node[u].Max = max(node[lson(u)].Max, node[rson(u)].Max);
    node[u].Sum = node[lson(u)].Sum + node[rson(u)].Sum;
    node[u].Cnt = node[lson(u)].Cnt + node[rson(u)].Cnt;
}

inline void Update(int u, int alter) {
    if(node[u].T != 0 && node[u].T <= alter) return ;
    node[u].T = alter;
    if(node[u].Cnt != node[u].R - node[u].L + 1) {
        node[u].Max = alter;
        node[u].Sum += 1LL * (node[u].R - node[u].L + 1 - node[u].Cnt) * alter;
        node[u].Cnt = node[u].R - node[u].L + 1;
    }
}

void dfs(int u, int alter) {
    if(node[u].Max < alter) return ;
    node[u].T = 0;
    if(node[u].L == node[u].R) {
        node[u].Sum = node[u].Max = node[u].Cnt = 0;
        return ;
    }
    dfs(lson(u), alter);
    dfs(rson(u), alter);
    PushUp(u);
}

inline void PushDown(int u) {
    if(node[u].T == 0) return ;
    Update(lson(u), node[u].T);
    Update(rson(u), node[u].T);
}

void build(int u, int l, int r) {
    node[u].L = l, node[u].R = r, node[u].T = 0;
    if(l == r) {
        //scanf("%d", &node[u].Max);
        //cin >> node[u].Max;
        read(node[u].Max);
        node[u].T = node[u].Sum = node[u].Max;
        node[u].Cnt = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson(u), l, mid);
    build(rson(u), mid+1, r);
    PushUp(u);
}

void Modify(int u, int l, int r, int alter) {
    if(node[u].Max <= alter) return ;
    if(l <= node[u].L && node[u].R <= r) {
        dfs(u, alter);
        Update(u, alter);
        return ;
    }
    int mid = (node[u].L + node[u].R) >> 1;
    PushDown(u);
    if(l <= mid) {
        Modify(lson(u), l, r, alter);
    }
    if(r > mid) {
        Modify(rson(u), l, r, alter);
    }
    PushUp(u);
}

void Query(int u, int l, int r, ll& sum, int& mx) {
    if(l <= node[u].L && node[u].R <= r) {
        sum += node[u].Sum;
        mx = max(mx, node[u].Max);
        return ;
    }
    int mid = (node[u].L + node[u].R) >> 1;
    PushDown(u);
    if(l <= mid) {
        Query(lson(u), l, r, sum, mx);
    }
    if(r > mid) {
        Query(rson(u), l, r, sum, mx);
    }
    PushUp(u);
}

int main() {
    //ios::sync_with_stdio(false);
    ch = buf - 1;
    ch1 = buf1 - 1;
    fread(buf, 1, 1000 * 35 * 1024, stdin);
    int T, n, m, op, x, y, num, mx;
    ll sum;
    read(T);
    //cout << T << "   MotherFucker" << endl;
    //cin >> T;
    while(T--) {
        //cin >> n >> m;
        read(n), read(m);
        build(1, 1, n);
        for(int i = 0; i < m; ++i) {
            read(op), read(x), read(y);
            //cin >> op >> x >> y;
            if(op) {
                sum = mx = 0;
                Query(1, x, y, sum , mx);
                if(op == 1) {
                    printf("%d\n", mx);
                    //cout << mx << endl;
                }
                else {
                    //cout << sum << endl;
                    printf("%lld\n", sum);
                }
            }
            else {
                read(num);
                //cin >> num;
                Modify(1, x, y, num);
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值