zoj3686(重新编号的线段树)

本文介绍了一种使用深度优先搜索(DFS)对树状结构进行重新编号的方法,并结合线段树实现区间更新和查询操作。这种方法适用于解决涉及树形结构中子树权值翻转及计数的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 题意:一颗n个节点的树,每个点都有一个权值(0或者1),初始化为0,两个操作:1、取反以i为节点的子树的所有点的权值;2、求以i点为根的子树中1的个数。

 思路:dfs将所有节点重新编号,然后就是普通的成段更新。

代码如下:


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdio.h>
#include<math.h>
#include<vector>
#define N 100005
#define inf 0x7ffffff
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
vector<int> G[N];
int hash[N],num[N];
int id;
void dfs(int cur)
{
    num[cur] = 1;
    hash[cur] = id++;
    int len = G[cur].size();
    for(int i = 0; i < len; i++)
    {
        dfs(G[cur][i]);
        num[cur] += num[ G[cur][i] ];
    }
}
struct node
{
    int l,r,flag,sum;
}tree[4*N];
void build(int o,int l,int r)
{
    tree[o].l = l;
    tree[o].r = r;
    tree[o].flag = tree[o].sum = 0;
    if(l == r)  return;
    int m = (l+r)/2;
    build(2*o,l,m);
    build(2*o+1,m+1,r);
}
void pushup(int o)
{
    tree[o].sum = tree[2*o].sum + tree[2*o+1].sum;
}
void pushdown(int o)
{
    if(tree[o].flag)
    {
        tree[2*o].flag ^= 1;
        tree[2*o].sum = tree[2*o].r - tree[2*o].l + 1 - tree[2*o].sum;

        tree[2*o+1].flag ^= 1;
        tree[2*o+1].sum = tree[2*o+1].r - tree[2*o+1].l + 1 - tree[2*o+1].sum;
        tree[o].flag = 0;
    }
}
void update(int o,int x,int y)
{
    if(x <= tree[o].l && tree[o].r <= y)
    {
        tree[o].sum = tree[o].r - tree[o].l + 1 - tree[o].sum;
        tree[o].flag ^= 1;
        return;
    }
    pushdown(o);
    int m = (tree[o].l+tree[o].r)/2;
    if(x <= m)  update(2*o,x,y);
    if(y > m)   update(2*o+1,x,y);
    pushup(o);
}
int query(int o,int x,int y)
{
    if(x <= tree[o].l && tree[o].r <= y)
         return tree[o].sum;
    pushdown(o);
    int m = (tree[o].l+tree[o].r)/2;
    int ans = 0;
    if(x <= m)  ans += query(2*o,x,y);
    if(y > m)   ans += query(2*o+1,x,y);
   // pushup(o);
    return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF)
    {
         int i;
         for(i = 1; i <= n; i++)    G[i].clear();
         for(i = 2; i <= n; i++)
         {
             int x;
             scanf("%d",&x);
             G[x].push_back(i);//xµÄ¶ù×Ó½Úµã
         }
         id = 1;
         dfs(1);
         build(1,1,n);
         while(m--)
         {
             char s[10];
             int x;
             scanf("%s%d",s,&x);
             if(s[0] == 'o')
                update(1,hash[x],hash[x]+num[x]-1);
             else
                printf("%d\n",query(1,hash[x],hash[x]+num[x]-1));
         }
         printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值