【玄关】本题无法用树状数组做吗?
查看原帖
【玄关】本题无法用树状数组做吗?
592751
xiaoke2021楼主2024/4/28 22:55

考场用了dfs喜提暴力分,后来突然想出树状数组+查分的做法。同学传了一组数据,已AC,但本题全TLE。本题无法用树状数组做吗?如果可以,求条。

#include<bits/stdc++.h>
#define int long long  
using namespace std;
const int MAXN=200000+5;
const int M=10007;
int n,m,k;
int c[MAXN];
int tree[MAXN];
int cnt[MAXN];
int o[MAXN],l[MAXN],r[MAXN];
int a[MAXN];
inline int read(){
	char ch;
	int ans=0,f=1;
	ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-') f=-1;
	}while(isdigit(ch)){
		ans=ans*10+ch-'0';
		ch=getchar();
	}return ans*f;
}
int lowbit(int t){
	return t&(-t);
}
void update(int pos,int t){
	for(int i=pos;i<=m;i+=lowbit(i)){
		tree[i]+=t;
	}
}
int query(int pos){
	int ans=0;
	while(pos){
		ans+=tree[pos];
		ans%=M;
		pos-=lowbit(pos);
	}return ans;
}
signed main(){
	n=read(),m=read(),k=read();
	for(int i=1;i<=k;i++){
		int x;
		x=read();
		cnt[x]++;
		cnt[x]%=M;
	}
	for(int i=1;i<=m;i++){
		update(i,cnt[i]-cnt[i-1]);
	}for(int i=1;i<=m;i++){
		cin>>o[i]>>l[i]>>r[i];
		if(o[i]==1){
			r[i]=(r[i]%M+M)%M;
		}
	}for(int i=m;i>0;i--){
		if(o[i]==1){
			a[l[i]]=(a[l[i]]+query(i)%M*r[i]%M)%M;
		}else{
			int t=query(i);
			update(l[i],t);
			update(r[i]+1,-t);
		}
	}for(int i=1;i<=n;i++) cout<<a[i]<<" ";
	return 0;
}

附:同学传的数据的那道题的AC record and TLE record

2024/4/28 22:55
加载中...