• BZOJ 4765 普通计算姬 (分块 + BIT)


    4765: 普通计算姬

    Time Limit: 30 Sec  Memory Limit: 256 MB
    Submit: 1547  Solved: 329
    [Submit][Status][Discuss]

    Description

    "奋战三星期,造台计算机"。小G响应号召,花了三小时造了台普通计算姬。普通计算姬比普通计算机要厉害一些
    。普通计算机能计算数列区间和,而普通计算姬能计算树中子树和。更具体地,小G的计算姬可以解决这么个问题
    :给定一棵n个节点的带权树,节点编号为1到n,以root为根,设sum[p]表示以点p为根的这棵子树中所有节点的权
    值和。计算姬支持下列两种操作:
    1 给定两个整数u,v,修改点u的权值为v。
    2 给定两个整数l,r,计算sum[l]+sum[l+1]+....+sum[r-1]+sum[r]
    尽管计算姬可以很快完成这个问题,可是小G并不知道它的答案是否正确,你能帮助他吗?
     

    Input

    第一行两个整数n,m,表示树的节点数与操作次数。
    接下来一行n个整数,第i个整数di表示点i的初始权值。
    接下来n行每行两个整数ai,bi,表示一条树上的边,若ai=0则说明bi是根。
    接下来m行每行三个整数,第一个整数op表示操作类型。
    若op=1则接下来两个整数u,v表示将点u的权值修改为v。
    若op=2则接下来两个整数l,r表示询问。
    N<=10^5,M<=10^5
    0<=Di,V<2^31,1<=L<=R<=N,1<=U<=N
     

    Output

    对每个操作类型2输出一行一个整数表示答案。
     

    Sample Input

    6 4
    0 0 3 4 0 1
    0 1
    1 2
    2 3
    2 4
    3 5
    5 6
    2 1 2
    1 1 1
    2 3 6
    2 3 5

    Sample Output

    16
    10
    9

    HINT

     

    Source

    析:首先是把这棵树进行dfs序,然后要对原序列进行分块,sum[i] 表示第 i 块,查询好说,直接整块的就整块查,不整块的就单独查用BIT即可,这个修改不好操作,可以再预处理出来f[i][j] 表示 j 个点对块 i 的影响,也就是要块 i 中出现多少次,有了这个我们就可以直接修改了,也就是 f[i][j] * det,也就差值,f[i][j] 和 sum[i] 都可以在dfs中处理出来。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-4;
    const int maxn = 1e5 + 10;
    const int maxm = 2e5 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, -1, 0, 1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    const int SIZE = 320;
    int a[maxn];
    
    struct Edge{
      int to, next;
    };
    Edge edges[maxn<<1];
    int head[maxn], cnt, root;
    ULL c[maxn], sum[maxn/SIZE+10];
    int f[maxn/SIZE+10][maxn];
    int in[maxn], out[maxn], pos[maxn], num[maxn];
    int idx, b;
    
    void add(int x, ULL val, bool ok){
      if(ok) while(x <= n){
        c[x] += val;
        x += -x&x;
      }
      else while(x <= n){
        c[x] -= val;
        x += -x&x;
      }
    }
    
    ULL query(int x){
      ULL ans = 0;
      while(x){
        ans += c[x];
        x -= -x&x;
      }
      return ans;
    }
    
    void addEdge(int u, int v){
      edges[cnt].to = v;
      edges[cnt].next = head[u];
      head[u] = cnt++;
    }
    
    ULL dfs(int u, int fa){
      in[u] = ++idx;
      ++num[pos[u]];
      ULL ans = a[u];
      for(int i = 0; i <= b; ++i)  f[i][in[u]] += num[i];
      for(int i = head[u]; ~i; i = edges[i].next){
        int v = edges[i].to;
        if(v == fa)  continue;
        ans += dfs(v, u);
      }
      sum[pos[u]] += ans;
      out[u] = idx;
      --num[pos[u]];
      return ans;
    }
    
    void update(int l, int r){
      int val = r - a[l];
      if(val > 0){
        add(in[l], val, 1);
        for(int i = 0; i < b; ++i)  sum[i] += (ULL)f[i][in[l]] * val;
      }
      else if(val < 0){
        val = -val;
        add(in[l], val, 0);
        for(int i = 0; i < b; ++i)  sum[i] -= (ULL)f[i][in[l]] * val;
      }
      a[l] = r;
    }
    
    ULL query(int l, int r){
      int lb = pos[l-1], rb = pos[r-1];
      ULL ans = 0;
      if(lb == rb){
        for(int i = l; i <= r; ++i)
          ans += query(out[i]) - query(in[i]-1);
        return ans;
      }
      for(int i = lb+1; i < rb; ++i)  ans += sum[i];
      for(int i = l; i <= (lb+1)*SIZE; ++i)  ans += query(out[i]) - query(in[i]-1);
      for(int i = rb*SIZE+1; i <= r; ++i)  ans += query(out[i]) - query(in[i]-1);
      return ans;
    }
    
    
    int main(){
      scanf("%d %d", &n, &m);
      ms(head, -1);
      int j = 0;
      for(int i = 1; i <= n; ++i)  scanf("%d", a+i);
      for(int i = 1; i <= n; ++i){
        int u, v;  scanf("%d %d", &u, &v);
        if(u == 0)  root = v;
        else  addEdge(u, v), addEdge(v, u);
        pos[i] = b;
        if(++j == SIZE)  ++b, j = 0;
      }
      dfs(root, -1);
      for(int i = 1; i <= n; ++i)  add(in[i], a[i], 1);
    
      while(m--){
        int l, r, op;  scanf("%d %d %d", &op, &l, &r);
        if(op == 1)   update(l, r);
        else  printf("%llu
    ", query(l, r));
      }
      return 0;
    }
    

      

  • 相关阅读:
    实验二 顺序结构程序设计
    Cracking the Coding Interview(1)
    zigzag数组,螺旋数组
    程序员面试宝典之输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序,即输出125,126,145,146
    static成员函数、成员变量
    快捷键
    多态的总结
    单实例设计模式及C++实现
    快速排序代码
    对象初始化
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7860115.html
Copyright © 2020-2023  润新知