Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2597 Accepted Submission(s): 1278
Problem Description
As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flowers will bloom in the garden in a specific time. But there are too many flowers in the garden, so he wants you to help him.
Input
The first line contains a single integer t (1 <= t <= 10), the number of test cases.
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 S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
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 S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
Output
For each case, output the case number as shown and then print M lines. Each line contains an integer, meaning the number of blooming flowers.
Sample outputs are available for more details.
Sample outputs are available for more details.
Sample Input
2 1 1 5 10 4 2 3 1 4 4 8 1 4 6
Sample Output
Case #1: 0 Case #2: 1 2 1分析:这道题本来是用线段树写的,但是这道题杭电的数据有点弱,1e9竟然没有超时。第二种方法是看的男神的,用二分写的很巧妙,佩服佩服。代码1【二分】:#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<stack> #include<cmath> using namespace std; const int inf=0x3f3f3f3f; const int maxn =1e5+100; #define mem(x,y) memset(x,y,sizeof(x)); int s[maxn],e[maxn]; int main() { int t; int n,m,cnt=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d%d",&s[i],&e[i]); sort(s,s+n); sort(e,e+n); // for(int i=0;i<n;i++) // printf("%d ",s[i]); // puts(""); // for(int i=0;i<n;i++) // printf("%d ",e[i]); // puts(" "); int q; printf("Case #%d:\n",++cnt); while(m--) { scanf("%d",&q); int x=upper_bound(s,s+n,q)-s; int y=lower_bound(e,e+n,q)-e; printf("%d\n",x-y); } } system("pause"); return 0;
}
代码2:【线段树】
<pre class="cpp" name="code">#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<stack> using namespace std; const int maxn=100001; struct node{ int l; int r; int val; }t[maxn<<2]; void build(int l,int r,int k) { t[k].l=l; t[k].r=r; t[k].val=0; if(l==r) return; int mid=(l+r)>>1; build(l,mid,k<<1); build(mid+1,r,k<<1|1); } void update(int l,int r,int k) { if(t[k].l==l&&t[k].r==r) { t[k].val++; return ; } int mid=(t[k].l+t[k].r)>>1; if(l>mid) update(l,r,k<<1|1); else if(r<=mid) update(l,r,k<<1); else{ update(l,mid,k<<1); update(mid+1,r,k<<1|1); } } int query(int l,int r,int k) { if(t[k].l==l&&t[k].r==l) return t[k].val; //if(t[k].l==t[k].r) //return 0; int mid=(t[k].l+t[k].r)>>1; if(l>mid) return query(l,r,k<<1|1)+t[k].val; else if(r<=mid) return query(l,r,k<<1)+t[k].val; else return query(l,mid,k<<1)+query(mid+1,r,k<<1|1); } int main() { int T; int n,m; int cnt=0; scanf("%d",&T); while(T--) { int a,b,c; build(1,maxn,1); scanf("%d%d",&n,&m); while(n--) { scanf("%d%d",&a,&b); update(a,b,1); } printf("Case #%d:\n",++cnt); while(m--) { scanf("%d",&c); printf("%d\n",query(c,c,1)); } } return 0; }