I am getting tle
#include <bits/stdc++.h>
#include<utility>
#include<map>
typedef int ll;
typedef unsigned long long ull;
typedef std::vector<long long > vi ;
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
ll pow1(ll n,ll p)
{
if(p==0) return 1;
ll x=pow1(n, p/2);
x=(x*x)%mod;
if(p%2==0)
return x;
else
return (x*n)%mod;
}
ll binarySearch(ll arr[], ll l, ll r, ll x)
{
if (r >= l) {
ll mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
ll bs_lower_bound(ll a[], ll n, ll x) {
ll l = 0;
ll h = n; // Not n - 1
while (l < h) {
int mid = (l + h) / 2;
if (x <= a[mid]) {
h = mid;
} else {
l = mid + 1;
}
}
return l;
}
using namespace std;
int main()
{
ll n,i,j,k;
while(1)
{
cin>>n;
if(n==-1)
break;
pair<ll,ll>s[n];
ll dp[n]={0},x[n],y[n],p;
for(i=0;i<n;i++)
{
std::cin >> s[i].F>>s[i].S;
}
sort(s,s+n);
for(i=0;i<n;i++)
{
x[i]=s[i].first;
y[i]=s[i].second;
}
dp[n-1]=0;
ll sum=0;
for(i=n-2;i>=0;i--)
{
p=bs_lower_bound(x,n,y[i]);
if(p<n&&x[p]>=y[i])
{
for(j=p;j<n;j++)
{
dp[i]+=dp[j]+1;
}
}
}
for(i=0;i<n;i++)
sum+=dp[i];
sum+=n;
if(sum/100000000==0)
{
if(sum/10000000==0&&sum/1000000!=0)
std::cout <<"0"<< sum << std::endl;
else if(sum/1000000==0&&sum/100000!=0)
std::cout <<"00"<< sum << std::endl;
else if(sum/100000==0&&sum/10000!=0)
std::cout <<"000"<< sum << std::endl;
else if(sum/10000==0&&sum/1000!=0)
std::cout <<"0000"<< sum << std::endl;
else if(sum/1000==0&&sum/100!=0)
std::cout <<"00000"<< sum << std::endl;
else if(sum/100==0&&sum/10!=0)
std::cout <<"000000"<< sum << std::endl;
else if(sum/10==0&&sum/1!=0)
std::cout <<"0000000"<< sum << std::endl;
}
else
std::cout << sum%100000000 << std::endl;
}
}