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的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. BZOJ 1103: [POI2007]大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2189  Solved: 1160[Submit][Sta ...

  3. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  4. [bzoj1103][POI2007]大都市meg(树状数组+dfs序)

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2031  Solved: 1069[Submit][Sta ...

  5. 【BZOJ】【1103】【POI2007】大都市meg

    dfs序 模板题,进点+1出点-1,刚好对于不在路径上的点一进一出刚好抵消,由于本题要动态修改(变成公路以后+1-1都变成0)所以在序列上套一个树状数组即可. TLE:1.递归dfs给爆了……写了个手 ...

  6. 数据结构(线段树):BZOJ 1103 [POI2007]大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1791  Solved: 925[Submit][Stat ...

  7. BZOJ 1103: [POI2007]大都市meg( 树链剖分 )

    早上数学考挂了...欲哭无泪啊下午去写半个小时政治然后就又可以来刷题了.. 树链剖分 , 为什么跑得这么慢... ------------------------------------------- ...

  8. bzoj1103【POI2007】大都市meg

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1544  Solved: 776 [Submit][St ...

  9. BZOJ1103 [POI2007]大都市meg 【树剖】

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3038  Solved: 1593 [Submit][S ...

随机推荐

  1. Codeforces 1249 F. Maximum Weight Subset

    传送门 设 $f[x][i]$ 表示 $x$ 的子树中,离 $x$ 最近的选择的节点距离为 $i$ 的合法方案的最大价值 设 $val[x]$ 表示节点 $x$ 的价值,首先有 $f[x][0]=va ...

  2. C# 对象互转

    /// <summary> /// 适用于初始化新实体 /// </summary> static public T RotationMapping<T, S>(S ...

  3. 怎样手写实现 new 命令的功能

    实现new命令的功能, 首先需要知道new命令都做了些什么: 第一步: 创建一个空对象, 作为实例对象的容器; 第二步: 将空对象的prototype指向构造函数的prototype; 第三步: 将空 ...

  4. CAS 5.x搭建常见问题系列(3).Failure to find org.apereo.cas:cas-server-support-pm-jdbc:jar:5.1.9

    错误内容 cas overlay的pom.xml增加了cas-server-support-pm-jdbc.jary依赖后, 打包(mvn package)出现如下的报错 D:\casoverlay\ ...

  5. 搭建SSM环境(淘淘商城)

    本文用到的资料: 链接:https://pan.baidu.com/s/1Pk_aI_PRbqRFP9i3o9Xodg 提取码:o4o4 1.1. 数据库 1.1.1. 使用navicat创建数据库连 ...

  6. android 自动化测试案例之 MonkeyScript

    #文件名 MonkeyScript.mks #功能: 使用monkey script测试app,此案例是测试搜索功能(输入关键字,然后点击搜索按钮)#参考: http://blog.csdn.net/ ...

  7. Django 之一些request封装的常用功能

    一些常用的request对象属性 介绍 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其 ...

  8. ImportError: No module named yaml

    问题: import yaml ImportError: No module named yaml 解决: wget http://pyyaml.org/download/pyyaml/PyYAML- ...

  9. 04 Windows编程——Unicode

    VS 2017下源码 #include<stdio.h> int main() { char ASC_a = 'a'; char *ASC_str = "hello"; ...

  10. Linux路径切换命令——directory stack

    操作directory stack一共需要3个命令: dirs .pushd.popd dirs: 显示当前目录栈 pushd: 把目录压栈 popd: 把目录弹栈 dirs 显示目录栈内容,如果没有 ...