Super Mario
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9618 Accepted Submission (s): 4074
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 Input110 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 Outputcase 1:4,003,120,151
Source2012 ACM/ICPC Asia Regional Hangzhou Online Test instructions is the number of intervals less than or equal to H. This inscribed time is a bit long, wrong in these places: (1) because the number is starting from 0, so when the query, Interval l[i] and r[i] should +1, when the query l[i]-1,r[i], or the interval l[i] and r[i] do not change, query, on l[i],r[i]+1. (2) Because the query is less than or equal to the number of H, so H should also be discretized, find the corresponding number, rather than direct query, here is a long time to find. (3) The array opened small, Hangzhou electric This problem array opened a small newspaper is WA, the maxn=1e5+10 changed into 2e5+10 on the past. There is a point, in fact, his brain is not good deliberately measured a bit. Because the data has been discretized processing, so the query will not have a number of repetitions, so when the query, Lower_bound () and Upper_bound () can be.
// int Cnt=lower_bound (b+1,b+1+d,h[i])-B; int Cnt=upper_bound (b +1, B +1+d,h[i])-b-1;
Both of the above are right.
Code:
1 //No modification Interval-can persist segment tree (weight segment tree + persistent)2#include <iostream>3#include <cstdio>4#include <cstring>5#include <algorithm>6#include <bitset>7#include <cassert>8#include <cctype>9#include <cmath>Ten#include <cstdlib> One#include <ctime> A#include <deque> -#include <iomanip> -#include <list> the#include <map> -#include <queue> -#include <Set> -#include <stack> +#include <vector> - using namespacestd; +typedefLong Longll; Atypedef pair<int,int>PII; at - Const DoublePi=acos (-1.0); - Const Doubleeps=1e-6; - Constll mod=1e9+7; - Const intinf=0x3f3f3f3f; - Const intmaxn=2e5+Ten; in Const intmaxm= -+Ten; - #defineiOS Ios::sync_with_stdio (false); Cin.tie (0); Cout.tie (0); to #defineLson l,m + #defineRson M+1,r - the inta[maxn],b[maxn],sum[maxn<<5],ls[maxn<<5],rs[maxn<<5];//sum the saved values in the segment tree, L left son, R right son * intn,m,sz=0; $ Panax Notoginseng voidBuildint&rt,intLintR//build an empty tree - { thert=++sz;sum[rt]=0;//Dynamic open point, initial value is 0, empty tree + if(l==R) { A return ; the } + - intM= (l+r) >>1; $ build (Ls[rt],lson); $ build (Rs[rt],rson); - } - the voidUpdateintPreint&rt,intLintRintPintc) - {WuyiRt=++sz;sum[rt]=sum[pre]+c;//insert sequence, first inherit the previous line segment tree and then direct single point +1 can be thels[rt]=ls[pre];rs[rt]=Rs[pre]; - if(l==R) { Wu return ; - } About $ intM= (l+r) >>1; - if(p<=m) update (LS[PRE],LS[RT],LSON,P,C);//because the right side does not need to be updated, so overwrite left - ElseUpdate (RS[PRE],RS[RT],RSON,P,C); - //Sum[rt]=sum[ls[rt]]+sum[rs[rt]]; A } + the intQueryintPreintRtintLintRintLintR//The query L to r interval is the first R-insertion minus the l-1 of the segment tree. - { $ if(L>r)return 0; the if(l<=l&&r<=R) { the returnsum[rt]-Sum[pre]; the } the - intret=0; in intM= (l+r) >>1; the if(l<=m) ret+=query (Ls[pre],ls[rt],l,r,lson); the if(r> m) ret+=query (Rs[pre],rs[rt],l,r,rson); About returnret; the } the the intRT[MAXN],L[MAXN],R[MAXN],H[MAXN]; + - intMain () the {Bayi intT; thescanf"%d",&t); the for(intcas=1; cas<=t;cas++){ -scanf"%d%d",&n,&m); -sz=0; the for(intI=1; i<=n;i++) the { thescanf"%d",&a[i]); theb[i]=A[i]; - } the for(intI=1; i<=m;i++){ thescanf"%d%d%d",&l[i],&r[i],&h[i]); theb[i+n]=H[i];94l[i]++,r[i]++; the } theSort (b +1, B +1+N+M);//first, the values are all sorted to the weight of the tree, the weight of the segment tree to save the content is the number of values. the intD=unique (b +1, B +1+N+M)-(b +1);98Build (rt[0],1, d); About for(intI=1; i<=n;i++)//inserting values in sequence order - {101 intP=lower_bound (b +1, B +1+d,a[i])-b;102Update (rt[i-1],rt[i],1, D,p,1);103 }104printf"Case %d:\n", CAs); the for(intI=1; i<=m;i++)106 {107 //int L=1,r=upper_bound (b+1,b+1+d,h)-b-1;108 //int Cnt=lower_bound (b+1,b+1+d,h[i])-B;109 intCnt=upper_bound (b +1, B +1+d,h[i])-b-1; the //printf ("%d\n", Query (Rt[l[i]],rt[r[i]+1],1,cnt,1,d));111 //printf ("%d\n", Query (Rt[l],rt[r+1],1,cnt,1,d)); theprintf"%d\n", Query (rt[l[i]-1],rt[r[i]],1Cnt1, D));113 } the } the return 0; the}
The discomfort of the dish ...
HDU 4417.Super mario-Number of non-modified intervals less than or equal to H-persisted segment tree