afterward

导航

 

先展示一个我同学些的代码,超级good

View Code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int C[150005];
int B[150005];
//int Out[150005];
int N=150005;
int Lowbit(int x)
{
    return x&(-x);
}
void Modify(int i,int x)
{
    while(i>0)
    {  cout<<i<<" : "<<C[i]<<"  "<<Lowbit(i)<<endl;
        C[i]+=x;
        i-=Lowbit(i);
    }
     cout<<i<<" : "<<C[i]<<endl;
}
int Sum(int n)
{
    int sum=0;
    while(n<=N)
    {   cout<<n<<" "<<C[n]<<" "<<Lowbit(n)<<endl;
        sum+=C[n];
        n+=Lowbit(n);
    }

    return sum;
}
int main()
{
    int t;
    cin>>t;
    int Case=1;
    while(t--)
    {
        int i,n,m;
        int x,y;
        memset(C,0,sizeof(C));
        memset(B,0,sizeof(B));
      //  memset(Out,0,sizeof(Out));
        scanf("%d%d",&n,&m);
        for (i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            Modify(x-1,-1);
            Modify(y,1);
           // Out[Sum(x)-1]++;
        }
        printf("Case #%d:\n",Case);
        int q;
        for (i=1;i<=m;i++)
        {
            scanf("%d",&q);
            printf("%d\n",Sum(q));
        }
        Case++;
    }
    return 0;
}

 

为了节约时间减少数据量的算法,这才是算法嘛!这个是网上找的,很复杂,勉强看懂,保存!

View Code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100005
struct node{
    int l,r,s;
}a[N*6];
int pa[N*2],cou,sum,cou1,tt;
int aa[N],bb[N],qq[N];
void build(int i,int left,int right){     //i=1
    a[i].l=left;
    a[i].r=right;
    a[i].s=0;
    cout<<" build  a["<<i<<"]:"<<a[i].l<<' '<<a[i].r<<' '<<a[i].s<<endl;
    if(a[i].l==a[i].r) return ;
    int mid=(left+right)>>1;             //相当于除以 2
    build(i*2,left,mid);                 //i=2  4 5   8 9   10 11   ....
    build(i*2+1,mid+1,right);            //i=3  6 7   12  13   14  15   ....
}
void insert(int i,int left,int right){
    if(a[i].l==left&&a[i].r==right){
        a[i].s++;  cout<<"insert a["<<i<<"]:"<<a[i].l<<' '<<a[i].r<<' '<<a[i].s<<endl;
        return ;
    }
    int mid=(a[i].l+a[i].r)>>1;
    if(left>mid) insert(i*2+1,left,right);
    else if(right<=mid) insert(i*2,left,right);
    else{
        insert(i*2,left,mid);
        insert(i*2+1,mid+1,right);
    }
}
int  querry(int i,int x){
    sum+=a[i].s;
    if(x==a[i].l&&x==a[i].r){
        return sum;
    }
    int mid=(a[i].l+a[i].r)>>1;
    if(x>mid) {
        querry(i*2+1,x);
    }
    else if(x<=mid) {
        querry(i*2,x);
    }
}
int find(int x){
    int l=1,r=tt-1;
    while(r>=l){
        int mid=(l+r)>>1;
        if(x>pa[mid]) l=mid+1;
        if(x<pa[mid]) r=mid-1;
        if(x==pa[mid]) return mid;
    }
    return 0;
}

int main(){
//    freopen("in.txt","r",stdin);
    int t,n,m,x,y,z,p=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        cou=1;
        for(int i=0;i<n;i++){
            scanf("%d%d",&x,&y);
            aa[i]=x;
            bb[i]=y;
            pa[cou++]=x;
            pa[cou++]=y;
        }
        for(int i=0;i<m;i++){
             scanf("%d",&qq[i]);
             pa[cou++]=qq[i];
        }
         printf("Case #%d:\n",p++);
        sort(pa+1,pa+cou);
for(int i=1;i<cou;i++) cout<<pa[i]<<" ";
cout<<endl;
        int cou1=1;
        pa[cou]=-1;
        for(int i=1;i<cou;i++){
            if(pa[i]!=pa[i+1]) pa[cou1++]=pa[i];
            else continue;

        }
for(int i=1;i<cou1;i++) cout<<pa[i]<<" ";
cout<<endl;
        build(1,0,cou1-1);

        tt=cou1;
        for(int i=0;i<n;i++){
            int ta=find(aa[i]);
            int tb=find(bb[i]);
            insert(1,ta,tb);
        }
        for(int i=0;i<m;i++){
            int z=find(qq[i]);
            sum=0;
            printf("%d\n",querry(1,z));
        }
    }
    return 0;
}

 

  下面是我简单的头脑可以想出来的不明白为什么同样的方法用c++写超时了而c写就过了

Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 288    Accepted Submission(s): 194


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 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.

 

 

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 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

 

 这个超时了

 

#include<iostream>

#include<string.h>

using namespace std;

int p[10000000];

int main( )

{

    int T,i;

 

    cin>>T;

    for(int Case=1; Case<=T; ++ Case)

    {

       cout<<"Case #"<<Case<<":"<<endl;

        int N, M,a, b;

        cin>>N>>M;

        memset( p,0,sizeof p);

        for( i=0;i<N;++i )

        {

           cin>>a>>b;

             for( int j=a; j<=b; ++j )

                p[j]++;

        }

 

        for( i=0;i<M;++i )

        {

            int x;

            cin>>x;

            cout<<p[x]<<endl;

        }

 

 

    }

    return 0;

}

 

 

 

2012-08-01 08:57:20 Time Limit Exceeded 4325 2000MS 39468K 579 B C++  

 

 下面是c写的代码。这个就AC了。希望哪位看到的大神指点指点。。。

View Code
#include <stdio.h>

#include <string.h>

int p[10000000];

int main( )

{

    int T,i;

    scanf( "%d", &T );

    for(int Case=1; Case<=T; ++ Case)

    {

        printf( "Case #%d:\n",Case );

        int N, M,a, b;

        scanf("%d%d", &N,&M );

        memset( p,0,sizeof p);

        for( i=0;i<N;++i )

        {

            scanf( "%d%d", &a, &b );

             for( int j=a; j<=b; ++j )

                p[j]++;

        }

 

        for( i=0;i<M;++i )

        {

            int x;

            scanf( "%d", &x );

 

            printf("%d\n", p[x] );

        }

 

 

    }

    return 0;

}

  

Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language  

 

  2012-08-01 08:57:50 Accepted 4325 1656MS 39432K 610 B C++  

 

 

 

posted on 2012-08-01 09:34  afterward  阅读(359)  评论(1编辑  收藏  举报