10分求助(给每个大佬一个关注!)

P3372 【模板】线段树 1

LabmemNo_012LzTopic @ 2024-11-05 18:16:10

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
using namespace std;

#define Max 100005

int tree[Max * 4] = { 0 }, laze[Max * 4] = { 0 }, a[Max] = { 0 }, k, kk;

int creattree(int u, int l, int r)
{
    if (l == r)
    {
        return tree[u] = a[l];
    }
    int imd = (l + r) / 2;
    return tree[u] = creattree(u * 2, l, imd) + creattree(u * 2 + 1, imd + 1, r);
}

void treewh(int u, int l, int r, int L,int R)
{
    int imd = (l + r) / 2;
    if (l == L && r == R)
    {
        laze[u] = k;
        kk = (r - l + 1) * k;
        tree[u] += kk;
        return;
    }
    if (imd < L)
    {
        treewh(u * 2 + 1, imd + 1, r, L, R);
        tree[u] += kk;
    }
    else if (imd + 1 > R)
    {
        treewh(u * 2, l, imd, L, R);
        tree[u] += kk;
    }
    else
    {
        treewh(u * 2, l, imd, L, imd);
        tree[u] += kk;
        treewh(u * 2 + 1, imd + 1, r, imd + 1, R);
        tree[u] += kk;
    }
}

int querytree(int u, int l, int r, int L, int R)
{
    int imd = (l + r) / 2;
    if (l == L && r == R)return tree[u];
    if (laze[u])
    {
        laze[u * 2] = laze[u];
        laze[u * 2 + 1] = laze[u];
        tree[u * 2] += (imd - l + 1) * laze[u];
        tree[u * 2 + 1] += (r - (imd + 1) + 1) * laze[u];
        laze[u] = 0;
    }
    if (imd < L)
    {
        return querytree(u * 2 + 1, imd + 1, r, L, R);
    }
    else if (imd + 1 > R)
    {
        return querytree(u * 2, l, imd, L, R);
    }
    else
    {
        return querytree(u * 2, l, imd, L, imd) + querytree(u * 2 + 1, imd + 1, r, imd + 1, R);
    }
}

int main()
{
    int n, m, t, x, y;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    creattree(1, 1, n);
    while (m--)
    {
        cin >> t >> x >> y;
        if (t == 1)
        {
            cin >> k;
            treewh(1, 1, n, x, y);
        }
        else
        {
            printf("%d\n", querytree(1, 1, n, x, y));
        }
    }
    return 0;
}

by Fcersoka @ 2024-11-05 18:32:27

@LabmemNo_012LzTopic 改好了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
using namespace std;

#define Max 100005

long long tree[Max * 4] = { 0 }, laze[Max * 4] = { 0 }, a[Max] = { 0 }, k, kk;

int creattree(int u, int l, int r)
{
    if (l == r)
    {
        return tree[u] = a[l];
    }
    int imd = (l + r) / 2;
    return tree[u] = creattree(u * 2, l, imd) + creattree(u * 2 + 1, imd + 1, r);
}

void treewh(int u, int l, int r, int L,int R)
{
    int imd = (l + r) / 2;
    if (l >= L && r <= R)
    {
        laze[u] += k;
        kk = (r - l + 1) * k;
        tree[u] += kk;
        return;
    }
    if (laze[u])
    {
        laze[u * 2] += laze[u];
        laze[u * 2 + 1] += laze[u];
        tree[u * 2] += (imd - l + 1) * laze[u];
        tree[u * 2 + 1] += (r - (imd + 1) + 1) * laze[u];
        laze[u] = 0;
    }
    if (L <= imd)
    {
        treewh(u * 2, l, imd, L, R);
    }
    if (R > imd)
    {
        treewh(u * 2 + 1, imd + 1, r, L, R);
    }
    tree[u] = tree[u * 2] + tree[u * 2 + 1];
}

long long querytree(int u, int l, int r, int L, int R)
{
    int imd = (l + r) / 2;
    if (l >= L && r <= R)return tree[u];
    if (laze[u])
    {
        laze[u * 2] += laze[u];
        laze[u * 2 + 1] += laze[u];
        tree[u * 2] += (imd - l + 1) * laze[u];
        tree[u * 2 + 1] += (r - (imd + 1) + 1) * laze[u];
        laze[u] = 0;
    }
    long long sum = 0;
    if (L <= imd)
    {
        sum += querytree(u * 2, l, imd, L, R);
    }
    if (R > imd)
    {
        sum += querytree(u * 2 + 1, imd + 1, r, L, R);
    }
    return sum;
}

int main()
{
    int n, m, t, x, y;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    creattree(1, 1, n);
    while (m--)
    {
        cin >> t >> x >> y;
        if (t == 1)
        {
            cin >> k;
            treewh(1, 1, n, x, y);
        }
        else
        {
            printf("%lld\n", querytree(1, 1, n, x, y));
        }
    }
    return 0;
}

要改的地方很多,楼主自己看代码吧,建议学懂了线段树再来写。


by LabmemNo_012LzTopic @ 2024-11-05 18:46:03

@Fcersoka 好的!!!感谢大佬!关注奉上Orz!


|