Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

能告诉我我怎么过了的么

Posted by lijijun12321 at 2014-07-30 20:40:32 on Problem 2528
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<string>
#include<string.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define nn 10005
#define INF 0x7FFFFFFF
typedef long long LL;

using namespace std;

int map[nn<<1][2];
bool flag[nn<<1];

struct TREE
{
    int l,r,col;
}tree[nn<<2];

struct SSS
{
    int id,pos;
}s[nn<<2];

void Build(LL L,LL R,LL root)
{
    tree[root].l=L;
    tree[root].r=R;
    tree[root].col=0;
    if(L<R)
    {
        LL M=(L+R)/2;
        Build(L,M,root<<1);
        Build(M+1,R,(root<<1)|1);
    }
}

void PushDown(int root)
{
    if(tree[root].col)
    {
        tree[root<<1].col=tree[(root<<1)|1].col=tree[root].col;
        tree[root].col=0;
    }
}

void UpDate(int a,int b,int root,int x)
{
    if(a<=tree[root].l&&tree[root].r<=b)
        tree[root].col=x;
    else
    {
        PushDown(root);
        int M=(tree[root].l+tree[root].r)/2;
        if(a<=M)
            UpDate(a,b,root<<1,x);
        if(b>M)
            UpDate(a,b,(root<<1)|1,x);
    }
}


int Query(int root)
{
    int res=0;
    if(tree[root].col)
    {
        if(!flag[tree[root].col])
        {
            res++;
            flag[tree[root].col]=1;
        }
        return res;
    }
    return Query(root<<1)+Query((root<<1)|1);
}

int cmp(SSS a,SSS b)
{
    return a.pos<b.pos;
}

int main()
{
    int n,t;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            int i;
            memset(flag,0,sizeof(flag));
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                scanf("%d%d",&map[i][0],&map[i][1]);
                s[i<<1].pos=map[i][0];
                s[(i<<1)|1].pos=map[i][1];
                s[i<<1].id=-(i+1);
                s[(i<<1)|1].id=i+1;
            }
            sort(s,s+2*n,cmp);
            int t=s[0].pos,cnt=1;
            for(i=0;i<2*n;i++)
            {
                if(t!=s[i].pos)
                {
                    t=s[i].pos;
                    cnt++;
                }
                if(s[i].id<0)
                    map[-s[i].id-1][0]=cnt;
                else
                    map[s[i].id-1][1]=cnt;
            }
            Build(1,cnt,1);
            for(i=0;i<n;i++)
                UpDate(map[i][0],map[i][1],1,i+1);
            printf("%d\n",Query(1));
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator