最后3个WA,已开long long

P3372 【模板】线段树 1

idiotgoose @ 2023-10-31 21:23:34

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include<queue>
#include<cassert>
#include<stack>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define int long long

using namespace std;
const int maxn = 2e5 + 5;//线段树的数组大小记得开双倍
struct segment_tree
{
    int y1, y2;
    int v;
    int sumv[maxn];
    int addv[maxn];
    int _sum;
    int n;//总的区间是[0,n-1]

    void dfs(int o, int l, int r,int* arr)
    {
        if (l == r)addv[o]=sumv[o] = arr[l];
        else
        {
            int m = l + (r - l) / 2;
            dfs(o * 2, l, m,arr);
            dfs(o * 2 + 1, m + 1, r,arr);
            sumv[o] = sumv[o * 2] + sumv[o * 2 + 1];
        }
    }

    void build(int* arr, int n)
    {
        dfs(1, 0, n - 1,arr);
        this->n = n;
    }

    void maintain(int o,int l,int r)
    {
        int lc = o * 2, rc = o * 2 + 1;
        sumv[o] = 0;
        if (r>l)
        {
            sumv[o] = sumv[lc] + sumv[rc];
        }
        sumv[o] += (r-l + 1) * addv[o];
    }

    void update(int o, int l, int r)
    {
        int lc = o * 2, rc = o * 2 + 1;
        if (y1 <= l && r <= y2)
        {
            addv[o] += v;
        }
        else
        {
            int m = l + (r - l) / 2;
            if (y1 <= m)update(lc, l, m);
            if (m < y2)update(rc, m + 1, r);
        }
        maintain(o, l, r);
    }

    void query(int o, int l, int r,int add)
    {
        int lc = o * 2, rc = o * 2 + 1;
        if (y1 <= l && r <= y2)_sum += (sumv[o] + add * (r - l + 1));
        else
        {
            int m = l + (r - l) / 2;
            if (y1 <= m)query(lc, l, m, add + addv[o]);
            if (m < y2)query(rc, m + 1, r, add + addv[o]);
        }
    }

    void interval_add(int a, int b, int v)//[a,b]每个数加v
    {
        this->v = v;
        y1 = a; y2 = b;
        update(1, 0, n-1);
    }

    int interval_sum(int a, int b)
    {
        y1 = a; y2 = b;
        _sum = 0;
        query(1, 0, n - 1, 0);
        return _sum;
    }

}t;
int arr[maxn];
signed main()
{
    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)cin >> arr[i];
    t.build(arr, n);
    for (int i = 0; i < m; i++)
    {
        int op, x, y, k;
        cin >> op >> x >> y;
        x--; y--;
        if (op == 1)
        {
            cin >> k;
            t.interval_add(x, y, k);
        }
        else
        {       
            cout << t.interval_sum(x, y) << endl;
        }
    }
}

by idiotgoose @ 2023-10-31 21:23:56

求调qwq


by pjfffb @ 2023-11-01 21:25:04

@idiotgoose 我也是后三个WA了


by idiotgoose @ 2023-11-07 10:38:57

发现问题了,是数组开小了,要开成元素个数的4倍,此贴结。(但居然报的是WA不是RE,唉害的我好一顿debug啊)


|