Codeforces Gym 100589A Queries on the Tree 树状数组 + 分块
来源:互联网 发布:中国平安证券软件 编辑:程序博客网 时间:2024/05/21 10:22
题目大意:
就是现在对于一棵有向树, 以1为根, 初始的时候每个节点上的硬币数量都是0, 树的结点个数为N <= 100000, 接下来是M <= 10000次操作, 每次操作要么是将所有深度为L的结点上的硬币数量增加一个值, 要么是询问以x为根的子树上的所有节点的硬币数量之和
大致思路:
首先如果对于每次更新操作用树状数组维护暴力执行的话, 对于同一深度的结点数量很多的时候这个复杂度会达到O(N*M*logN)显然是不能接受的, 那么考虑一下分块的思想
首先用时间戳的思想将这棵树映射到一个区间上然后对于每次修改, 当要修改的点数小于sqrt(N)的时候用树状数组维护暴力执行, 否则只是记录这个深度增加了多少, 在每次询问的时候查询树状数组的和, 然后对于查询的子树所在的区间, logN求出各个没有修改的深度对应的点的数量, 那么整体的复杂度是O(sqrt*N)*M*logN)
代码如下:
Result : Accepted Memory : 14488 KB Time : 78 ms
/* * Author: Gatevin * Created Time: 2015/8/8 19:45:44 * File Name: Sakura_Chiyo.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define maxn 100010#define time timvector<int> G[maxn];vector<int> pos[maxn];vector<int> large;//存储那些深度拥有的点的个数很多int dfn[maxn];int L[maxn], R[maxn];int time;int n, m;int Limit = 1000;lint s[maxn];lint C[maxn];void dfs(int now, int deep){ int nex; L[now] = ++time; pos[deep].push_back(L[now]); for(int i = 0, sz = G[now].size(); i < sz; i++) { nex = G[now][i]; dfn[nex] = dfn[now] + 1; dfs(nex, deep + 1); } R[now] = time; return;}int lowbit(int x){ return x & -x;}void add(int x, int d){ while(x <= n) C[x] += d, x += lowbit(x); return;}lint sum(int x){ lint ret = 0; while(x > 0) ret += C[x], x -= lowbit(x); return ret;}int main(){ while(~scanf("%d %d", &n, &m)) { memset(C, 0, sizeof(C)); memset(s, 0, sizeof(s)); for(int i = 0; i <= n; i++) pos[i].clear(); large.clear(); for(int i = 1; i <= n; i++) G[i].clear(); int u, v; for(int i = 1; i < n; i++) { scanf("%d %d", &u, &v); G[u].push_back(v); } memset(dfn, 0, sizeof(dfn)); time = 0; dfs(1, 0); for(int i = 0; i <= n; i++) if(pos[i].size() > Limit) large.push_back(i); while(m--) { int op; scanf("%d", &op); if(op == 1) { int l, y; scanf("%d %d", &l, &y); if(pos[l].size() <= Limit) for(int i = 0, sz = pos[l].size(); i < sz; i++) add(pos[l][i], y); else s[l] += y; } else { int x; scanf("%d", &x); int l = L[x], r = R[x];//询问区间[l, r]的和以及要找出所有没有更新的深度的和 lint ans = sum(r) - sum(l - 1); for(int i = 0, sz = large.size(); i < sz; i++) ans += (upper_bound(pos[large[i]].begin(), pos[large[i]].end(), r) - lower_bound(pos[large[i]].begin(), pos[large[i]].end(), l))*s[large[i]]; printf("%I64d\n", ans); } } } return 0;}
0 0
- Codeforces Gym 100589A Queries on the Tree 树状数组 + 分块
- Codeforces Gym 100589A Queries on the Tree(树状数组+分块)
- Codeforces 396C On Changing Tree(树状数组)
- CodeForces - 396C On Changing Tree(树状数组)
- codeforces 375D. Tree and Queries (莫队+分块)
- BZOJ 3637: Query on a tree VI (树链剖分+树状数组)
- Codeforces 785E 分块+树状数组
- UVA 12424 Answering Queries on a Tree
- Codeforces gym 100739 A. Queries(线段树+拆位)
- Hdu1556 Color the ball [分块][树状数组]
- [Codeforces375D]Tree and Queries(dsu on the tree+bit)
- [codeforces375D]Tree and Queries(dsu on the tree+bit)
- CodeForces - 369E Valera and Queries 离线+树状数组
- Codeforces 369E Valera and Queries --树状数组+离线操作
- CodeForces - 369E Valera and Queries(树状数组)
- hdu 4836 The Query on the Tree(线段树or树状数组)
- CF GYM 100741 A Queries
- Codeforces Round 232 Div 1 C On Changing Tree 树状数组 或 线段树
- log4j日志输出性能优化-缓存、异步
- 常用正则表达式
- UIScrollerView约束注意点
- NoSQL简介
- 线程开销
- Codeforces Gym 100589A Queries on the Tree 树状数组 + 分块
- 逻辑代数与位运算
- Max Sum
- $.parseJSON()方法
- 在 Web 应用中增加用户跟踪功能
- HDU 1503(Advanced Fruits)最长公共子序列+路径记录
- UNIX网络编程卷一:第八章 I/O UDP
- 图的三种存储方式
- TCP/IP,http,socket,长连接,短连接