Guest User

ACTIV

a guest
Aug 27th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl '\n'
  3. #define INF 999999999
  4.  
  5. typedef long long int ll;
  6.  
  7. using namespace std;
  8.  
  9. int n;
  10. vector <pair<int ,int> > interval;
  11. vector <pair<int ,int> > interval2;
  12. bool dp[100001];
  13. ll dpp[100001];
  14. const ll mod=100000000;
  15.  
  16. bool sortBySec(const pair<int,int>a, const pair<int,int> b){
  17.   return a.second<b.second;
  18. }
  19.  
  20. int next(int i){//returns the first interval which has its startTime >= EndTime of the ith interval
  21.   auto it = lower_bound(interval2.begin(), interval2.end(), make_pair(interval[i].second,0));
  22.   if(it==interval2.end())return n+1;
  23.   pair<int,int> m=*it;//cout << m.first << ' ' << i << ' ';
  24.  
  25.   int lo=0, hi=n-1, mid;
  26.   while(lo<=hi){
  27.     mid=lo+(hi-lo)/2;
  28.     if(interval[mid]==m){
  29.         break;
  30.     }
  31.     if(interval[mid].second>m.second){
  32.         hi=mid-1;
  33.     }else{
  34.         lo=mid+1;
  35.     }
  36.   }
  37.   return mid;
  38. }
  39.  
  40. ll DP(int i){//int i represents ith interval the intervals sorted by their endtime
  41.   if(i==n-1)return 1;
  42.   if(i>=n)return 0;//handles when next(i) returns n+1
  43.   if(dp[i])return dpp[i];
  44.   dp[i]=1;
  45.   dpp[i] = (DP(next(i))%mod+(1 + DP(i+1))%mod)%mod;
  46.   return dpp[i];
  47. }
  48. void makeFalse(){
  49.   for(int i=0; i<n; i++){
  50.     dp[i]=dpp[i]=0;
  51.     }
  52. }
  53. int main(){
  54.   ios_base::sync_with_stdio(false);cin.tie(NULL);
  55.   int s,e;
  56.   ll ans;
  57.   while(cin >> n && n!=-1){
  58.     interval.clear();interval2.clear();
  59.     for(int i=0; i<n; i++){
  60.       cin >> s >> e;
  61.       interval.push_back({s,e});
  62.       interval2.push_back({s,e});
  63.     }
  64.     sort(interval.begin(), interval.end(), sortBySec);
  65.     sort(interval2.begin(),interval2.end());
  66.     makeFalse();
  67.     ans = DP(0);
  68.     int res = 1 + floor(log10(ans));
  69.     for(int i=0; i<8-res; i++)cout << 0;
  70.     cout << ans << endl;
  71.   }
  72. }
Add Comment
Please, Sign In to add comment