6.2收费

就是一棵完全二叉树,然后两种操作,一种加收费,一种查收费
因为二叉树的根节点的编号是x/2,直接根据这个规律就好了,记得开ll

#include<bits/stdc++.h>
#define sf scanf
#define scf(x) scanf("%d",&x)
#define pf printf
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
void in(ll &x){
int f=1;x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;}
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1000005;
ll a[N];
ll n,tnt,x,y,w;
void Add(ll x,ll y,ll w)
{
	while(1)
	{
		if(x>y)
		{
			a[x]+=w;
			x/=2;
		}else if(x<y)
		{
			a[y]+=w;
			y/=2;
		}else
			break;
	}
}
ll query(ll x,ll y)
{
	ll ans=0;
	while(1)
	{
		if(x>y)
		{
			ans+=a[x];
			x/=2;
		}else if(x<y)
		{
			ans+=a[y];
			y/=2;
		}else
		{
			return ans;
		}
	}
}
void init()
{
	in(tnt);
	if(tnt==1)
	{
		in(x);in(y);in(w);
		Add(x,y,w);
	}else
	{
		in(x);in(y);
		ll ans;
		ans=query(x,y);
		pf("%lld\n",ans);
	}
}
int main()
{
	mm(a,0);
	in(n);
	while(n--)
		init();
}
posted @ 2018-12-08 12:39  一无所知小白龙  阅读(384)  评论(0编辑  收藏  举报