HDU 5306 Gorgeous Sequence

来源:互联网 发布:那个软件有柯南漫画 编辑:程序博客网 时间:2024/06/09 22:25

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=100n1000000, m1000000.
 

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

Sample Input
15 51 2 3 4 51 1 52 1 50 3 5 31 1 52 1 5
 

Sample Output
515312
Hint
Please use efficient IO method

 参考了这位犇的代码:http://blog.csdn.net/keshuai19940722/article/details/47343557
这个输入外挂真的是懵逼了。。。。。

#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
原创粉丝点击