why?玄关

P3372 【模板】线段树 1

Happy_Doggie @ 2024-12-08 10:02:43

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N], x, y, k, op, ans;
int tr[N << 2], lazy[N << 2];
void build(int id, int l, int r)
{
    if (l == r)
    {
        tr[id] = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(id * 2, l, mid);
    build(id * 2 + 1, mid + 1, r);
    tr[id] = tr[id * 2 + 1] + tr[id * 2];
}
void pushdown(int id, int l, int r)
{
    if (lazy[id] >= 1)
    {
        int mid = l + r >> 1;
        lazy[id * 2] += lazy[id];
        lazy[id * 2 + 1] += lazy[id];
        tr[id * 2] += lazy[id] * (mid - l + 1);
        tr[id * 2 + 1] += lazy[id] * (r - mid);
        lazy[id] = 0;
    }
}
void jia(int id, int l, int r, int la, int ra, int k)
{
    //  cout << 1 << endl;
    if (la <= l && ra >= r)
    {
        lazy[id]++;
        tr[id] = tr[id] + (r - l + 1) * k;
        return;
    }
    pushdown(id, l, r);
    int mid = l + r >> 1;
    if (x <= mid)
        jia(id * 2, l, mid, la, ra, k);
    if (y > mid)
        jia(id * 2 + 1, mid + 1, r, la, ra, k);
    tr[id] = tr[id * 2] + tr[id * 2 + 1];
}
int chaxun(int id, int l, int r, int la, int ra)
{
    if (la <= l && ra >= r)
        return tr[id];
    pushdown(id, l, r);
    int mid = l + r >> 1;
    if (x <= mid)
        ans += chaxun(id * 2, l, mid, la, ra);
    if (y > mid)
        ans += chaxun(id * 2 + 1, mid + 1, r, la, ra);
    return ans;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    build(1, 1, n);
    // for (int i = 1; i <= 2 * n; i++)
    //  cout << tr[i] << " ";
    while (m--)
    {
        cin >> op;
        if (op == 1)
        {
            cin >> x >> y >> k;
            jia(1, 1, n, x, y, k);
        }
        else if (op == 2)
        {
            cin >> x >> y;
            ans = 0;
            cout << chaxun(1, 1, n, x, y) << endl;
        }
    }
    return 0;
}

为什么样例也没过?


by __mcr130102__ @ 2024-12-08 10:12:35

没有错啊


|