Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4090 Accepted Submission(s): 1883
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 struct Node{ 7 int rs,ls,sum; 8 }tr[2500010]; 9 int A[100010],B[100010]; 10 int rt[100010],pos,cnt; 11 void Build(int &node,int a,int b) 12 { 13 node=++cnt; 14 if(a==b)return; 15 int mid=(a+b)>>1; 16 Build(tr[node].ls,a,mid); 17 Build(tr[node].rs,mid+1,b); 18 } 19 20 void Insert(int pre,int &node,int a,int b) 21 { 22 node=++cnt; 23 tr[node].ls=tr[pre].ls; 24 tr[node].rs=tr[pre].rs; 25 tr[node].sum=tr[pre].sum+1; 26 if(a==b)return; 27 int mid=(a+b)>>1; 28 if(mid>=pos)Insert(tr[pre].ls,tr[node].ls,a,mid); 29 else Insert(tr[pre].rs,tr[node].rs,mid+1,b); 30 } 31 int Query(int pre,int node,int p,int a,int b) 32 { 33 if(b<=p)return tr[node].sum-tr[pre].sum; 34 int ret=0; 35 ret=Query(tr[pre].ls,tr[node].ls,p,a,(a+b)>>1); 36 if(p>(a+b)>>1) 37 ret+=Query(tr[pre].rs,tr[node].rs,p,((a+b)>>1)+1,b); 38 return ret; 39 } 40 void Solve(int pre,int node,int h,int a,int b) 41 { 42 if(a==b){ 43 pos=a; 44 return; 45 } 46 if(B[((a+b)>>1)+1]<=h)Solve(tr[pre].rs,tr[node].rs,h,((a+b)>>1)+1,b); 47 if(pos==-1&&B[a]<=h)Solve(tr[pre].ls,tr[node].ls,h,a,(a+b)>>1); 48 } 49 void Init() 50 { 51 memset(tr,0,sizeof(tr)); 52 cnt=0; 53 } 54 int main() 55 { 56 int Q,n,q,kase=0; 57 scanf("%d",&Q); 58 while(Q--) 59 { 60 Init(); 61 printf("Case %d:\n",++kase); 62 scanf("%d%d",&n,&q); 63 for(int i=1;i<=n;B[i]=A[i],i++) 64 scanf("%d",&A[i]); 65 sort(B+1,B+n+1); 66 Build(rt[0],1,n); 67 for(int i=1;i<=n;i++) 68 { 69 pos=lower_bound(B+1,B+n+1,A[i])-B; 70 Insert(rt[i-1],rt[i],1,n); 71 } 72 int l,r,h; 73 for(int i=1;i<=q;i++) 74 { 75 scanf("%d%d%d",&l,&r,&h);l++;r++; 76 pos=-1;Solve(rt[l-1],rt[r],h,1,n); 77 if(pos==-1){ 78 printf("0\n"); 79 continue; 80 } 81 printf("%d\n",Query(rt[l-1],rt[r],pos,1,n)); 82 } 83 } 84 return 0; 85 }