http://acm.hdu.edu.cn/showproblem.php?pid=4417
Super MarioTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2720 Accepted Submission (s): 1322
Problem Descriptionmario is world-famous plumber. He "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess are in trouble again and Mario needs to save his lover. We regard the road to the boss's Castle as a line (the length are N), on every integer point I there are a brick on height h I. Now the question is what many bricks in [L, R] Mario can hits if the maximal height he can jump is H.
Inputthe first line follows an integer T, the number of test data.
For each test data:
The first line contains-integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, M-T He number of queries.
Next line contains n integers, the height of each brick, and the range is [0, 1000000000].
Next m lines, each line contains three integers L, r,h. (0 <= L <= R < n 0 <= H <= 1000000000.)
Outputfor each case, output ' Case x: ' (x is the case number starting from 1) followed by M lines, each line contains an I Nteger. The ith integer is the number of bricks Mario can hits for the ith query.
Sample Input
110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3
Sample Output
Case 1:4,003,120,151
Source2012 ACM/ICPC Asia Regional Hangzhou Online
Test instructions: Number of N, M inquiry [L,r] interval is less than or equal to the number of H.
Train of thought: Originally is a partition tree naked question, bad partition tree 233. But a tree-like array can also be used.
Save all queries offline. According to the H size from small to large processing each inquiry, for each inquiry, the query [l,r] interval is smaller than H, in fact is the query [1,r] interval minus [1,l-1] interval, we put the number of smaller than h into the tree array, here is the location of the original array is inserted, and then query, You can figure out every single question.
/** * @author neko01 *///#pragma comment (linker, "/stack:102400000,102400000") #include <cstdio> #include < cstring> #include <string.h> #include <iostream> #include <algorithm> #include <queue># Include <vector> #include <cmath> #include <set> #include <map>using namespace Std;typedef long Long LL; #define MIN3 (a,b,c) min (a,min (b,c)) #define MAX3 (A,B,C) max (A,max (b,c)) #define PB Push_back#define MP (A, b) make_ Pair (A, B) #define CLR (a) memset (a,0,sizeof a) #define CLR1 (a) memset (a,-1,sizeof a) #define DBG (a) printf ("%d\n", a) typedef pair<int,int> PP;CONST Double Eps=1e-8;const double Pi=acos ( -1.0); const int Inf=0x7fffffff;const LL inf= (( (LL) 1) <<61) +5;const int n=100005;struct node{int l,r,val; int id;} Q[n];int ans[n];int bit[n];struct hehe{int x,id;} A[n];int n,m;bool cmp1 (hehe U,hehe v) {return u.x<v.x;} BOOL CMP2 (node U,node v) {return u.val<v.val;} int sum (int x) {int s=0; while (x>0) {S+=bit[x]; x-=x&-x; } return s;} void Add (int x,int val) {while (x<=n) {bit[x]+=val; x+=x&-x; }}int Main () {int t,cnt=0; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); CLR (bit); for (int i=1;i<=n;i++) {scanf ("%d", &a[i].x); A[i].id=i; } for (int i=0;i<m;i++) {scanf ("%d%d%d", &q[i].l,&q[i].r,&q[i].val); q[i].l++; q[i].r++; Q[i].id=i; } sort (A+1,A+N+1,CMP1); Sort (Q,Q+M,CMP2); int j=1; for (int i=0;i<m;i++) {while (j<=n&&q[i].val>=a[j].x) {Add (a[ j].id,1); j + +; } ans[q[i].id]=sum (Q[I].R)-sum (Q[I].L-1); } printf ("Case%d:\n", ++cnt); for (int i=0;i<m;i++) printf ("%d\n", Ans[i]); } return 0;}
hdu4417 Super Mario tree-like array offline/partition tree