topic
Tree
http://www.gdfzoj.com/oj/contest/248/problems/1
analysis
- Wow, although this question is placed on the first question of improving group B, it is stunned.
- After listening to ZW talk about this question for half a minute, I suddenly realized...
- This is the search tree drawn by the sample (sorted in numerical order in the horizontal direction)
- Simulate it by yourself and you will find that after adding a new number, find the two left and right points (using the horizontal coordinate as the standard), then its depth is the next layer with the larger left and right depth.
- Use a set to record node information (value and depth), find the left and right nodes each time, just operate it, just to practice STL.
- See the program for details on the boundary.
program
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
typedef pair<int,int> pr;
int n,x,k;
long long ans;
set <pr> S;
set <pr>::iterator it;
pr l,r;
int main(){
scanf("%d",&n);
while (scanf("%d",&x)!=EOF){
it=S.lower_bound((pr){x,0});
if (it!=S.end()) r=*it;
else r=(pr){-1,-1};
if (it==S.begin()) l=(pr){-1,-1};
else {it--; l=*it;}
k=max(l.second,r.second)+1;
S.insert((pr){x,k});
printf("%lld\n",ans+=k);
}
}
prompt
- This question is worth accumulating...