Submission #16914767


Source Code Expand

//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word

#include <bits/stdc++.h> 
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007ll //998244353ll
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second 
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)    
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }
int par[N],siz[N];

int findset(int a)
{
    if(par[a]==a)
    return a;
    return par[a]=findset(par[a]);
}
void unionset(int a, int b)
{
    a=findset(a);
    b=findset(b);
    if(a==b)
    return;
    if(siz[a]>siz[b])
    swap(a, b);
    par[a]=b;
    siz[b]+=siz[a];
}

int32_t main()
{
    IOS;
    int n;
    cin>>n;
    vector <pair<pii, int> > v(n);
    for(int i=0;i<n;i++)
    {
        siz[i]=1;
        par[i]=i;
    }
    for(int i=0;i<n;i++)
    {
        cin>>v[i].ff.ff>>v[i].ff.ss;
        v[i].ss=i;
    }
    sort(all(v));
    stack <pii> s;
    for(int i=0;i<n;i++)
    {
        int x=v[i].ff.ff, y=v[i].ff.ss, ind=v[i].ss;
        //cout<<x<<" "<<y<<" "<<ind<<"\n";
        int z=y;
        while(!s.empty() && s.top().ff<y)
        {
            z=min(z, s.top().ff);
            unionset(ind, s.top().ss);
            s.pop();
        }
        s.push({z, ind});
    }
    for(int i=0;i<n;i++)
    cout<<siz[findset(i)]<<"\n";
}

Submission Info

Submission Time
Task A - Reachable Towns
User yash_daga
Language C++ (GCC 9.2.1)
Score 300
Code Size 2711 Byte
Status AC
Exec Time 82 ms
Memory 11108 KiB

Compile Error

./Main.cpp: In function ‘int32_t main()’:
./Main.cpp:93:13: warning: unused variable ‘x’ [-Wunused-variable]
   93 |         int x=v[i].ff.ff, y=v[i].ff.ss, ind=v[i].ss;
      |             ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 24
Set Name Test Cases
Sample example_00, example_01
All example_00, example_01, manyperm_00, manyperm_01, manyperm_02, manyperm_03, max_random_00, max_random_01, random_00, random_01, small_00, small_01, small_02, small_03, small_04, small_05, small_06, small_07, small_08, small_09, special1_00, special1_01, special1_02, special1_03
Case Name Status Exec Time Memory
example_00 AC 7 ms 3624 KiB
example_01 AC 2 ms 3620 KiB
manyperm_00 AC 82 ms 11028 KiB
manyperm_01 AC 77 ms 10956 KiB
manyperm_02 AC 79 ms 11036 KiB
manyperm_03 AC 79 ms 11028 KiB
max_random_00 AC 78 ms 11108 KiB
max_random_01 AC 80 ms 11024 KiB
random_00 AC 54 ms 8180 KiB
random_01 AC 61 ms 8912 KiB
small_00 AC 5 ms 3464 KiB
small_01 AC 3 ms 3628 KiB
small_02 AC 2 ms 3652 KiB
small_03 AC 2 ms 3520 KiB
small_04 AC 1 ms 3580 KiB
small_05 AC 2 ms 3516 KiB
small_06 AC 2 ms 3632 KiB
small_07 AC 3 ms 3628 KiB
small_08 AC 2 ms 3520 KiB
small_09 AC 2 ms 3564 KiB
special1_00 AC 54 ms 10268 KiB
special1_01 AC 62 ms 8996 KiB
special1_02 AC 30 ms 5284 KiB
special1_03 AC 66 ms 9636 KiB