zoukankan      html  css  js  c++  java
  • 绵阳东辰国际test10.12

    蒟蒻分析:考试时写个权值线段树就是调不出来

    蒟蒻总结:平时多练手

    solution:其实就用树状数组维护一下就行

    多了个撤销操作而已,应该十分钟就写完的,我写了三个小时!!!

    code by jklover

    #include<bits/stdc++.h>
    using namespace std;
    inline int read()
    {
    	int out=0,sgn=1;
    	char jp=getchar();
    	while(jp!='-' && (jp<'0' || jp>'9'))
    		jp=getchar();
    	if(jp=='-')
    		sgn=-1,jp=getchar();
    	while(jp>='0' && jp<='9')
    		out=out*10+jp-'0',jp=getchar();
    	return out*sgn;
    }
    const int P=1e9+7;
    int mul(int a,int b)
    {
    	return 1LL * a * b % P;
    }
    const int MAXN=2e5+10;
    int n,ecnt=0,head[MAXN],nx[MAXN<<1],to[MAXN<<1];
    int pw[MAXN],ans=1;
    void addedge(int u,int v)
    {
    	++ecnt;
    	to[ecnt]=v;
    	nx[ecnt]=head[u];
    	head[u]=ecnt;
    }
    #define lowbit(x) x&(-x)
    int bit[MAXN];
    void add(int x,int c)
    {
    	for(;x<=n;x+=lowbit(x))
    		bit[x]+=c;
    }
    int query(int x)
    {
    	int s=0;
    	for(;x;x-=lowbit(x))
    		s+=bit[x];
    	return s;
    }
    void dfs(int u,int fa)
    {
    	int t=query(u-1);
    	ans=mul(ans,pw[t]);
    	for(int i=head[u];i;i=nx[i])
    	{
    		int v=to[i];
    		if(v==fa)
    			continue;
    		add(v,1);
    		dfs(v,u);
    		add(v,-1);
    	}
    }
    int main()
    {
    	freopen("dfs.in","r",stdin);
    	freopen("dfs.out","w",stdout);
    	n=read();
    	pw[0]=1;
    	for(int i=1;i<n;++i)
    	{
    		int u=read(),v=read();
    		addedge(u,v);
    		addedge(v,u);
    		pw[i]=mul(pw[i-1],2);
    	}
    	dfs(1,0);
    	cout<<ans<<endl;
    	return 0;
    }
    

    未完待续。。。。。

  • 相关阅读:
    Luogu P2016 战略游戏(树形DP)
    Luogu P2486 染色(树链剖分+线段树)
    Luogu P3178 树上操作(树链剖分+线段树)
    Luogu P2590 树的统计(树链剖分+线段树)
    Luogu P2146 软件包管理器(树链剖分+线段树)
    获得spring
    网卡绑定多个ip
    描述01-配置文件咋整
    进程查看
    端口查看,进程杀死
  • 原文地址:https://www.cnblogs.com/wzxbeliever/p/11661549.html
Copyright © 2011-2022 走看看