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\n", query(l, r));
}
return 0;
}

  

BZOJ 4765 普通计算姬 (分块 + BIT)的更多相关文章

  1. BZOJ 4765: 普通计算姬 [分块 树状数组 DFS序]

    传送门 题意: 一棵树,支持单点修改和询问以$[l,r]$为根的子树的权值和的和 只有我这种不会分块的沙茶不会做这道题吗? 说一点总结: 子树和当然上$dfs$序了,询问原序列一段区间所有子树和,对原 ...

  2. BZOJ 4765: 普通计算姬 (分块+树状数组)

    传送门 解题思路 树上的分块题,,对于修改操作,每次修改只会对他父亲到根这条链上的元素有影响:对于查询操作,每次查询[l,r]内所有元素的子树,所以就考虑dfn序,进标记一次,出标记一次,然后子树就是 ...

  3. bzoj 4765 普通计算姬 dfs序 + 分块

    题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...

  4. bzoj 4765: 普通计算姬

    Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能计算数列区间和,而普通计算姬能计算树中 ...

  5. bzoj 4765: 普通计算姬 主席树+替罪羊树思想

    题目大意: 给定一棵\(n\)个节点的带权树有根树,设\(sum_p\)表示以点\(p\)为根的这棵子树中所有节点的权 计算姬支持下列两种操作: 给定两个整数\(u,v\),修改点\(u\)的权值为\ ...

  6. bzoj 4765 普通计算姬(树状数组 + 分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=4765 很nice的一道题啊(可能是因为卡了n久终于做出来了 题意就是给你一棵带点权的有根树,sum( ...

  7. BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!

    真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...

  8. [BZOJ4765]普通计算姬(分块+树状数组)

    4765: 普通计算姬 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1725  Solved: 376[Submit][Status][Discus ...

  9. bzoj 4766: 文艺计算姬 -- 快速乘

    4766: 文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MB Description "奋战三星期,造台计算机".小W响应号召,花了三星期 ...

随机推荐

  1. 将unitest整合和python发送测试报告

    废话少说先上代码 # -*- coding:UTF-8 -*- __autor__ = 'zhouli' __date__ = '2018/11/12 21:29' import unittest i ...

  2. python中类变量和成员变量、局部变量总结

    class Member(): num= #类变量,可以直接用类调用,或用实例对象调用 def __init__(self,x,y): self.x=x #实例变量(成员变量),需要它是在类的构造函数 ...

  3. Django配置后台xadmin管理界面

    Django配置后台xadmin管理界面 python版本3.6.5 Django版本1.10.8(刚开始是2.1.5,由于各种错误,改成了低版本) 1.xadmin的安装,下载地址https://g ...

  4. dev-server.js浅析

    // 检查NodeJS和npm的版本 require('./check-versions')() // 获取配置 var config = require('../config') // 如果Node ...

  5. 百度地图插件(百度地图AK申请配置指南)

    百度地图AK申请配置指南     [LBS云] 百度地图AK申请配置指南 1. 该文档是详细版,图文并茂: 2. 该指南是针对browser-mobile-sever三种终端开发的申请与配置说明: 3 ...

  6. 函数 day9

    一,什么是函数? 函数的定义与调用 s = 'fkdsagadfdsagfdsagg' count = 0 for i in s: count += 1 print(count) l1 = [1,2, ...

  7. (转)Oracle 使用 DBLINK详解

    DBLINK详解 1.创建dblink语法: CREATE [PUBLIC] DATABASE LINK link CONNECT TO username IDENTIFIED BY password ...

  8. 转 java反射详解

    本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的,要不然只看理论的话,看了也不懂,不过建议大家在看完文章之后,在回过头去看看理论,会有更好的理解. 下面开始正文. [案例1]通过一个对象 ...

  9. Increase PHP script execution time with Nginx

    If you have a large WordPress setup or a server with limited resources, then you will often see the ...

  10. SPring中quartz的配置(可以用实现邮件定时发送,任务定时执行,网站定时更新等)

    http://www.cnblogs.com/kay/archive/2007/11/02/947372.html 邮件或任务多次发送或执行的问题: 1.<property name=" ...