题解 P3038 【[USACO11DEC]牧草种植Grass Planting】

· · 题解

既然各位dalao都用的线段树+树剖或者LCA+树状数组,那为什么不用一下树状数组+树剖呢~?(滑稽

目前开O2后是本题的RANK2

Accepted 100 用时: 172ms / 内存: 19480KB

前置知识:树链剖分P3384树状数组2(区间添加单点查询,差分)P3368

大致思路:

1.读入,连边,树剖。

2.对于'P'操作,我们只需在这个路径上的除了公共祖先以外的所有点添加1即可。

可以在草稿纸上随便画上一颗树,每次在边上添加可以转化为添加某条边的底端(即深度大的那个点),显而易见,这样做,一个点是必定对应一条边的。

但LCA那个点不属于我们要查的路径,所以当两个点跳到一条链上时,设x的深度小于y的深度,id[x]为x在树状数组上的编号,那么id[x](即LCA)不在这条路径上,添加时只添加(id[x]+1,id[y])即可~!

3.对于'Q'操作,因为题目说在查询的时候只需要查“一条边”,而不是“一段路径”。所以在树状数组上单点查寻两端点深的那个端点即可(深的那个代表了一条边,如思路2所述)。

CODE(附有简单注释):

#pragma warning (disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define RG register
using namespace std;
const int maxn = 1e5 + 5, maxm = maxn << 1;
int head[maxm], nxt[maxm], v[maxm]/*前向星*/, dad[maxn]/*某点父亲*/, son[maxn]/*重儿子*/,
sz[maxn]/*子树大小*/, depth[maxn]/*深度*/, id[maxn]/*某点在树状数组上的对应编号*/, top[maxn]/*重链头*/;

int n, m, cnt/*前向星用*/, root/*树根*/, num/*树剖用dfs序*/;

struct Binary_Indexed_Tree//结构体封装树状数组!
{
    int c[maxn];
    inline int lowbit(int k) { return k & (-k); }
    inline void add(int x, int k) { for (int i = x; i <= n; i += lowbit(i)) c[i] += k; }
    inline int sum(int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += c[i]; return ans; }
    inline void update(int l, int r, int k) { add(l, k), add(r + 1, -k); }//区间加法
}BIT;
//以下是前向星,快读写和树剖
inline void addline(int x, int y) { v[cnt] = y, nxt[cnt] = head[x], head[x] = cnt++; }

inline int read()
{
    RG char c = getchar(); RG int x = 0;
    while (c<'0' || c>'9')  c = getchar();
    while (c >= '0'&&c <= '9')  x = (x << 3) + (x << 1) + c - '0', c = getchar();
    return x;
}

inline void write(int x)
{
    if (x > 9)  write(x / 10);
    putchar(x % 10 + '0'); return;
}

inline void dfs1(int x, int f, int d)
{
    dad[x] = f, depth[x] = d, sz[x] = 1;
    for (RG int i = head[x]; i != -1; i = nxt[i])
    {
        if (v[i] == f)  continue;
        dfs1(v[i], x, d + 1);
        sz[x] += sz[v[i]];
        if (sz[v[i]] > sz[son[x]])  son[x] = v[i];
    }
    return;
}

inline void dfs2(int x, int t)
{
    id[x] = ++num, top[x] = t;
    if (!son[x])    return;
    dfs2(son[x], t);
    for (RG int i = head[x]; i != -1; i = nxt[i])
        if (v[i] != dad[x] && v[i] != son[x])   dfs2(v[i], v[i]);
    return;
}
//添加操作!
inline void add(int x, int y, int k)
{
    RG int tx = top[x], ty = top[y];
    while (tx != ty)
    {
        if (depth[tx] >= depth[ty]) BIT.update(id[tx], id[x], k), x = dad[tx], tx = top[x];
        else BIT.update(id[ty], id[y], k), y = dad[ty], ty = top[y];
    }
    if (x == y) return;//如果x==y不退出,线段树里会出现查询区间[l,r]出现l>r的情况
    if (id[x] <= id[y]) BIT.update(id[x] + 1, id[y], k);//记得+1避免LCA那个点
    else BIT.update(id[y] + 1, id[x], k);
    return;
}

int main()
{
    memset(head, -1, sizeof(head));
    n = read(), m = read();
    for (RG int i = 1; i < n; i++) { int x = read(), y = read(); addline(x, y), addline(y, x); }
    srand(19260817), root = rand() % n + 1, dfs1(root, 0, 1), dfs2(root, root);//随机选根!(用了一个很暴力的树快了4ms ???
    while (m--)
    {
        RG char c = getchar(); RG int x, y;
        while (c != 'P' && c != 'Q')    c = getchar();  x = read(), y = read();
        if (c == 'P')   add(x, y, 1);//直接调用
        if (c == 'Q')   depth[x] > depth[y] ? write(BIT.sum(id[x])) : write(BIT.sum(id[y])), putchar('\n');
        //看不懂的也可以翻译成:
        //if(depth[x]>depth[y]) write(BIT.sum(id[x]));
        //else  write(BIT.sum(id[y]));
        //putchar('\n');
        //即选取深度大的那个点进行查询
    }
    return 0;
}