[BZOJ4636][扫描线+set]蒟蒻的数列

感觉用扫面线+set要比线段树好打一些

#include 
#include 
#include 
#include 
#define N 40010

using namespace std;

typedef long long ll;

multiset > S;

int n,cnt;
ll Ans;
struct stp{
  int x; ll k; int g;
  friend bool operator <(stp a,stp b){
    return a.x3];

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline void rea(int &x){
  char c=nc(); x=0; int f=1;
  for(;c>'9'||c<'0';c=nc())f=c=='-'?-f:f;
  for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); x*=f;
}

inline void rea(ll &x){
  char c=nc(); x=0; int f=1;
  for(;c>'9'||c<'0';c=nc())f=c=='-'?-f:f;
  for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); x*=f;
}

int main(){
  rea(n);
  for(int i=1;i<=n;i++){
    int l,r; ll k;
    rea(l); rea(r); rea(k);
    if(l>=r) continue;
    A[++cnt]=(stp){l,k,1}; A[++cnt]=(stp){r,k,0};
  }
  S.insert(0); 
  sort(A+1,A+1+cnt); int last=A[1].x;
  for(int i=1;i<=cnt;i++){
    Ans+=1ll*(A[i].x-last)*(*S.begin()); last=A[i].x;
    if(A[i].g) S.insert(A[i].k);
    else S.erase(S.find(A[i].k));
  }
  printf("%lld\n",Ans+(*S.begin()));
  return 0;
}

你可能感兴趣的:(STL,扫描线)