树桩数组TLE求调
查看原帖
树桩数组TLE求调
555301
zfx_VeXl6楼主2024/11/13 22:13

rt,对着题解肉眼调试无果

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define lowbit(x) (x)&-(x)
const int N=1e3+5;
const int mod=1e9+7;
int t,n,m,a[N],cnt;
LL tr[N][N],ans;
struct dd
{
	int i,x;
	bool operator<(const dd xx){return x<xx.x;}
}c[N];
LL mo(LL x){return x>=mod?x-mod:x;}
void upd(int o,int x,LL v){for(;x<=cnt;x+=lowbit(x))tr[o][x]=mo(tr[o][x]+v);}
LL qr(int o,int x){LL r=0;for(;x;x-=lowbit(x))r=mo(r+tr[o][x]);return r;}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	for(int oo=1;oo<=t;oo++)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			c[i]={i,a[i]};
		}
		cnt=0;
		sort(c+1,c+n+1);
		for(int i=1;i<=n;i++)
			if(c[i].x!=c[i-1].x)
				a[c[i].i]=++cnt;
			else
				a[c[i].i]=cnt;
		memset(tr,0,sizeof(tr));
		ans=0;
		for(int i=1;i<=n;i++)
		{
			upd(1,a[i],1);
			for(int j=2;j<m;j++)
				upd(j,a[i],qr(j-1,a[i]-1));
			ans=mo(ans+qr(m-1,a[i]-1));
		}
		if(m==1)
			ans=n;
		cout<<"Case #"<<oo<<": "<<ans<<'\n';
	}
	return 0;
}
2024/11/13 22:13
加载中...