Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2616 Accepted Submission(s):
1287
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times.
In the next N lines, each line contains two integer Si and Ti (1 <= Si <= Ti <= 10^9), means i-th flower will be blooming at time [Si, Ti].
In the next M lines, each line contains an integer Ti, means the time of i-th query.
Sample outputs are available for more details.
#include<stdio.h> #include<string.h> #include<algorithm> #define LL long long #define MAX 100100 using namespace std; int s[MAX],e[MAX],q[MAX]; int rec[MAX];//记录所有值排序后的下标 int add[MAX<<2]; int sum[MAX<<2]; int le[MAX],ri[MAX]; void pushup(int o) { sum[o]=sum[o<<1]+sum[o<<1|1]; } void pushdown(int o,int m) { if(add[o]) { add[o<<1]+=add[o]; add[o<<1|1]+=add[o]; sum[o<<1]+=add[o]*(m-(m>>1)); sum[o<<1|1]+=add[o]*(m>>1); add[o]=0; } } void gettree(int o,int l,int r) { add[o]=0; if(l==r) { sum[o]=0; return ; } int mid=(l+r)>>1; gettree(o<<1,l,mid); gettree(o<<1|1,mid+1,r); pushup(o); } void update(int o,int l,int r,int L,int R,int val) { if(L<=l&&R>=r) { add[o]+=val; sum[o]+=val*(r-l+1); return ; } pushdown(o,r-l+1); int mid=(l+r)>>1; if(L<=mid) update(o<<1,l,mid,L,R,val); if(R>mid) update(o<<1|1,mid+1,r,L,R,val); pushup(o); } int find(int o,int l,int r,int pos) { if(l==r) { return sum[o]; } pushdown(o,r-l+1); int ans=0; int mid=(l+r)>>1; if(pos<=mid) ans=find(o<<1,l,mid,pos); else ans=find(o<<1|1,mid+1,r,pos); return ans; } int query(int l,int r,int pos)//查找输入当前值,在树中对应的位置 { while(r>=l) { int mid=(l+r)>>1; if(rec[mid]==pos) return mid; else if(rec[mid]>pos) r=mid-1; else l=mid+1; } return -1; } int main() { int t,n,m,k,i; scanf("%d",&t); k=1; int maxx; while(t--) { scanf("%d%d",&n,&m); int p=1; for(i=0;i<n;i++) { scanf("%d%d",&s[i],&e[i]); rec[p++]=s[i]; rec[p++]=e[i]; } for(i=0;i<m;i++) { scanf("%d",&q[i]); rec[p++]=q[i]; } sort(rec+1,rec+p);// int R=2; for(i=2;i<p;i++)//去除数组中重复的点 { if(rec[i]!=rec[i-1]) rec[R++]=rec[i]; } sort(rec+1,rec+R); gettree(1,1,R-1);//对下标建树 for(int i=0;i<n;i++) { int x=query(1,R-1,s[i]); int y=query(1,R-1,e[i]); update(1,1,R-1,x,y,1); } printf("Case #%d:\n",k++); for(i=0;i<m;i++) { int x=query(1,R-1,q[i]); printf("%d\n",find(1,1,R-1,x)); } } }