[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 ...
随机推荐
- redis用法分析
redis基本介绍 redis也是一个内存非关系型数据库,它拥有memcache在数据存储上的全部优点,而且在memcache的基础上增加了数据持久性功能,redis用rdb和aof两种方式实现数据持 ...
- package.json 版本解释
指定版本:比如1.2.2,遵循“大版本.次要版本.小版本”的格式规定,安装时只安装指定版本.波浪号(tilde)+ 指定版本:比如~1.2.2,表示安装1.2.x的最新版本(不低于1.2.2),但是不 ...
- 解决https 请求过程中SSL问题
最近一个项目中用到了https的请求,在实际调用过程中发现之前的http方法不支持https,调用一直报错. 查询了一下,添加几行代码解决问题. public string HttpPost(stri ...
- C# XML 序列化帮助类
/// <summary> /// Xml helper class /// </summary> public static class XmlHelper { #regio ...
- 分布式事务(ACID特性、CAP定律)
普通事务和分布式事务的区别: 普通事务就是一般所说的数据库事务,事务是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成.当事务被提交给了DBMS(数据库管理系统),则DBMS(数 ...
- vue-cli || webpack 打包的时候css里面写的背景图片的路径出错问题
.bg width 100% position fixed left 0 top 0 height 100vh z-index -1 background url('~@/assets/imgs/bg ...
- linux环境下composer的安装与使用
一.下载 Composer(安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号.) 1.下载安装脚本 - composer-setup.php - 到当 ...
- CentOS7安装CDH 第四章:CDH的版本选择和安装方式
相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...
- pytorch训练模型的一些坑
1. 图像读取 opencv的python和c++读取的图像结果不一致,是因为python和c++采用的opencv版本不一样,从而使用的解码库不同,导致读取的结果不同. 详细内容参考:https:/ ...
- DNS服务和BIND
今天我们来介绍一下DNS服务,在大家的印象中DNS可能只是将域名解析为IP地址,可能其他的暂时还不太了解,希望本篇内容能帮助大家. 1.什么是DNS? DNS( Domain Name System) ...