hdu 4417(离线 + 树状数组)

做洛谷的题遇到一个要求区间小于 x值个数的,就搜到了这个题,可惜这种做法是离线的,我想要在线算法,好像是要用主席树,先写个简单的离线吧。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#define mem(a,x) memset(a,x,sizeof(a));
#define s1(x) scanf("%d",&x);
#define s2(x,y) scanf("%d%d",&x,&y);
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z);
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k);
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
const ll inf = 0x3f3f3f3f;
/*void dis(int a[], int n){
	printf("总数为%d个\n",n); 
	for(int i = 0; i < n; i++) 	cout<<a[i]<<", ";
	cout<<endl<<"------------------"<<endl;		
}*/

const int mx = 1e5+10;
int node[mx],ans[mx],n,m;

void  update(int x ,int k){
	
	while(x <= n){
		node[x] += k;
		x += x&(-x);
	}  
    
}  
 
int sea(int x){  
    int ans = 0;
   	while(x){
	 ans += node[x];
	 x -= x&(-x);    
	}
    
    return ans;  
}  


struct q1{
	int l,r,x,id;
	bool operator < (const q1 &te){
		return x < te.x;
	}
}a[mx]; 

struct q2{
	int id,vaule;
	bool operator <(const q2 & te){
		return vaule < te.vaule;
	}
}b[mx];

int main(){
	int T,l,r;
	s1(T);
	for(int ca = 1; ca <= T; ca++){
		mem(node,0);
		s2(n,m);
		for(int i = 1; i <= n; i++){
			b[i].id = i;
			s1(b[i].vaule);
		}
		
		for(int i = 1; i <= m; i++){
			a[i].id = i;
			s3(l,r,a[i].x);    
			a[i].l = l + 1;       //忘了加1 树状数组会炸 
			a[i].r = r + 1;
		}
		//sea(0);
	//	cout<<"zha"<<endl;
		sort(a+1, a+1+m);
		sort(b+1, b+1+n);
		
		for(int i = 1,j = 1; i <= m; i++){
			while(j <= n && b[j].vaule <= a[i].x){
				update(b[j].id,1);      //应该是插入1 才对 
				j++;
			}
			
			ans[a[i].id] = sea(a[i].r) - sea(a[i].l-1);
		}
		
		printf("Case %d:\n",ca);
		
		for(int i = 1; i <= m; i++)		
			printf("%d\n",ans[i]);
	
	}
	
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Blaze Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值