2 条题解

  • 0
    @ 2025-10-8 16:52:14
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=1e5+10;
    struct trnode{int lc, rc, l, r; LL c, lazy; trnode() {c=lazy=0;}} tr[N*32]; int trlen;
    LL a[N], s[N], ans; int root[2*N];
    void change(int pre, int &now, int l, int r, int x, int y, LL c)
    {
        now=++trlen; tr[now]=tr[pre]; int mid=(l+r)/2;
        tr[now].c+=(min(r, y)-max(l, x)+1)*c;
        if(x<=l && y>=r) {tr[now].lazy+=c; return ;}
        if(x<=mid) change(tr[pre].lc, tr[now].lc, l, mid, x, y, c);
        if(mid<y) change(tr[pre].rc, tr[now].rc, mid+1, r, x, y, c);
    }
    void query(int now, int l, int r, int x, int y)
    {
        if(x<=l && y>=r) {ans+=tr[now].c; return ;}  int mid=(l+r)/2; 
        if(tr[now].lazy) {ans+=(min(r, y)-max(l, x)+1)*tr[now].lazy;}
        if(x<=mid) query(tr[now].lc, l, mid, x, y);
        if(mid<y) query(tr[now].rc, mid+1, r, x, y);
    }
    int main()
    {
        int n, m, now=0; scanf("%d%d", &n, &m); s[0]=0;
        for(int i=1; i<=n; i++) scanf("%lld", &a[i]), s[i]=s[i-1]+a[i];
        memset(root, 0, sizeof(root));
        for(int i=1; i<=m; i++)
        {
            int op; scanf("%d", &op);
            if(op==1)
            {
                int x, y; LL c; scanf("%d%d%lld", &x, &y, &c);
                if(x>y) swap(x, y);
                now++; change(root[now-1], root[now], 1, n, x, y, c);
            }
            else if(op==2)
            {
                int x, y; scanf("%d%d", &x, &y);
                if(x>y) swap(x, y);
                ans=s[y]-s[x-1]; query(root[now], 1, n, x, y);
                printf("%lld\n", ans);
            }
            else if(op==3)
            {
                int x, y, last; scanf("%d%d%d", &x, &y, &last);
                if(x>y) swap(x, y);
                ans=s[y]-s[x-1]; query(root[last], 1, n, x, y);
                printf("%lld\n", ans);
            }
            else
            {
                int last; scanf("%d", &last);
                now=last;
            }
        }
        return 0;
    }
    
    • 0
      @ 2025-10-8 16:52:05
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      const int N=1e5+10;
      struct trnode{int lc, rc, l, r; LL c, lazy; trnode() {c=lazy=0;}} tr[N*32]; int trlen;
      LL a[N], s[N], ans; int root[2*N];
      void change(int pre, int &now, int l, int r, int x, int y, LL c)
      {
          now=++trlen; tr[now]=tr[pre]; int mid=(l+r)/2;
          tr[now].c+=(min(r, y)-max(l, x)+1)*c;
          if(x<=l && y>=r) {tr[now].lazy+=c; return ;}
          if(x<=mid) change(tr[pre].lc, tr[now].lc, l, mid, x, y, c);
          if(mid<y) change(tr[pre].rc, tr[now].rc, mid+1, r, x, y, c);
      }
      void query(int now, int l, int r, int x, int y)
      {
          if(x<=l && y>=r) {ans+=tr[now].c; return ;}  int mid=(l+r)/2; 
          if(tr[now].lazy) {ans+=(min(r, y)-max(l, x)+1)*tr[now].lazy;}
          if(x<=mid) query(tr[now].lc, l, mid, x, y);
          if(mid<y) query(tr[now].rc, mid+1, r, x, y);
      }
      int main()
      {
          int n, m, now=0; scanf("%d%d", &n, &m); s[0]=0;
          for(int i=1; i<=n; i++) scanf("%lld", &a[i]), s[i]=s[i-1]+a[i];
          memset(root, 0, sizeof(root));
          for(int i=1; i<=m; i++)
          {
              int op; scanf("%d", &op);
              if(op==1)
              {
                  int x, y; LL c; scanf("%d%d%lld", &x, &y, &c);
                  if(x>y) swap(x, y);
                  now++; change(root[now-1], root[now], 1, n, x, y, c);
              }
              else if(op==2)
              {
                  int x, y; scanf("%d%d", &x, &y);
                  if(x>y) swap(x, y);
                  ans=s[y]-s[x-1]; query(root[now], 1, n, x, y);
                  printf("%lld\n", ans);
              }
              else if(op==3)
              {
                  int x, y, last; scanf("%d%d%d", &x, &y, &last);
                  if(x>y) swap(x, y);
                  ans=s[y]-s[x-1]; query(root[last], 1, n, x, y);
                  printf("%lld\n", ans);
              }
              else
              {
                  int last; scanf("%d", &last);
                  now=last;
              }
          }
          return 0;
      }
      • 1

      *【可持久化线段树】可持久化线段树[SPOJ11470]TTM - To the moon

      信息

      ID
      562
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      89
      已通过
      25
      上传者