线段树(区间修改 , 单点查询)

线段树的单点查询

  1. 先判断当前区间[l,r][l,r]是否与目标区间完全相等
inline int query(int pos, int L, int l, int r)
{

    if (L == l && r == L)
    {
        return tree[pos];
    }

    int mid = (l + r) >> 1;
    int sum = 0;
    push_down(pos, l, r);

    if (L <= mid)
    {
        sum += query(lw, L, l, mid);
    }
    else
    {
        sum += query(rw, L, mid + 1, r);
    }
	push_up(pos);
	
    return sum;
}
  1. 下发标记,请参考 就是玩儿
  2. 分解区间
  3. 上传标记

线段树的区间修改

请参考

模板

#include <bits/stdc++.h>
using namespace std;
#define lw pos << 1
#define rw pos << 1 | 1

#define int long long

/* --------------- fast io --------------- */ // begin
namespace Fread
{
    const int SIZE = 1 << 21;
    char buf[SIZE], *S, *T;
    inline char getchar()
    {
        if (S == T)
        {
            T = (S = buf) + fread(buf, 1, SIZE, stdin);
            if (S == T)
                return '\n';
        }
        return *S++;
    }
} // namespace Fread
namespace Fwrite
{
    const int SIZE = 1 << 21;
    char buf[SIZE], *S = buf, *T = buf + SIZE;
    inline void flush()
    {
        fwrite(buf, 1, S - buf, stdout);
        S = buf;
    }
    inline void putchar(char c)
    {
        *S++ = c;
        if (S == T)
            flush();
    }
    struct NTR
    {
        ~NTR() { flush(); }
    } ztr;
} // namespace Fwrite
#ifdef ONLINE_JUDGE
#define getchar Fread ::getchar
#define putchar Fwrite ::putchar
#endif
namespace Fastio
{
    struct Doublee
    {
        double x;
        int k = 6;
    };
    struct Reader
    {
        template <typename T>
        Reader &operator>>(T &x)
        {
            char c = getchar();
            T f = 1;
            while (c < '0' || c > '9')
            {
                if (c == '-')
                    f = -1;
                c = getchar();
            }
            x = 0;
            while (c >= '0' && c <= '9')
            {
                x = x * 10 + (c - '0');
                c = getchar();
            }
            x *= f;
            return *this;
        }
        Reader &operator>>(double &num)
        {
            char in;
            double Dec = 0.1;
            bool IsN = false, IsD = false;
            in = getchar();
            if (in == EOF)
            {
                return *this;
            }
            while (in != '-' && in != '.' && (in < '0' || in > '9'))
            {
                in = getchar();
            }
            if (in == '-')
            {
                IsN = true;
                num = 0;
            }
            else if (in == '.')
            {
                IsD = true;
                num = 0;
            }
            else
            {
                num = in - '0';
            }
            if (!IsD)
            {
                while (in = getchar(), in >= '0' && in <= '9')
                {
                    num *= 10;
                    num += in - '0';
                }
            }
            if (in != '.')
            {
                if (IsN)
                    num = -num;
            }
            else
            {
                while (in = getchar(), in >= '0' && in <= '9')
                {
                    num += Dec * (in - '0');
                    Dec *= 0.1;
                }
            }
            if (IsN)
            {
                num = -num;
            }
        }
        Reader &operator>>(char &c)
        {
            c = getchar();
            while (c == ' ' || c == '\n')
            {
                c = getchar();
            }
            return *this;
        }
        Reader &operator>>(char *str)
        {
            int len = 0;
            char c = getchar();
            while (c == ' ' || c == '\n')
            {
                c = getchar();
            }
            while (c != ' ' && c != '\n' && c != '\r')
            { // \r\n in windows
                str[len++] = c;
                c = getchar();
            }
            str[len] = '\0';
            return *this;
        }
        Reader() {}
    } cin;
    const char endl = '\n';
    struct Writer
    {
        Writer &operator<<(Doublee op)
        {
            static int n = pow(10, op.k);
            if (op.x == 0)
            {
                putchar('0'), putchar('.');
                for (int i = 1; i <= op.k; ++i)
                    putchar('0');
                return *this;
            }
            if (op.x < 0)
                putchar('-'), op.x = -op.x;
            long long y = (long long)(op.x * n) % n;
            op.x = (long long)op.x;
            cout << op.x;
            putchar('.');
            int bit[10], p = 0, i;
            for (; p < op.k; y /= 10)
                bit[++p] = y % 10;
            for (i = p; i > 0; i--)
                putchar(bit[i] + 48);
            return *this;
        }
        template <typename T>
        Writer &operator<<(T x)
        {
            if (x == 0)
            {
                putchar('0');
                return *this;
            }
            if (x < 0)
            {
                putchar('-');
                x = -x;
            }
            static int sta[45];
            int top = 0;
            while (x)
            {
                sta[++top] = x % 10;
                x /= 10;
            }
            while (top)
            {
                putchar(sta[top] + '0');
                --top;
            }
            return *this;
        }
        Writer &operator<<(char c)
        {
            putchar(c);
            return *this;
        }
        Writer &operator<<(char *str)
        {
            int cur = 0;
            while (str[cur])
                putchar(str[cur++]);
            return *this;
        }
        Writer &operator<<(const char *str)
        {
            int cur = 0;
            while (str[cur])
                putchar(str[cur++]);
            return *this;
        }
        Writer() {}
    } cout;
} // namespace Fastio
#define cin Fastio ::cin
#define cout Fastio ::cout
#define endl Fastio ::endl
#define Doublee Fastio::Doublee
/* --------------- fast io --------------- */ // end

