• Tree Requests CodeForces


    大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文.

    直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m)log(n+m))$

    看了别人题解后发现有简单做法, 高度相同的点在每个子树内的dfs序一定相邻, 直接维护每一层的异或和, 每次二分出该层属于$x$的子树的一段区间即可.

    放一下线段树暴力的代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    #define hr putchar(10)
    #define pb push_back
    #define lc tr[o].l
    #define rc tr[o].r
    #define mid ((l+r)>>1)
    #define ls lc,l,mid
    #define rs rc,mid+1,r
    #define x first
    #define y second
    #define io std::ios::sync_with_stdio(false)
    #define endl '
    '
    #define DB(a) {REP(i,1,n) cout<<a[i]<<' ';hr;}
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, INF = 0x3f3f3f3f;
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
    ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
    //head
    
    
    
    
    
    const int N = 5e5+10;
    int n, m, t, tot;
    vector<int> g[N];
    char s[N];
    int L[N], R[N], T[N];
    struct {int l,r,v;} tr[N<<5];
    
    void update(int &o, int l, int r, int x, int v) {
    	if (!o) o=++tot;
    	tr[o].v ^= v;
    	if (l==r) return;
    	if (mid>=x) update(ls,x,v);
    	else update(rs,x,v);
    }
    void dfs(int x, int d) {
    	L[x]=++*L,update(T[d],1,n,L[x],1<<s[x]-'a');
    	for (int y:g[x]) dfs(y,d+1);
    	R[x]=*L;
    }
    void query(int o, int l, int r, int ql, int qr) {
    	if (!o) return;
    	if (ql<=l&&r<=qr) return t ^= tr[o].v, void();
    	if (mid>=ql) query(ls,ql,qr);
    	if (mid<qr) query(rs,ql,qr);
    }
    int main() {
    	scanf("%d%d", &n, &m);
    	REP(i,2,n) scanf("%d", &t),g[t].pb(i);
    	scanf("%s", s+1);
    	dfs(1,1);
    	REP(i,1,m) {
    		int x, h;
    		scanf("%d%d", &x, &h);
    		t = 0, query(T[h],1,n,L[x],R[x]);
    		puts(t^t&-t?"No":"Yes");
    	}
    }
    
  • 相关阅读:
    【USACO2017JAN】 Promotion Counting
    【POJ 3468】 A Simple Problem with Integers
    【HDU 1754】 I Hate It
    【Codeforces 20C】 Dijkstra?
    【POJ 2407】 Relatives
    BZOJ5249 九省联考2018IIIDX(线段树+贪心)
    BZOJ5251 八省联考2018劈配(网络流)
    BZOJ4200 NOI2015小园丁与老司机(动态规划+上下界网络流)
    BZOJ3876 AHOI/JSOI2014支线剧情(上下界网络流)
    LOJ117 有源汇有上下界最小流(上下界网络流)
  • 原文地址:https://www.cnblogs.com/uid001/p/10629138.html
Copyright © 2020-2023  润新知