跳到正文

个人记录

树状数组进阶

flamefrag约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年11月2日
收录
2026年11月26日
更新
2026年11月26日
篇幅
约 7 分钟

版本

  1. v1当前

u125682

很经典的应用,注意输入的时候就是按照y坐标来排序给出的,所以我们就把y固定就行了。

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=3.2*10000+5;
int n;
int c[maxn];
struct stu
{
	int x,y;
}a[maxn];
map<int,int>op;
bool cmp(stu a,stu b)
{
	if(a.x!=b.x)
	{
		return a.x<b.x;
	}
	return a.y<b.y;
}
int lowbit(int x)
{
	return x&-x;
}
int query(int x)
{
	int res=0;
	while(x>0)
	{
		res+=c[x];
		x-=lowbit(x); 
	}
	return res;
}
void update(int x,int id)
{
	while(x<=32001)
	{
		c[x]+=id;
		x+=lowbit(x); 
	}
}
signed main()
{
	cin>>n;	
	for(int i=1;i<=n;++i)
	{
		cin>>a[i].x>>a[i].y;
		a[i].y++; 
	} 
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;++i)
	{
		op[query(a[i].y)]++;
		update(a[i].y,1);
	}
	for(int i=0;i<n;++i)
	{
		cout<<op[i]<<'\n';
	}
	return 0;
} 

P5094

一.我们考虑将v排序,因为这样枚举i时,max(v[i],v[j])=v[i]就可以去除max的影响。

二.我们统计[1,i-1]中所有与i音量贡献之和,v[i]*(i与前i-1头牛的距离和)

三.公式:

1.(i与前i-1头牛的距离和)=(i与其左侧牛的距离之和)+(i与其右侧牛的距离之和)

2.(i与其左侧牛的距离之和) = sigma(x[i]-x[j])=x[i]*(i左侧牛的数量-i左侧牛的坐标之和)

3.(i与其右侧牛的距离之和)=sigma(x[j]-x[i])=(i右侧牛的坐标之和-i右侧牛的数量*x[i])

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=5e5+9;
const int mod=998244353;
struct stu
{
	int u,x;
}a[maxn];
int n;
int como[maxn];
int ctot[maxn]; 
bool cmp(stu a,stu b)
{
	return a.u<b.u;
}
int lowbit(int x)
{
	return x&(-x); 
}
int query_omo(int x)//坐标 
{
	int res=0;
	while(x!=0)
	{
		res+=como[x];
		x-=lowbit(x);
	}
	return res;
} 
int query_tot(int x)//数量 
{
	int res=0;
	while(x!=0)
	{
		res+=ctot[x];
		x-=lowbit(x);
	}
	return res;
} 
void update_omo(int x,int val)
{
	while(x<=maxn)
	{
		como[x]+=val;
		x+=lowbit(x);
	}
}
void update_tot(int x,int val)
{
	while(x<=maxn)
	{
		ctot[x]+=val;
		x+=lowbit(x);
	}
}
signed main()
{
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i].u>>a[i].x;
	}
	sort(a+1,a+n+1,cmp);
	int sum=0;
	for(int i=1;i<=n;++i)
	{
		int lt=a[i].u*(a[i].x*query_tot(a[i].x)-query_omo(a[i].x))%mod;
		int rt=a[i].u*(query_omo(50000)-query_omo(a[i].x)-(i-1-query_tot(a[i].x))*a[i].x)%mod;
		sum=(sum+lt+rt)%mod;
		update_omo(a[i].x,a[i].x);
		update_tot(a[i].x,1);
	}
	cout<<sum<<'\n';
	return 0;
}

POJ-2481

1.我们可以把[s,e]看作一组坐标,转化为二维数点问题,第一维不超过,第二维不小于,不含同坐标的点。

2.先把s看作第一维,我们从小到大排序,s相等的,按e从小到大排序

3.枚举到区间i时,s维绝对符合要求,只要找到前i-1个区间中e不小于e[i]的数量,i-1-query(e[i]-1)

4.若区间i与区间i-1完全重叠,那么ans[i]=ans[i-1];

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e5+9;
struct stu
{
	int s,e,id,ans;
}a[maxn];
int c[maxn];
int ans=0,n;
int lowbit(int x)
{
	return x&(-x); 
}
int query(int x)
{
	int res=0;
	while(x>0)
	{
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
} 
void update(int x,int val)
{
	while(x<=1e5+1)
	{
		c[x]+=val;
		x+=lowbit(x);
	}
}
bool cmp1(stu a,stu b)
{
	if(a.s!=b.s)
	{
		return a.s<b.s;
	}
	return a.e>b.e;	
} 
bool cmp2(stu a,stu b)
{
	return a.id<b.id;
}
void work()
{
	while(cin>>n)
	{
		for(int i=1;i<=n;++i)
		{
			cin>>a[i].s>>a[i].e;
			a[i].e++;
			a[i].id=i;
		}
		memset(c,0,sizeof(c));
		sort(a+1,a+1+n,cmp1);
		for(int i=1;i<=n;++i)
		{
			if(a[i].s==a[i-1].s && a[i].e==a[i-1].e) 
			{
				a[i].ans=a[i-1].ans;
			}
			else
			{
				a[i].ans=i-1-query(a[i].e-1);
			}	
			update(a[i].e,1);
		}
		sort(a+1,a+1+n,cmp2);
		for(int i=1;i<=n;++i)
		{
			cout<<a[i].ans<<' ';
		}
		cout<<'\n';
	}
} 
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	work();
	return 0;
}
原文