当前位置: 代码迷 >> 综合 >> hdu 4417(离线 + 树状数组)
  详细解决方案

hdu 4417(离线 + 树状数组)

热度:23   发布时间:2023-11-06 17:34:05.0

做洛谷的题遇到一个要求区间小于 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;
}