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: ...
随机推荐
- 【转】winform退出代码:Application.Exit和Environment.Exit(0)
Application.Exit和Environment.Exit(0)有什么退出方面的区别吗? Application.Exit:通知winform消息循环退出.会在所有前台线程退出后,退出应用 强 ...
- 5个基于css3超炫的鼠标滑动按钮动画
今天给大家分享5个基于css3超炫的鼠标滑动按钮动画.这5个按钮鼠标经过的时候有超炫的动画效果.这5个按钮适用浏览器:360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之 ...
- C++字符串处理封装类String
概述 C++在处理字符串时相对于python等脚本语言并没有什么优势,下面将常用的字符串处理函数封装成一个String工具类,方便以后使用,后期还会对该类进行扩充,下面是具体的实现: // Strin ...
- LeetCode31 Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- 使用java8
刚开始一直使用的jdk7,但是学习spark的时候spark推荐Jdk8,很多示例程序都用到了lambda 机器上安装jdk8只是实验用途,默认还想使用jdk7 安装完后 网上说这是因为jdk8安装的 ...
- 开发工具 之 PowerDesigner 应用积累
1.在默认情况下,code与name是联动,修改了name中的数据. 解决方法:设置菜单栏选择"Tools→General Options→Dialog" 中的 "Na ...
- Wcf 之 配置文件解析
在WCF Service Configuration Editor的使用中,我们通过配置工具自动生成了WCF服务端的config文件.现在我们来看下这个配置文件各个标签的意义(解释在下面xml文件中的 ...
- Sum of divisors
Problem Description mmm is learning division, she's so proud of herself that she can figure out the ...
- ios----protocol, optional ,delegate
ios----protocol,delegate protocol——协议 协议是用来定义对象的属性,行为和用于回调的. 协议中有两个关键字@private和@optional,@privat ...
- [SQLServer]学习总结笔记(基本涵盖Sql的所有操作)
--################################################################################### /* 缩写: DDL(Dat ...