[POI] 大都市meg
http://www.lydsy.com/JudgeOnline/problem.php?id=1103
树剖边权转点权,vector存图卡一下午RE
气炸。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector> using namespace std;
const int N = ; #define yxy getchar()
#define lson jd << 1
#define rson jd << 1 | 1 #define RR freopen("gg.in", "r", stdin) int top[N], deep[N], fa[N], size[N], son[N], tree[N];
int now = , n, Ty, Tim, ans;
vector <int> vec[N << ];
int W[N << ];
int head[N];
struct Node {int u, v, nxt;} G[N << ]; void add(int u, int v) {
G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;
} inline int read() {
int x = ; char c = yxy;
while(c < '' || c > '') c = yxy;
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x;
} void Dfs_son(int u, int f_, int dep) {
fa[u] = f_;
deep[u] = dep;
size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != f_) {
Dfs_son(v, u, dep + );
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
}
} void Dfs_un(int u, int tp) {
top[u] = tp;
tree[u] = ++ Tim;
if(!son[u]) return ;
Dfs_un(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa[u] && v != son[u]) Dfs_un(v, v);
}
} void Build_tree(int l, int r, int jd) {
if(l == r) {
if(l != ) W[jd] ++; return ;
}
int mid = (l + r) >> ;
Build_tree(l, mid, lson);
Build_tree(mid + , r, rson);
W[jd] = W[lson] + W[rson];
} void Sec_A(int l, int r, int jd, int x, int y) {
if(x <= l && r <= y) {
ans += W[jd];
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid) Sec_A(mid + , r, rson, x, y);
} inline int Sec_A_imp(int x) {
int tp1 = top[x], ret();
while(tp1 != ) {
ans = ;
Sec_A(, n, , tree[tp1], tree[x]);
ret += ans;
x = fa[tp1];
tp1 = top[x];
}
return ret;
} void Poi_G(int l, int r, int jd, int x) {
if(l == r) {
W[jd] = ; return ;
}
int mid = (l + r) >> ;
if(x <= mid) Poi_G(l, mid, lson, x);
else Poi_G(mid + , r, rson, x);
W[jd] = W[lson] + W[rson];
} int main()
{
n = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {
int u = read(), v = read();
if(u > v) swap(u, v);
add(u, v);
}
Dfs_son(, , );
Dfs_un(, );
Build_tree(, n, );
Ty = read(); Ty += (n - );
while(Ty --) {
char c[]; scanf("%s", c);
if(c[] == 'W') {
int x = read();
printf("%d\n", Sec_A_imp(x));
} else {
int x = read(), y = read();
if(x < y) swap(x, y);
Poi_G(, n, , tree[x]);
}
}
return ;
}
[POI] 大都市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】大都市meg 树状数组 + DFS序
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2009 Solved: 1056[Submit][Sta ...
- [bzoj1103][POI2007]大都市meg(树状数组+dfs序)
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2031 Solved: 1069[Submit][Sta ...
- 【BZOJ】【1103】【POI2007】大都市meg
dfs序 模板题,进点+1出点-1,刚好对于不在路径上的点一进一出刚好抵消,由于本题要动态修改(变成公路以后+1-1都变成0)所以在序列上套一个树状数组即可. TLE:1.递归dfs给爆了……写了个手 ...
- 数据结构(线段树):BZOJ 1103 [POI2007]大都市meg
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1791 Solved: 925[Submit][Stat ...
- BZOJ 1103: [POI2007]大都市meg( 树链剖分 )
早上数学考挂了...欲哭无泪啊下午去写半个小时政治然后就又可以来刷题了.. 树链剖分 , 为什么跑得这么慢... ------------------------------------------- ...
- bzoj1103【POI2007】大都市meg
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1544 Solved: 776 [Submit][St ...
- BZOJ1103 [POI2007]大都市meg 【树剖】
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3038 Solved: 1593 [Submit][S ...
随机推荐
- java实现带过期时间的缓存
private static ScheduledExecutorService swapExpiredPool = new ScheduledThreadPoolExecutor(10); priva ...
- RMAN执行crosscheck archive报错ORA-19633问题处理
一.问题现象 RMAN connect target /; run { crosscheck archivelog all; } ORA-: control file record is out of ...
- sql注入测试(1)---概念概述
在进行java web 测试时,经常会忽略的测试种类就是sql注入测试,这类缺陷造成的原因是开发技术在这方面欠缺的表现,虽然不常见,但一旦有这类缺陷,就很因此对运营的数据造成很多不必要的损失,所以,还 ...
- 数据库与MySQL进阶(4)
1,事务 事务指的是满足 ACID 特性的一组操作,可以通过 Commit 提交一个事务,也可以使用 Rollback 进行回滚. 1.1 ACID四大特性 原子性(Atomicity) 事务被视为不 ...
- .net Core CLR
.net Core CLR是开源的.大部分文件是C++写成.这样他就可以编译后再不同的平台运行. https://github.com/dotnet/coreclr
- .NET Core api部署到IIS上405的问题
今天部署到iis 服务器上,api的put请求一直报405.其他像get post都没问题. google了半天,找到两种解决方案,亲测都可以.但我个人认为不是最理想的解决方案. 1.IIS拒绝PUT ...
- XML-RPC-3XML-RPC 与 XML-RPC 服务器类
http://codeigniter.org.cn/user_guide/libraries/xmlrpc.html XML-RPC 与 XML-RPC 服务器类 CodeIgniter 的 XML- ...
- 动手实现CNN卷积神经网络
数据集采用的是手写数据集(http://yann.lecun.com/exdb/mnist/): 本文构建的CNN网络图如下: 像素点:28*28 = 784,55000张手写数字图片. # -*- ...
- Python模拟登陆某网教师教育网
本文转载自看雪论坛[作者]rdsnow 不得不说,最近的 Python 蛮火的,我也稍稍了解了下,并试着用 Python 爬取网站上的数据 不过有些数据是要登陆后才能获取的,我们每年都要到某教师教育网 ...
- cocos发布遇到的问题
学习第二天,用官方的demo进行打包,出现以下问题: 第一个问题: 报错信息:scene 没有保存,请先保存相关信息再进行构建. 解决方案:ctrl+s保存即可,一开始没注意前面的英文是场景的意思 第 ...