SPOJ: BGSHOOT ( SegmentTree-DS)

SPOJ: BGSHOOT ( Segment Tree-DS )

///==================================================/// /// HELLO WORLD !! /// /// IT'S ME /// /// BISHAL GAUTAM /// /// [ bsal.gautam16@gmail.com ] /// ///==================================================/// #include<bits/stdc++.h> #define X first #define Y second #define mpp make_pair #define nl printf("\n") #define SZ(x) (int)(x.size()) #define pb(x) push_back(x) #define pii pair<int,int> #define pll pair<ll,ll> ///--------------------- #define S(a) scanf("%d",&a) #define P(a) printf("%d",a) #define SL(a) scanf("%lld",&a) #define S2(a,b) scanf("%d%d",&a,&b) #define SL2(a,b) scanf("%lld%lld",&a,&b) ///------------------------------------ #define all(v) v.begin(),v.end() #define CLR(a) memset(a,0,sizeof(a)) #define SET(a) memset(a,-1,sizeof(a)) #define fr(i,a,n) for(int i=a;i<=n;i++) #define UNIK(v) sort(all(v)),v.resize( unique(all(v)) -v.begin() ); using namespace std; typedef long long ll; ///==========CONSTANTS=============/// /// Digit 0123456789012345678 /// #define MX 200005 #define inf 1000000010 #define MD 1000000000LL #define eps 1e-9 ///===============================/// int xx[MX+2],qx[MX+2],yy[MX+2],qy[MX+2]; int lim,ar[2*MX+2],dp[2*MX+2][22]; void Rmq( ) { for(int i=1; i<=lim; i++)dp[i][0]=ar[i]; for(int j=1; (1<<j)<=lim; j++) { for(int i=1; i+(1<<j)-1<=lim; i++) { dp[i][j]=(dp[i][j-1]>dp[i+(1<<(j-1))][j-1])?dp[i][j-1]:dp[i+(1<<(j-1))][j-1]; } } } int Qry(int l,int r) { int k=log2(r-l+1); return (dp[l][k]>dp[r-(1<<k)+1][k])?dp[l][k]:dp[r-(1<<k)+1][k]; } vector<int>v; int main() { int tc,cs=1,i,j,k,n,m,q,x,y; S(n); fr(i,1,n) { S2(xx[i],yy[i]); v.pb( xx[i] ); v.pb( yy[i] ); } S(m); fr(i,1,m) { S2(qx[i],qy[i]); v.pb( qx[i] ); v.pb( qy[i] ); } UNIK(v); lim=0; fr(i,1,n) { x=lower_bound( all(v),xx[i] )-v.begin()+1; y=lower_bound( all(v),yy[i] )-v.begin()+1; ar[x]++; ar[y+1]--; lim=max(lim,y+2); } fr(i,1,lim) { ar[i]+=ar[i-1]; } Rmq( ); ll sm=0LL; fr(i,1,m) { x=lower_bound( all(v),qx[i] )-v.begin()+1; y=lower_bound( all(v),qy[i] )-v.begin()+1; int mx=Qry(x,y); printf("%d\n",mx); } return 0; }

Comments

  1. ar eta naki segment+compression kore solve kora jay ! kibhabe compression?

    ReplyDelete
  2. You can replace RMQ part of this solution with segment tree ,which will cost LogN for each query ,wheres RMQ cost O(1). For compression, Here I pushed each input on a vector-v, then unify vector. After that, compressed each value to 1-based index of on sorted vector.

    ReplyDelete

Post a Comment

Popular posts from this blog

UVA 11019 - Matrix Matcher( 2D String matching, Rolling Hash )

UVA-12062 - Reverse Assignment

HackerRank: Poisonous Plants ( Stacks, DS )