[SP11470]TTM - To the moon

To The Monn

 区间加的可持久化线段树

懒标记不再适用,将标记永久化,统计答案时累加即可。

#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm>

using namespace std;

#define LL long long
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO debug("GO\n")

inline int rint() {
    register int x = 0, f = 1; register char c;
    while (!isdigit(c = getchar())) if (c == '-') f = -1;
    while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar()));
    return x * f;
}

template<typename T> inline void chkmin(T &a, T b) { a > b ? a = b : 0; }
template<typename T> inline void chkmax(T &a, T b) { a < b ? a = b : 0; }

const int maxN = 1e5 + 10, maxM = 2e7;

int n, m, now, a[maxN];
int rt[maxN], cnt;

#define ls(x) (tree[x].ls)
#define rs(x) (tree[x].rs)
struct Node {
    int ls, rs;
    LL sum, tag;
} tree[maxM];

void Build(int &x, int l, int r) {
    x = ++cnt;
    if (l == r) {
        tree[x].sum = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    Build(ls(x), l, mid); Build(rs(x), mid + 1, r);
    tree[x].sum = tree[ls(x)].sum + tree[rs(x)].sum;
}

void Add(int last, int &x, int l, int r, int L, int R, int add) {
    tree[x = ++cnt] = tree[last];
    tree[x].sum += 1ll * (min(R, r) - max(L, l) + 1) * add;
    if (L <= l and r <= R) {
        tree[x].tag += add;
        return;
    }
    int mid = (l + r) >> 1;
    if (L <= mid) Add(ls(last), ls(x), l, mid, L, R, add);
    if (mid < R) Add(rs(last), rs(x), mid + 1, r, L, R, add);
}

LL query(int x, int l, int r, int L, int R) {
    if (L <= l and r <= R)
        return tree[x].sum;
    int mid = (l + r) >> 1;
    LL res = 0;
    if (L <= mid) res += query(ls(x), l, mid, L, R);
    if (mid < R) res += query(rs(x), mid + 1, r, L, R);
    return res + 1ll * tree[x].tag * (min(R, r) - max(L, l) + 1);
}

signed main() {
   n = rint(), m = rint();
    for (int i = 1; i <= n; ++ i) a[i] = rint();
    Build(rt[0], 1, n);
    char op[30];
    int a, b, c;
    while (m--) {
        scanf("%s", op);
        switch(op[0]) {
        case 'C' :
            a = rint(), b = rint(), c = rint(); now++;
            Add(rt[now-1], rt[now], 1, n, a, b, c); break;
        case 'Q' :
            a = rint(), b = rint();
            printf("%lld\n", query(rt[now], 1, n, a, b)); break;
        case 'H' :
            a = rint(), b = rint(), c = rint();
            printf("%lld\n", query(rt[c], 1, n, a, b)); break;
        case 'B' :
            now = rint(); break;
        }
    }
}

原文地址:https://www.cnblogs.com/HNYLMSTea/p/10574722.html

时间: 2024-10-03 21:04:48

[SP11470]TTM - To the moon的相关文章

SP11470 TTM - To the moon[主席树标记永久化]

SP11470 TTM - To the moon C l r d:区间 \([L,R]\) 中的数都加 d ,同时当前的时间戳加 1. Q l r:查询当前时间戳区间 \([L,R]\) 中所有数的和 . H l r t:查询时间戳 \(t\) 区间 \([L,R]\) 的和 . B t:将当前时间戳置为 \(t\) 每次记下版本 \(times\) 保留标记-每次标记的时候直接加上\(tag_p\) 即可 #include <bits/stdc++.h> #define ls(x) ch[

HDU 4348 / SPOJ TTM - To the moon

终于过了..感谢xiaodao提供测试数据,然后最终找到了一个十分ruozhi的BUG...= =,坑爹..没什么好说的,直接上主席树的干活...下面是HDU的代码,如果是SPOJ自行把%I64d换成%lld继续干活.. 1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:hdu4348 5 */ 6 #include <cassert> 7 #include <cstdio> 8 #include <cstring> 9 #include <i

hdu 5318 The Goddess Of The Moon(矩阵快速幂)

题目链接:hdu 5318 The Goddess Of The Moon 将50个串处理成50*50的矩阵,注意重复串. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 55; const int mod = 1e9+7; int N, M, A[maxn]; struct M

FZU 2148 Moon Game 判断凸边形

点击打开链接 Problem 2148 Moon Game Accept: 512    Submit: 1419 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a k

FZOJ Problem 2148 Moon Game

                                                                                              Problem 2148 Moon Game Problem Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consid

ACM学习历程—FZU2148 Moon Game(计算几何)

Moon Game Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a kind of two-dimensional plane. Then Fat brother starts to draw N starts in the sky which we can just consid

ACM: FZU 2148 Moon Game - 海伦公式

FZU 2148  Moon Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a kind of two-dimensio

1583: [Usaco2009 Mar]Moon Mooing 哞哞叫

1583: [Usaco2009 Mar]Moon Mooing 哞哞叫 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 244  Solved: 126[Submit][Status][Discuss] Description Input 第一行两个数,C和N 第二行3个数,a1,b1,c1 第三行3个数,a2,b2,c2 Output 一个整数代表最长的那一次嚎叫 Sample Input 3 10 4 3 3 17 8 2 Sample Out

hdu 4348 To the moon(主席树区间操作)

题目链接:hdu 4348 To the moon 题意: 给你n个数,有m个操作. 1.给区间[l,r]的所有数+d,并且时间戳+1 2.询问当前时间戳的区间和. 3.询问过去时间戳t的区间和. 4.退回到时间戳t. 题解: 直接上主席树. 不过区间操作的时候push_down空间似乎不是那么够用. 所有把push_down这个操作去掉. 用一个标记记录当前这个区间的累加. 询问的时候就将这个累加传下去.(具体看代码) 最后还有退回状态t的时候可以把cnt=root[t+1], 因为后面的内存