• 【洛谷 P5305】【GXOI/GZOI2019】—旧词(树链剖分)


    传送门

    用类似LcaLca那道题的做法做
    但这里是depkdep^k
    把一个点系数设为depk(dep1)kdep^k-(dep-1)^k即可

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
        static char ibuf[RLEN],*ib,*ob;
        (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
        return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=ch=='-',ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    #define ll long long
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    #define bg begin
    #define poly vector<int>  
    cs int mod=998244353;
    inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
    inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
    inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
    inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
    inline int mul(int a,int b){return 1ll*a*b%mod;}
    inline void Mul(int &a,int b){a=1ll*a*b%mod;}
    inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
    inline int Inv(int x){return ksm(x,mod-2);}
    inline void chemx(int &a,int b){a<b?a=b:0;}
    inline void chemn(int &a,int b){a>b?a=b:0;}
    cs int N=50005;
    vector<int> e[N];
    int dfn,in[N],out[N],siz[N],son[N],fa[N],dep[N],top[N],idx[N];
    int n,q,t,ans[N];
    struct ask{
    	int y,id;
    	ask(int _y=0,int _d=0):y(_y),id(_d){}	
    };
    vector<ask> p[N];
    namespace Seg{
    	int tag[N<<2],s[N<<2],coef[N<<2];
    	#define lc (u<<1)
    	#define rc ((u<<1)|1)
    	#define mid ((l+r)>>1)
    	inline void build(int u,int l,int r){
    		if(l==r){coef[u]=dec(ksm(dep[idx[l]],t),ksm(dep[idx[l]]-1,t));return;}
    		build(lc,l,mid),build(rc,mid+1,r);
    		coef[u]=add(coef[lc],coef[rc]);
    	}
    	inline void pushup(int u){
    		s[u]=add(s[lc],s[rc]);
    	}
    	inline void pushnow(int u,int k){
    		Add(tag[u],k),Add(s[u],mul(coef[u],k));
    	}
    	inline void pushdown(int u){
    		if(!tag[u])return ;
    		pushnow(lc,tag[u]);
    		pushnow(rc,tag[u]);
    		tag[u]=0;
    	}
    	void update(int u,int l,int r,int st,int des){
    		if(st<=l&&r<=des)return pushnow(u,1);
    		pushdown(u);
    		if(st<=mid)update(lc,l,mid,st,des);
    		if(mid<des)update(rc,mid+1,r,st,des);
    		pushup(u);
    	}
    	int query(int u,int l,int r,int st,int des){
    		if(st<=l&&r<=des)return s[u];
    		int res=0;pushdown(u);
    		if(st<=mid)Add(res,query(lc,l,mid,st,des));
    		if(mid<des)Add(res,query(rc,mid+1,r,st,des));
    		pushup(u);return res;
    	}
    }
    namespace SLPF{
    	void update(int u){
    		while(top[u]){
    			Seg::update(1,1,n,in[top[u]],in[u]);
    			u=fa[top[u]];
    		}
    	}
    	int query(int u){
    		int res=0;
    		while(top[u]){
    			Add(res,Seg::query(1,1,n,in[top[u]],in[u]));
    			u=fa[top[u]];
    		}
    		return res;
    	}
    }
    void dfs1(int u){
    	siz[u]=1;
    	for(int &v:e[u]){
    		dep[v]=dep[u]+1;
    		dfs1(v),siz[u]+=siz[v];
    		if(siz[v]>siz[son[u]])son[u]=v;
    	}
    }
    void dfs2(int u,int tp){
    	top[u]=tp,in[u]=++dfn,idx[dfn]=u;
    	if(son[u])dfs2(son[u],tp);
    	for(int &v:e[u]){
    		if(v==son[u])continue;
    		dfs2(v,v);
    	}
    	out[u]=dfn;
    }
    int main(){
    	n=read(),q=read(),t=read();
    	for(int i=2;i<=n;i++)fa[i]=read(),e[fa[i]].pb(i);
    	dep[1]=1,dfs1(1),dfs2(1,1);
    	Seg::build(1,1,n);
    	for(int i=1;i<=q;i++){
    		int x=read(),y=read();
    		p[x].pb(ask(y,i));
    	}
    	for(int i=1;i<=n;i++){
    		SLPF::update(i);
    		for(ask &x:p[i])
    		ans[x.id]=SLPF::query(x.y);
    	}
    	for(int i=1;i<=q;i++)cout<<ans[i]<<'
    ';
    }
    
  • 相关阅读:
    [POJ]poj2632(模拟)
    [EOJ]2019 ECNU XCPC March Selection #2
    [POJ]POJ1328(trie)
    卡特兰数相关总结
    2019海亮夏令营随笔
    树上数数 题解
    护卫小队 题解
    洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题解
    洛谷 P4735 最大异或和
    登峰造极 题解
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328495.html
Copyright © 2020-2023  润新知