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 2013-03-26 21:13:46 on Problem 1631
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int const M = 40043;
int a[M];
struct node {
  int left,right ,val ;
}tree[M<<2];
void build(int l ,int r,int i){
  tree[i].left = l;
  tree[i].right = r;
  tree[i].val = 0;
  if(l== r) return ;
  int mid = (l+r ) >>1 ;
  build (l,mid,i<<1);
  build (mid +1,r,(i<<1)+1);

}
void update(int l,int r,int i,int key){
 if(tree[i].left == l&& tree[i].right ==r ){
   tree [i].val = key;
   return ;
 }
 int mid = (tree[i].left+tree[i].right) >>1;
 if(r<=mid ) update(l,r,i<<1,key);
 else if(l>mid) update(l,r,(i<<1)+1,key);
 else update(l,mid,i<<1,key),update(mid+1,r,(i<<1)+1,key);
 tree[i].val = max(tree[i<<1].val,tree[(i<<1)+1].val);

}
int query(int l ,int r,int i){
 if(tree[i].left == l && tree[i].right==r) {
  return tree[i].val;
 }
  int mid = (tree[i].left+tree[i].right) >>1;
  if(r<=mid)  return query(l,r,i<<1);
  else if(l>mid) return query(l,r,(i<<1)+1);
  else
  return max(query(l,mid,i<<1),query(mid+1,r,(i<<1)+1));
}
int dp [M];
int main(){
 int t ;
 cin>>t ;
 while(t--){
  int n,p=-12;
  cin>>n;
  for(int i=1;i<=n;i++)
  scanf("%d",&a[i]),a[i]++,
  p = max(p,a[i]);
 build(1,p,1);
 for(int i=1;i<=n;++i){
   if(i>1){
     update(a[i-1],a[i-1],1,dp[i-1]);
   }
   dp[i] = query(1,a[i]-1,1)+1;
 }
 int u =-9;
 for(int i=1;i<=n;i++){
 u = max(u,dp[i]);
 }
 printf("%d\n",u);
 }
}

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