1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std;; 5 const int N=1e5+7; 6 struct T { 7 int key; 8 int id; 9 }; 10 struct ask { 11 int lr; 12 int hr; 13 int val; 14 int id; 15 }; 16 T arr[N]; 17 ask q[N]; 18 int tree[N]; 19 int ans [N]; 20 int n,m; 21 bool cmp1 (T a,T b) { 22 return a.key<b.key; 23 } 24 bool cmp2 (ask a,ask b) { 25 return a.val<b.val; 26 } 27 void add (int x,int k) { 28 while (k<=n) { 29 tree[k]+=x; 30 k+=k&(-k); 31 } 32 } 33 int sum (int k) { 34 int _ans=0; 35 while (k) { 36 _ans+=tree[k]; 37 k-=k&(-k); 38 } 39 return _ans; 40 } 41 int main () 42 { 43 int T; 44 scanf ("%d",&T); 45 int num=1; 46 while (T--) { 47 printf("Case %d:\n",num++); 48 scanf ("%d %d",&n,&m); 49 for (int i=1;i<=n;i++) { 50 scanf ("%d",&arr[i].key); 51 arr[i].id=i; 52 } 53 sort (arr+1,arr+1+n,cmp1); 54 for (int i=1;i<=m;i++) { 55 scanf ("%d %d %d",&q[i].lr,&q[i].hr,&q[i].val); 56 q[i].id=i; 57 } 58 sort (q+1,q+1+m,cmp2); 59 memset (tree,0,sizeof(tree)); 60 int top=1; 61 for (int i=1;i<=m;i++) { 62 while (top<=n&&arr[top].key<=q[i].val) { 63 add(1,arr[top].id); 64 top++; 65 } 66 ans[q[i].id]=sum(q[i].hr+1)-sum(q[i].lr); 67 } 68 for (int i=1;i<=m;i++) 69 printf("%d\n",ans[i]); 70 } 71 return 0; 72 }
抓住青春的尾巴。。。