Advertisement
dudu2004

codeforces 1197E Culture Code

Jul 22nd, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int maxn=2e5+2;
  5. const int mod=1e9+7;
  6. const int INF=0x3f3f3f3f;
  7. int n;
  8. int f[maxn],g[maxn];
  9. struct Matryoshka{
  10.     int in,out;
  11.     Matryoshka(int in=0,int out=0):in(in),out(out){}
  12.     bool operator < (const Matryoshka &x)const{
  13.         return out<x.out;
  14.     }
  15. };
  16. Matryoshka doll[maxn];
  17. int trf[maxn],trg[maxn];
  18. struct Binary_Indexed_Tree{
  19.     int lowbit(int x){return x&-x;}
  20.     void update(int x,int f,int g){
  21.         while (x<=n){
  22.             if (trf[x]==f) trg[x]=(trg[x]+g)%mod;
  23.             if (trf[x]>f){
  24.                 trf[x]=f;
  25.                 trg[x]=g;
  26.             }
  27.             x+=lowbit(x);
  28.         }
  29.     }
  30.     void query(int x,int &f,int &g){
  31.         f=INF,g=0;
  32.         while (x){
  33.             if (f==trf[x]) g=(g+trg[x])%mod;
  34.             if (f>trf[x]){
  35.                 f=trf[x];
  36.                 g=trg[x];
  37.             }
  38.             x-=lowbit(x);
  39.         }
  40.     }
  41. };
  42. Binary_Indexed_Tree BIT;
  43. int main(){
  44.     scanf("%d",&n);
  45.     for (int i=1;i<=n;i++){
  46.         scanf("%d%d",&doll[i].out,&doll[i].in);
  47.         trf[i]=INF;
  48.     }
  49.     sort(doll+1,doll+n+1);
  50.     int Maxpos=0;
  51.     for (int i=1;i<=n;i++){
  52.         int pos=upper_bound(doll+1,doll+i+1,Matryoshka(0,doll[i].in))-doll-1;
  53.         Maxpos=max(Maxpos,pos);
  54.         if (!pos) BIT.update(i,f[i]=doll[i].in-doll[i].out,g[i]=1);
  55.         else{
  56.             BIT.query(pos,f[i],g[i]);
  57.             f[i]+=doll[i].in-doll[i].out;
  58.             BIT.update(i,f[i],g[i]);
  59.         }
  60.     }
  61.     int ansf=INF,ansg=0;
  62.     for (int i=Maxpos+1;i<=n;i++){
  63.         int tmp=f[i]+doll[i].out;
  64.         if (ansf==tmp) ansg=(ansg+g[i])%mod;
  65.         if (ansf>tmp){
  66.             ansf=tmp;
  67.             ansg=g[i];
  68.         }
  69.     }
  70.     printf("%d",ansg);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement