树链剖分水过,单点修改,树状数组即可。

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

  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 [POI2007]大都市meg

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

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

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

  5. 【BZOJ】1103: [POI2007]大都市meg

    http://www.lydsy.com/JudgeOnline/problem.php?id=1103 题意:一棵n节点的树(1<=n<=250000),m条边(1<=m<= ...

  6. 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 ...

  7. BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...

  8. bzoj 1103 : [POI2007]大都市meg (树链剖分+线段树)

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

  9. BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)

    本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...

随机推荐

  1. LINUX内核笔记

    http://blog.chinaunix.net/uid/27119491/list/1.html?cid=161103

  2. 通过Javascript模拟登陆Windows认证的网站

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>模拟登陆</title ...

  3. Transact-SQL三值逻辑

    /*===========================<一>========================== 在SQL中逻辑表达式的值有三种: 1.TRUE 2.FALSE 3.U ...

  4. js和jquery的DOM事件大全

  5. 使用C#实现读取/写入Excel表

    C#实现写入Excel表 using System; using System.Reflection; using System.IO; using Microsoft.Office.Interop. ...

  6. G - Not so Mobile

    G - Not so Mobile Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu     Des ...

  7. cigarettes

    描述 Tom has many cigarettes. We hypothesized that he has n cigarettes and smokes them one by one keep ...

  8. SpringMvc多文件上传简单实现

    public ResponseItem uploadFile(MultipartHttpServletRequest request,FileItem fileItem,PageData pd) { ...

  9. SolrEntityProcessor

    SolrEntityProcessor从不同的solr实例和内核中引入数据,这个数据是基于指定的或者是过滤的查询来获取到的.如果你需要复制索引,并且小幅度的修改目标索引文件中的数据,那么可以使用Sol ...

  10. 3. Android框架和工具之 xUtils(DbUtils )

    1. xUtils简介 xUtils 包含了很多实用的android工具.xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓 ...