代码实现写挂求调

P3372 【模板】线段树 1

zhizhizhiwang @ 2024-10-24 21:30:59

脑洞大开想出了一种新的代码结构, 然后成功的让exe停止工作了。求调()

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdint.h>

using namespace std;

template <typename T>
void read(T &x)
{
    int f = 1;
    x = 0;
    char ch = getchar();
    while(!isdigit(ch))
    {
        if(ch == '-')f = -f;
        ch = getchar();
    }
    while(isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    x = f * x;
}

template<typename T, typename... Args>
void read(T &x, Args & ... args)
{
    read<T>(x);
    read(args...);
    return ;
}

namespace local
{
    #define int32_t int64_t
    const int N = 2e5 + 10;

    int32_t w[N];   
    int64_t q, n, W;
    int32_t m;

    struct Node;

    extern Node tree[];

    struct Node
    {
        int32_t     l, r;
        int32_t     num;
        int32_t     add;
        int32_t     n;
        Node&       left() {return tree[n << 1];}
        int32_t     left_idx() {return n << 1;}
        Node&       right() {return tree[n << 1 | 1];}
        int32_t     right_idx() {return n << 1 | 1;}    
    };

    Node tree[4 * N];

    void tree_init()
    {
        tree[1].n = 1;
    }

    void push_up(Node &n)
    {
        n.num = n.left().num + n.right().num;
    }

    void build(Node &n, int32_t l, int32_t r)
    {
        if(l == r)
        {
            n.l = l;
            n.r = r;
            n.num = w[l];
            n.add = 0;
        }
        else
        {
            n.left().n = n.left_idx();
            n.right().n = n.right_idx();
            int32_t mid = r + ((l - r) >> 1);
            build(n.left(), l, mid);
            build(n.right(), mid + 1, r);
            push_up(n);
        }
    }

    void eval(Node &n, Node &fa)
    {
        n.num += (n.r - n.l) * fa.add;
        n.add += fa.add;
    }

    void eval(Node &n, int32_t add)
    {
        n.num += (n.r - n.l) * add;
        n.add += add;
    }

    void push_down(Node &n)
    {
        eval(n.right(), n);
        eval(n.left() , n);

        n.add = 0;
    }

    void modify(Node &n, int32_t l, int32_t r,int32_t add )
    {
        if(n.l >= l and n.r <= r) eval(n, add);
        else
        {
            push_down(n);
            int32_t mid = n.r + ((n.r - n.l) >> 1);
            if(l <= mid) modify(n.left() , l, r, add);
            if(r > mid)  modify(n.right(), l, r, add);
            push_up(n);
        }
    }

    int32_t query(Node &u, int32_t l, int32_t r)
    {
        if(u.l >= l and u.r <= r) return u.num;

        int32_t mid = u.l + ((u.r - u.l) >> 1);
        int32_t sum = 0;

        push_down(u);
        if(l <= mid) sum += query(u.left() , l, r);
        if(r > mid)  sum += query(u.right(), l, r);
        return sum;
    }
}

int main()
{
    using local::build;using local::tree_init;using local::modify;using local::query;
    using local::n;using local::m;using local::w;

    scanf("%lld %lld", &n, &m);

    for(int i = 1;i <= n;i++)
    {
        cin >> w[i];
    }
    for(int op, x, y, k, i = 1;i <= m;i ++)
    {
        cin >> op >> x >> y;
        if(op == 1)
        {
            cin >> k;
            modify(local::tree[1], x, y, k);
        }else if(op == 2)
        {
            printf("%lld", query(local::tree[1], x, y));
        }
    }

}

by wjr_jok @ 2024-10-24 21:38:24

光放个代码看个集贸啊


by zhizhizhiwang @ 2024-10-24 21:42:39

@wjr_jok 就输入样例1运行时直接崩溃了(


by zhizhizhiwang @ 2024-10-25 19:59:02

已解决, 写完函数记得去调用()


|