hdu 4417 Super Mario(树状数组+离线处理)
2012-09-23 23:18
399 查看
按H排序,每次查询把不大于H的值插入到树状数组中的相应位置并完成查询。
171MS
View Code
171MS
View Code
/* Author:Zhaofa Fang Lang:C++ */ #include <cstdio> #include <cstdlib> #include <iostream> #include <cmath> #include <cstring> #include <algorithm> #include <string> #include <utility> #include <vector> #include <queue> #include <stack> #include <map> #include <set> using namespace std; typedef long long ll; #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define lowbit(x) (x&(-x)) const int INF = 2147483647; const int maxn = 100005; int C[maxn]; struct query { int l,r,h,pos,ans; }q[maxn]; struct bri { int num,pos; }a[maxn]; int n,m; int cmp1(bri a,bri b) { return a.num<b.num; } int cmp2(query a,query b) { return a.h<b.h; } int cmp3(query a,query b) { return a.pos<b.pos; } void add(int x,int pos) { while(pos<=n) { C[pos] += x; pos += lowbit(pos); } } int sum(int pos) { int res = 0; while(pos > 0) { res += C[pos]; pos -= lowbit(pos); } return res; } int main() { #ifndef ONLINE_JUDGE freopen("in","r",stdin); #endif int T; int cas=0; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(C,0,(n+1)*sizeof(int)); for(int i=0;i<n;i++) { scanf("%d",&a[i].num); a[i].pos = i + 1; } sort(a,a+n,cmp1); printf("Case %d:\n",++cas); for(int i=0;i<m;i++) { scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h); q[i].pos=i + 1; } sort(q,q+m,cmp2); int k=0; for(int i=0;i<m;i++) { while(a[k].num <= q[i].h && k < n) { add(1,a[k].pos); k++; } q[i].ans = sum(q[i].r+1) - sum(q[i].l); } sort(q,q+m,cmp3); for(int i=0;i<m;i++) { printf("%d\n",q[i].ans); } } return 0; }
相关文章推荐
- HDU 4417 Super Mario (树状数组+离线处理)(划分树+二分答案)
- [线段树+离线处理] hdu 4417 Super Mario
- HDU 4417 —— Super Mario(树状数组,离散化,离线处理)
- hdu 4417 树状数组离线处理
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)
- HDU-4417:Super Mario(树状数组+离线操作)
- HDU 4417 Super Mario(树状数组离线处理 or 主席树)
- HDU 4417 Super Mario(线段树离线处理/主席树)
- [hdu 4417]树状数组+离散化+离线处理
- HDU 4417 Super Mario (树状数组、离线处理)
- HDU 4417 Super Mario(12年杭州网络赛-H题-离线 + 树状数组)
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之树状数组篇)
- HDU 4417 Super Mario [树状数组+离线操作]【数据结构】
- HDU 5869 Different GCD Subarray Query (离线处理 树状数组)
- HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)
- hdu 4417:Super Mario(主席树或离线操作)
- hdu 3874 树状数组+离线处理
- HDU 4417 Super Mario (树状数组 + 离线)
- hdu 4417 Super Mario (主席树+离线)
- HDU 3333 Turing Tree --树状数组+离线处理