捞一个题
  • 板块学术版
  • 楼主I_AK_CTS
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/11/27 18:54
  • 上次更新2024/11/27 20:37:49
查看原帖
捞一个题
643818
I_AK_CTS楼主2024/11/27 18:54

https://www.luogu.com.cn/problem/P10814

这道题被神秘卡常,不知道为什么T了3个点。

树状数组都能被卡常吗?

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e6+5;
int n,m,a[N],cnt=0,ans[N];
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^'0'),ch=getchar();
	return x*f;
}
void write(int x){
	if(x<0) x=-x,putchar('-');
	if(x>=10) write(x/10);
	putchar(x%10+'0');
}
struct query{
	int id,r,x,flag;
} q[N<<1];
bool cmp(query x,query y){
	return x.r<y.r;
}
template<typename TT>
class BIT{
	int T[N];
	int lowbit(int x){
		return x&-x;
	}
public:
	void upd(int x,int k){
		for(;x<=N-5;x+=lowbit(x))
			T[x]+=k;
	}
	int query(int x){
		int res=0;
		for(;x;x-=lowbit(x)) res+=T[x];
		return res;
	} 
};
BIT<int> T;
signed main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=m;i++){
		int l,r,x;l=read(),r=read(),x=read();
		q[++cnt].r=l-1,q[cnt].id=i,q[cnt].flag=0,q[cnt].x=x;
		q[++cnt].r=r,q[cnt].id=i,q[cnt].flag=1,q[cnt].x=x;
	}
	for(int i=1;i<=cnt;i++){
		for(int j=q[i-1].r+1;j<=q[i].r;j++) T.upd(a[j],1);
		if(!q[i].flag) ans[q[i].id]=-T.query(q[i].x);
		else ans[q[i].id]+=T.query(q[i].x);
	}
	for(int i=1;i<=m;i++) write(ans[i]),puts("");
	return 0;
}

2024/11/27 18:54
加载中...