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 wocha at 2012-08-13 20:59:52 on Problem 3667
#include <cstdio>
#include <iostream>
#include <algorithm>
#define lson i<<1
#define rson i<<1|1
#define M 50000+33
#define max(a,b,c) (a>b?a:b)>c?(a>b?a:b):c
using namespace std;
struct no {
 int left;
 int right;
 int flag;
 int ls;
 int rs;
 int maxs;
}tree[M<<2];
inline int num(int i){
 return (tree[i].right-tree[i].left+1);
}
void build (int l,int r,int i){
 tree[i].left=l;
 tree[i].right=r;
 tree[i].flag=0;
 tree[i].rs=tree[i].ls=tree[i].maxs=num(i);
 if(l==r)  return ;
 int mid=(l+r)>>1;
 build (l,mid,lson);
 build (mid+1,r,rson);
}
inline void pushup(int i){
    if(tree[lson].ls==num(lson)&&tree[rson].ls==num(rson)){
       tree[i].rs=tree[i].maxs=tree[i].ls=num(i);
    }
    else if(tree[lson].ls==num(lson)&&tree[rson].ls!=num(rson)){
       tree[i].ls=tree[lson].ls+tree[rson].ls;
       tree[i].maxs=max(tree[i].ls,tree[rson].maxs,-1);
       tree[i].rs=tree[rson].rs; 
    }
    else if(tree[lson].ls!=num(lson)&&tree[rson].ls==num(rson)){
       tree[i].rs=tree[lson].rs+tree[rson].rs;
       tree[i].maxs=max(tree[i].rs,tree[lson].maxs,-1);
       tree[i].ls=tree[lson].ls;
    }
    else {
       tree[i].rs=tree[rson].rs;
       tree[i].ls=tree[lson].ls;
       tree[i].maxs=max(tree[lson].maxs,tree[rson].maxs,tree[lson].rs+tree[rson].ls);
    }
    
}
inline void pushdown(int i){
    if(tree[i].maxs==0){
     tree[lson].rs=tree[lson].maxs=tree[lson].ls=0;
     tree[rson].rs=tree[rson].maxs=tree[rson].ls=0;
    }
    if(tree[i].maxs==num(i)){
     tree[lson].rs=tree[lson].maxs=tree[lson].ls=num(lson);
     tree[rson].rs=tree[rson].maxs=tree[rson].ls=num(rson);
    }
   
 
}
void insert (int l,int r,int i,int val){
 if(tree[i].left>=l&&tree[i].right<=r){
  if(val==1){
  
   tree[i].ls=tree[i].rs=tree[i].maxs=0;
  }else {
 
   tree[i].rs=tree[i].maxs=tree[i].ls=num(i);
  }
  return ;
 }
 pushdown(i);
 int mid=(tree[i].left+tree[i].right)>>1;
 if(r<=mid)    insert(l,r,lson,val);
 else if(l>mid)insert(l,r,rson,val);
 else          insert(l,mid,lson,val),
               insert(mid+1,r,rson,val);
   pushup(i);
}

int query(int root,int lenth) 
{ 
    if(tree[root].left == tree[root].right) 
    { 
        if(tree[root].ls == 1 && lenth == 1) 
            return tree[root].left; 
        return 0; 
    } 
    
    pushdown(root); 
    if(tree[root<<1].maxs >= lenth) 
        return query(root<<1,lenth); 
    if(tree[root<<1].rs + tree[root<<1|1].ls >= lenth) 
        return tree[root<<1].right - tree[root<<1].rs + 1; 
    if(tree[root<<1|1].maxs >= lenth) 
        return query(root<<1|1,lenth); 
    return 0; 
} 

int main(){
 int n,m,c,a,b;;
 while(~scanf("%d%d",&n,&m)){
  build(1,n,1);
  while(m--){
  
   scanf("%d",&c);
   if(c==1){
    scanf("%d",&a);int g=query(1,a);
	printf("%d\n",g);
    if(g){
    	insert(g,g+a-1,1,1);
     }
   }else {
        scanf("%d%d",&a,&b);
		insert(a,b+a-1,1,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