const int maxn = 1e6 + 5;
int n, m;
int tree[maxn << 2], lazy[maxn << 2];
inline void push_up(int pos)
{
    tree[pos] = tree[lw] + tree[rw];
}
inline void js(int pos, int l, int r, int k)
{
    lazy[pos] += k;
    tree[pos] += k * (r - l + 1);
}
inline void push_down(int pos, int l, int r)
{
    int mid = (l + r) >> 1;
    js(lw, l, mid, lazy[pos]);
    js(rw, mid + 1, r, lazy[pos]);
    lazy[pos] = 0;
}
inline void build(int pos, int l, int r)
{
    lazy[pos] = 0;
    if (l == r)
    {
        cin >> tree[pos];
        return;
    }
    int mid = (l + r) >> 1;
    build(lw, l, mid);
    build(rw, mid + 1, r);
    push_up(pos);
}
inline void update(int pos, int left, int right, int l, int r, int val)
{
    if (left <= l && r <= right)
    {
        lazy[pos] += val;
        tree[pos] += val * (r - l + 1);
        return;
    }
    push_down(pos, l, r);
    int mid = (l + r) >> 1;
    if (left <= mid)
        update(lw, left, right, l, mid, val);
    if (right > mid)
        update(rw, left, right, mid + 1, r, val);
    push_up(pos);
}
inline int query(int pos, int L, int l, int r)
{

    if (L == l && r == L)
    {
        return tree[pos];
    }

    int mid = (l + r) >> 1;
    int sum = 0;
    push_down(pos, l, r);

    if (L <= mid)
    {
        sum += query(lw, L, l, mid);
    }
    else
    {
        sum += query(rw, L, mid + 1, r);
    }
	push_up(pos);
	
    return sum;
}
signed main()
{
    cin >> n >> m;
    build(1, 1, n);
    for (int i = 1, q, q1, q2, q3; i <= m; i++)
    {
        cin >> q >> q1;
        if (q == 1)
        {
            cin >> q2 >> q3;
            update(1, q1, q2, 1, n, q3);
        }
        else if (q == 2)
        {
            cout << query(1, q1, 1, n) << endl;
        }
    }
    return 0;
}
posted @ 2021-05-08 08:43  蒟蒻orz  阅读(23)  评论(0编辑  收藏  举报  来源