题解【SP8002】HORRIBLE - Horrible Queries

Face questions

answer

This is the template title of a segment tree.

Topic we need to maintain a data structure to support a range of modification, the query interval, it is easy to think of tree line.

And then found the Valley and Los template tree line 1 are the same questions.

Since the initial value of each number in this problem are \ (0 \) , so we do not need to contribute directly to the start of each tree node values are initialized to \ (0 \) can.

Looking for a specified time interval modification, maintenance, what \ (lazy \ tag \) and the updated value to the node.

Query and modify the same token, just look for the update interval and the interval becomes a plus.

note:

  1. Data have \ (T \) group, and therefore needs to be cleared at the beginning of each data array;
  2. \ (lazy \ tag \) mark at the final pass should pay attention to empty the current node \ (lazy \ tag \) ;
  3. We need to open \ (Long \ Long \) .

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define int long long
#define itn int
#define gI gi

using namespace std;

inline int gi()
{
    int f = 1, x = 0; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
    return f * x;
}

int n, ans[100005 << 2], tag[100005 << 2];

inline int ls(int x) {return x << 1;}//左儿子
inline int rs(int x) {return x << 1 | 1;}//右儿子

inline void push_up(int x)//上传标记
{
    ans[x] = ans[ls(x)] + ans[rs(x)];//求和
}

inline void pushdown(int p, int l, int r)//下传标记
{
    if (tag[p])//如果还有标记
    {
        ans[ls(p)] = ans[ls(p)] + tag[p] * l;
        ans[rs(p)] = ans[rs(p)] + tag[p] * r;//加上和
        tag[ls(p)] = tag[ls(p)] + tag[p];
        tag[rs(p)] = tag[rs(p)] + tag[p];//加上标记
        tag[p] = 0;//清零标记
    }
}

void upd(int nl, int nr, int l, int r, int p, int k)//更新操作
{
    if (nl <= l && nr >= r)//当前区间包含于要更新的区间
    {
        ans[p] = ans[p] + (r - l + 1) * k, tag[p] = tag[p] + k;//加上和并记录lazy标记
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(p, mid - l + 1, r - mid);//下传lazy标记
    if (nl <= mid) upd(nl, nr, l, mid, ls(p), k);//更新左区间
    if (nr > mid) upd(nl, nr, mid + 1, r, rs(p), k);//更新右区间
    push_up(p);//上传节点
}

int getans(int ql, int qr, int l, int r, int p)//求和操作,与更新同理
{
    int sum = 0;
    if (ql <= l && qr >= r) return ans[p];
    int mid = (l + r) >> 1;
    pushdown(p, mid - l + 1, r - mid);
    if (ql <= mid) sum = sum + getans(ql, qr, l, mid, ls(p));
    if (qr > mid) sum = sum + getans(ql, qr, mid + 1, r, rs(p));
    return sum;
}

signed main()
{
    int T = gi();
    while (T--)//多组数据
    {
    memset(tag, 0, sizeof(tag));
    memset(ans, 0, sizeof(ans));//多组数据记得清空数组
    int n = gi(), m = gi();
    for (int i = 1; i <= m; i++)
    {
        int fl = gi();
        if(fl == 0)//修改操作
        {
            int x = gi(), y = gi(), k = gi();
            upd(x, y, 1, n, 1, k);
        }
        else//求和操作
        {
            int x = gi(), y = gi();
            printf("%lld\n", getans(x, y, 1, n, 1));
        }
    }
    }
    return 0;//结束
}

Guess you like

Origin www.cnblogs.com/xsl19/p/11370423.html