【HDU5828】Rikka with Sequence(线段树)

来源:互联网 发布:测试手机信号强度软件 编辑:程序博客网 时间:2024/05/23 15:45

记录一个菜逼的成长。。

参考博客: http://blog.csdn.net/xtttgo/article/details/52184771

无限T之后,参考了上面的博客。终于过了。。

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdlib>#include <vector>#include <set>#include <map>#include <queue>#include <stack>#include <list>#include <deque>#include <cctype>#include <bitset>#include <cmath>using namespace std;#define ALL(v) (v).begin(),(v).end()#define cl(a) memset(a,0,sizeof(a))#define fin freopen("D://in.txt","r",stdin)#define fout freopen("D://out.txt","w",stdout)#define lson t<<1,l,mid#define rson t<<1|1,mid+1,r#define seglen (node[t].r-node[t].l+1)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;typedef vector<PII> VPII;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;template <typename T>inline void read(T &x){    T ans=0;    char last=' ',ch=getchar();    while(ch<'0' || ch>'9')last=ch,ch=getchar();    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();    if(last=='-')ans=-ans;    x = ans;}inline bool DBread(double &num){    char in;double Dec=0.1;    bool IsN=false,IsD=false;    in=getchar();    if(in==EOF) return false;    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))        in=getchar();    if(in=='-'){IsN=true;num=0;}    else if(in=='.'){IsD=true;num=0;}    else num=in-'0';    if(!IsD){        while(in=getchar(),in>='0'&&in<='9'){            num*=10;num+=in-'0';}    }    if(in!='.'){        if(IsN) num=-num;            return true;    }else{        while(in=getchar(),in>='0'&&in<='9'){                num+=Dec*(in-'0');Dec*=0.1;        }    }    if(IsN) num=-num;    return true;}template <typename T>inline void write(T a) {    if(a < 0) { putchar('-'); a = -a; }    if(a >= 10) write(a / 10);    putchar(a % 10 + '0');}/******************head***********************/const int maxn = 100000 + 10;struct Node{    int l,r;    LL mx,mn,sum,lazy,cover;}node[maxn * 4];LL ans;void pushup(int t){    node[t].sum = node[t<<1].sum + node[t<<1|1].sum;    node[t].mx = max(node[t<<1].mx,node[t<<1|1].mx);    node[t].mn = min(node[t<<1].mn,node[t<<1|1].mn);}void pushdown(int t){    int mid = (node[t].r + node[t].l) >> 1;    LL llen = mid - node[t].l + 1;    LL rlen = node[t].r - mid ;    if(node[t].cover != 0){        LL tmp = node[t].cover;        node[t<<1].cover = node[t<<1|1].cover = tmp;        node[t<<1].sum = llen * tmp;        node[t<<1|1].sum = rlen * tmp;        node[t<<1].mx = node[t<<1].mn = node[t<<1|1].mx = node[t<<1|1].mn = node[t].cover;        node[t<<1].lazy = node[t<<1|1].lazy = 0;        node[t].cover = 0;    }    if(node[t].lazy != 0){        LL tmp = node[t].lazy;        node[t<<1].lazy += tmp;        node[t<<1|1].lazy += tmp;        node[t<<1].sum += tmp * llen;        node[t<<1|1].sum += tmp * rlen;        node[t<<1].mx += tmp;node[t<<1].mn += tmp;        node[t<<1|1].mx += tmp;node[t<<1|1].mn += tmp;        node[t].lazy = 0;    }}void build(int t,int l,int r){    node[t].l = l;    node[t].r = r;    node[t].cover = node[t].lazy = node[t].sum = 0;    if(l == r){        read(node[t].sum);        node[t].mx = node[t].mn = node[t].sum;        return ;    }    int mid = (l + r) >> 1;    build(lson);    build(rson);    pushup(t);}void update1(int t,int l,int r,int v){    if(l <= node[t].l && node[t].r <= r){        node[t].sum += (LL)v * seglen;        node[t].lazy += (LL)v;        node[t].mx += (LL)v;        node[t].mn += (LL)v;        return ;    }    pushdown(t);    int mid = (node[t].l + node[t].r)>> 1;    if(l <= mid)update1(t<<1,l,r,v);    if(r > mid)update1(t<<1|1,l,r,v);    pushup(t);}void update2(int t,int l,int r){    if(l <= node[t].l && node[t].r <= r){        if(node[t].mx == 1)return ;        if(node[t].l == node[t].r){            node[t].sum = floor(sqrt(node[t].sum));            node[t].mx = node[t].mn = node[t].sum;            return ;        }        if(node[t].mx == node[t].mn){            LL tmp = node[t].mx;            node[t].mx = node[t].mn = floor(sqrt(tmp));            node[t].lazy += node[t].mx - tmp;            node[t].sum = node[t].mx * seglen;            return ;        }        else if(node[t].mx - node[t].mn == 1){            LL ta = node[t].mx,tb = node[t].mn;            LL mxnum = node[t].sum - tb * seglen;            LL mnnum = seglen - mxnum;            node[t].mx = floor(sqrt(node[t].mx));            node[t].mn = floor(sqrt(node[t].mn));            if(node[t].mx - node[t].mn == 1){                node[t].lazy += node[t].mx - ta;                node[t].sum = node[t].mx * mxnum + node[t].mn * mnnum;            }            else {                node[t].lazy = 0;                node[t].cover = node[t].mx;                node[t].sum = node[t].mx * seglen;            }            return ;        }        pushdown(t);        int mid = (node[t].l + node[t].r)>> 1;        update2(t<<1,l,r);        update2(t<<1|1,l,r);        pushup(t);        return ;    }    pushdown(t);    int mid = (node[t].l + node[t].r)>> 1;    if(l <= mid)update2(t<<1,l,r);    if(r > mid)update2(t<<1|1,l,r);    pushup(t);}void query(int t,int l,int r){    if(node[t].l == l && r == node[t].r){        ans += node[t].sum;        return ;    }    pushdown(t);    int mid = (node[t].l + node[t].r) >> 1;    if(l > mid)query(t<<1|1,l,r);    else if(r <= mid)query(t<<1,l,r);    else {        query(lson);        query(rson);    }}int main(){    //fin;    //fout;    int T;    read(T);    while(T--){        int n,m;        read(n),read(m);        build(1,1,n);        int ope,l,r,x;        while(m--){            read(ope),read(l),read(r);            if(ope == 1){                read(x);                update1(1,l,r,x);            }            else if(ope == 2){                update2(1,l,r);            }            else {                ans = 0;                query(1,l,r);                write(ans);                puts("");            }        }    }    return 0;}
0 0