1103 POI2007 大都市meg
树链剖分水过,单点修改,树状数组即可。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define N 250100
using namespace std; int n, m, nowplace = ;
int p[N] = {}, next[N], v[N], bnum = ;
int son[N] = {}, fa[N], w[N], top[N], deep[N] = {}, num[N];
int t[N] = {}; int lowbit(int x) { return x & -x; } void dfs_1(int now, int fat)
{
int k = p[now]; num[now] = ; int maxson = ;
fa[now] = fat; deep[now] = deep[fat] + ;
while (k)
{
dfs_1(v[k], now);
if (num[v[k]] > maxson)
{
maxson = num[v[k]];
son[now] = v[k];
}
num[now] += num[v[k]];
k = next[k];
}
} void dfs_2(int now, int nowtop)
{
int k = p[now]; w[now] = ++nowplace; top[now] = nowtop;
if (son[now]) dfs_2(son[now], nowtop);
while (k)
{
if (v[k] != son[now])
dfs_2(v[k], v[k]);
k = next[k];
}
} void add(int now, int addnum)
{
while (now <= n)
{
t[now] += addnum;
now += lowbit(now);
}
return;
} int task(int now)
{
int ans = ;
while (now)
{
ans += t[now];
now -= lowbit(now);
}
return ans;
} int ask(int u, int v)
{
int f1 = top[u], f2 = top[v];
if (deep[f1] < deep[f2]) { swap(f1, f2); swap(u, v); }
if (f1 == f2) return task(max(w[u], w[v])) - task(min(w[u], w[v])-);
else
{
int ans = ;
ans = task(w[u]) - task(w[f1]-); // 搞清先后
ans += ask(fa[f1], v);
return ans;
}
} int main()
{
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
int x, y; scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;
}
dfs_1(, ); dfs_2(, );
for (int i = ; i <= n; ++i) add(w[i], );
scanf("%d", &m); m = m+n-;
for (int i = ; i <= m; ++i)
{
char s[]; scanf("%s", s);
if (s[] == 'A')
{
int x, y; scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
add(w[y], -);
}
else
{
int x; scanf("%d", &x);
printf("%d\n", ask(, x));
}
}
return ;
}
还有DFS序做法,其实就是求点到根的路径权值和,样例的DFS序 为 1 4 5 5 4 3 3 2 2 1
我们把入序的地方+1 出序的地方-1, 查询的时候求入序的前缀和 - 1 就OK了(因为要减去多出来的1节点)
修改的时候入序和出序都改为0
上代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define N 250010
using namespace std; int n, m, nowplace = ;
int p[N] = {}, next[N], v[N], bnum = ;
int t[N*] = {};
int first[N], second[N]; int lowbit(int x) { return x & -x; } void add(int now, int addnum)
{
while (now <= *n)
{
t[now] += addnum;
now += lowbit(now);
}
} void dfs(int now)
{
int k = p[now]; add(++nowplace, );
first[now] = nowplace;
while (k)
{
dfs(v[k]);
k = next[k];
}
add(++nowplace, -);
second[now] = nowplace;
} int ask(int now)
{
int ans = ;
while (now)
{
ans += t[now];
now -= lowbit(now);
}
return ans;
} int main()
{
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
int x, y; scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;
}
dfs();
scanf("%d", &m); m = m+n-;
for (int i = ; i <= m; ++i)
{
char s[]; scanf("%s", s);
if (s[] == 'A')
{
int x, y; scanf("%d%d", &x, &y);
if (x < y) swap(x, y);
add(first[x], -);
add(second[x], );
}
else
{
int x; scanf("%d", &x);
printf("%d\n", ask(first[x])-);
}
}
return ;
}
1103 POI2007 大都市meg的更多相关文章
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 1103: [POI2007]大都市meg
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2189 Solved: 1160[Submit][Sta ...
- 数据结构(线段树):BZOJ 1103 [POI2007]大都市meg
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1791 Solved: 925[Submit][Stat ...
- BZOJ 1103: [POI2007]大都市meg( 树链剖分 )
早上数学考挂了...欲哭无泪啊下午去写半个小时政治然后就又可以来刷题了.. 树链剖分 , 为什么跑得这么慢... ------------------------------------------- ...
- 【BZOJ】1103: [POI2007]大都市meg
http://www.lydsy.com/JudgeOnline/problem.php?id=1103 题意:一棵n节点的树(1<=n<=250000),m条边(1<=m<= ...
- Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg
这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y 以节点x的权值修改为y. 2.Q x ...
- BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...
- bzoj 1103 : [POI2007]大都市meg (树链剖分+线段树)
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)
本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...
随机推荐
- QStyle
转贴: http://hi.baidu.com/yjj2008/blog/item/6cd4a1892ef0d4b60f2444a5.html 本文介绍了如何使用qt提供的接口来设计自己的GUI风格( ...
- Lua读写文件
文件读写 文件读写对制作游戏很有帮助.可以调用别的文件中的代码,保存最高分.游戏存档.玩家状态等信写到文件中. 首先,让我们看一个简单的命令:dofile.这个命令会读入另一个文件的代码并立即执行. ...
- __attribute__((unused))
在gcc手册中找到了有关的解释: unused:This attribute, attached to a function, means that the function is meant to ...
- 关于解决 Failed to prepare partial IU:
在新版本的Eclipse(Luna)中安装插件经常会碰到Failed to prepare partial IU的错误,一把都是兼容性的问题,要下载个兼容包,步骤如下: 1.打开安装插件的页面:Hel ...
- tachyon 初识
一.简介 Tachyon是一个高容错的分布式文件系统,允许文件以内存的速度在集群框架中进行可靠的共享,就像Spark和MapReduce那样.通过利用信息继承,内存侵入,Tachyon获得了高性能.T ...
- 优化 App 的启动速度
App 的启动速度不仅影响我们调试,也直接关系到用户体验.之前有些很久没有打开过的项目,需要花费很长的时间才完成编译:对应的 App 在点击后,许久才出现启动画面.你是否为这些问题苦恼过呢? 这是我观 ...
- Xcode 8:在 Active Compilation Conditions 中自定义环境变量
来源:没故事的卓同学 链接:http://www.jianshu.com/p/96b36360bb2d 在Xcode 7我们在 OTHER_SWIFT_FLAGS中配置环境变量.但是有一个不爽的地方就 ...
- C# 自定义重绘TabControl
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runti ...
- Play on Words 欧拉通路(回路)判断
Play on Words note: 判断一下连通性. #include <iostream> #include <cstdio> #include <cstring ...
- 【Android 界面效果29】研究一下Android滑屏的功能的原理,及scrollTo和scrollBy两个方法
Android中的滑屏功能的原理是很值得我们去研究的,在知道这两个原理之前,有必要先说说View的两个重要方法,它们就是scrollTo 和scrollBy. Android View视图是没有边界的 ...