题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1208
代码(STL<set>):
#include <iostream>
#include <set>
using namespace std;
#define inf 0x7fffffff
set<int>bst;
int n,x,y,ans=0,st;
int main() {
bst.insert(-inf),bst.insert(inf);
cin>>n;
while (n--) {
cin>>x>>y;
if (bst.size()==2) bst.insert(y),st=x
; else if (st!=x) {
int r=*bst.lower_bound(y),l=*(--bst.lower_bound(y));
if (y-l<=r-y&&l>-inf) ans+=y-l,bst.erase(l)
; else ans+=r-y,bst.erase(r);
ans%=1000000;
} else bst.insert(y);
}
cout<<ans<<endl;
return 0;
}