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: ...
随机推荐
- LeetCode37 Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 如何简单便捷的搭建一个网站 - 基于Django
一.所需工具以及相关环境 1. 系统:win7,win8.1,win10(亲测可用 - 本文为win7,64位) 2. 本文使用的版本是: 1)python-2.7.11[百度云盘分享:http:// ...
- Python笔记(一)
我是一名211高校软件工程大三学生,由于前段时间一直在找实习公司.笔试面试了很多公司,虽然有一定的基础,但是还是被某些公司面试官像虐狗一样的虐了.最后找到了一个口碑比较好的外企,主攻信息安全方面.这段 ...
- Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2899 Accepted: 133 ...
- 02.Java多线程并发库API使用
1. 传统线程技术回顾 继承线程与实现Runnable的差异?为什么那么多人都采取第二种方式? 因为第二种方式更符合面向对象的思维方式.创建一个线程,线程要运行代码,而运行的代码都封装到 ...
- iOS -动态可变参数
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- [HTML] Google IE-x.js 解决IEx与W3C标准的冲突
如果分别用IE5.IE6.IE7浏览同一个网页,将可能出现不一样的效果.这是它们之间对CSS的解析选择器不一样或错误和个别bug所导致.为了解决这些错误和bug.我们不得不找到一个能平衡于它们之间的解 ...
- 如何利用OCS存取PHP session全局变量
如何利用OCS存取PHP session全局变量 阿里云技术团队:余汶龙 一.场景介绍 用户在利用PHP搭建网站时,会把一些信息存放在$_SESSION全局变量里,可以很方便的存取.在PHP的in ...
- centos下查看rpm包安装位置
1.如何安装rpm软件包 rpm -ivh your-package.rpm其中your-package.rpm是你要安装的rpm包的文件名,一般置于当前目录下. 2.如何卸载rpm软件包使用命令 r ...
- (三)u-boot2013.01.01 for TQ210:《mkconfig分析》
/* 和分析makefile一样,分析mkconfig同样注重句法分析 */ ############################################################# ...