求助,最后三个数据TLE了
查看原帖
求助,最后三个数据TLE了
405001
TheSunInTheMorning楼主2021/4/13 20:41
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
#define lson root << 1
#define rson root <<1|1
const  ll MAXN = 1e6+3;
ll input[MAXN];

struct Node
{
    ll l, r ;
    
    ll val, lazy;
} tr[10*MAXN];

inline void Push_up(ll root)
{
    tr[root].val = tr[lson].val + tr[rson].val;
}

inline void Push_dowm(ll root)
{   
    if(tr[root].lazy==0)
        return;
    tr[lson].val += tr[root].lazy * (tr[lson].r - tr[lson].l + 1);
    tr[rson].val += tr[root].lazy * (tr[rson].r - tr[rson].l + 1);

    tr[lson].lazy += tr[root].lazy;
    tr[rson].lazy += tr[root].lazy;

    tr[root].lazy = 0;
}


void Build_Tree(ll root , ll l , ll r)
{
    tr[root].l = l, tr[root].r = r;
    if(l==r)
    {
        tr[root].val = input[l];
        return;
    }
    int mid = (l + r) >> 1;
    Build_Tree(lson, l, mid);
    Build_Tree(rson, mid + 1, r);
    Push_up(root);
}

void Segment_Add(ll root, ll l, ll r, ll x)
{
    Push_dowm(root);
    if(tr[root].l==l && tr[root].r==r)
    {
        tr[root].val += x * (tr[root].r - tr[root].l + 1);
        tr[root].lazy += x;
        return;
    }
    int mid = (tr[root].l + tr[root].r) >> 1;
    if(r<=mid)
        Segment_Add(lson, l, r, x);
    else if(mid<l)
        Segment_Add(rson, l, r, x);
    else 
    {
        Segment_Add(lson, l, mid, x);
        Segment_Add(rson, mid + 1, r, x);
    }
    Push_up(root);
}   



    void Point_Add(int root, int dis, int val)
{
    if(tr[root].l==tr[root].r)
    {
        tr[root].val += val;
        return;
    }
    int mid = (tr[root].l + tr[root].r) >> 1;
    if(mid>=dis)
    {
        Point_Add(lson, dis, val);
    }
    else
        Point_Add(rson, dis, val);
    Push_up(root);
}

ll Segment_Query(ll root , ll l , ll r)
{
    Push_dowm(root);
    if(tr[root].l==tr[root].r)
    {
        return tr[root].val;
    }
    int mid = (tr[root].l + tr[root].r) >> 1;
    if(mid>=r)
        return Segment_Query(lson, l, r);
    else if(mid<l)
        return Segment_Query(rson, l, r);
    else return Segment_Query(lson,l,mid)+Segment_Query(rson,mid+1,r);
}
// int pow2[20] = {1, 1, 2, 4, 8, 16, 32};
// void Print_tree(int deep , int num)
// {
//    if(pow2[deep+1]-pow2[deep]>=num)
//    {
//        for (int i = pow2[deep];i<=pow2[deep]+num-1 ; ++i)
//        {
//            //Push_dowm(i);
//            cout << tr[i].val << " ";
//        }
//        cout << "\n";
//        return;
//    }
//    for (int i = pow2[deep]; i <= pow2[deep + 1] - 1; ++i)
//    {
//        //Push_dowm(i);
//        cout << tr[i].val << " ";
//    }
//    cout << "\n";
//    Print_tree(deep + 1, num);
// }
int main()
{
    ll n, m;
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%lld", &input[i]);
    }
    Build_Tree(1, 1, n);
    for (int i = 1; i <= m; ++i)
    {
        int flag;
        scanf("%d", &flag);
        if(flag==1)
        {
            ll x, y;
            ll k;
            scanf("%lld%lld%lld", &x, &y, &k);
            Segment_Add(1, x, y, k);
        }
        else
        {
            ll x, y;
            scanf("%lld%lld", &x, &y);
            printf("%lld\n", Segment_Query(1, x, y));
        }
    }
}
2021/4/13 20:41
加载中...