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

贴个线段树,有点丑,7200ms,普通线段树就可以过,不必优化

Posted by 986677841 at 2016-05-26 11:36:18 on Problem 2823
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=1111111;
struct node
{
    int minn,maxn;
    int l,r;
}seg[M*8];
int f[M];
void build(int u,int l,int r)
{
    seg[u].minn=inf,seg[u].maxn=0;
    seg[u].l=l;
    seg[u].r=r;
    if(l==r){
        f[l]=u;
        return ;
            }
    int mid=(l+r)>>1;
    build(u*2,l,mid);
    build(u*2+1,mid+1,r);
}
int maxn[M],minn[M];
void up(int u,int val)
{
    if(u==1)return ;
    int dad=u>>1;
    seg[dad].maxn=max(seg[dad*2].maxn,seg[dad*2+1].maxn);
    seg[dad].minn=min(seg[dad*2].minn,seg[dad*2+1].minn);
    up(dad,val);
}

void Q(int u,int l,int r,int i)
{
    if(l<=seg[u].l&&r>=seg[u].r){
        maxn[i]=max(maxn[i],seg[u].maxn);
        minn[i]=min(minn[i],seg[u].minn);
                                }
    else 
    {
        int mid=(seg[u].l+seg[u].r)/2;
        if(r<=mid)Q(u*2,l,r,i);
        else if(l>mid)Q(u*2+1,l,r,i);
        else 
            {
                Q(u*2,l,mid,i);
                Q(u*2+1,mid+1,r,i);
            }
    }
}
int main()
{
    int x;
    int n,k;
    cin>>n>>k;
    build(1,1,n);
    for(int i=0;i<n;i++)
    {
        minn[i]=inf;
        maxn[i]=-inf;
    }
    for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            seg[f[i]].minn=seg[f[i]].maxn=x;
            up(f[i],x);
        }
    for(int i=k;i<=n;i++)
        Q(1,i-k+1,i,i-k);
    for(int i=0;i<=n-k;i++)
        printf("%d ",minn[i]);
    printf("\n");
    for(int i=0;i<=n-k;i++)
        printf("%d ",maxn[i]);
system("pause");
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