652C - Foe Pairs - CodeForces Solution


combinatorics sortings two pointers *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
/*----------------------------------------------------------------------------------------------*/
#define xx cout<<"I'm Here\n";
#define int long long
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define prntv(arr) for(auto &a:arr) cout<<a<<" ";cout<<"\n";
#define prntp(arr) for(auto &a:arr){cout<<a.first<<" "<<a.second; cout<<"\n";}
#define prnt2d(v)  for(auto &arr:v){ for(auto &n:arr) cout<<n<<" "; cout<<"\n";}
#define pn(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout<<"\n";}template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) {cout << *it << " = " << a <<" ";err(++it, args...);}
template <typename T1, typename T2> istream &operator>>(istream &istream, pair<T1, T2> &p){return (istream >> p.first >> p.second);}template <typename T> istream &operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}
template <typename T1, typename T2> ostream &operator<<(ostream &ostream, const pair<T1, T2> &p){return (ostream << p.first << " " << p.second);}template <typename T> ostream &operator<<(ostream &ostream, const vector<T> &c){for(auto &it : c)cout << it << " ";return ostream;}
template <typename T> void prnt(T &&t){cout<<t<<"\n";} template <typename T, typename... Args> void prnt(T &&t, Args &&...args){cout << t << " ";prnt(forward<Args>(args)...);}
struct custom_hash{static uint64_t splitmix64(uint64_t x){x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}size_t operator()(uint64_t x) const{static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
constexpr long long N = 1e7+10;
constexpr long long INF = 4e18;
constexpr long double EPS = 1e-9;
constexpr long long MOD = 998244353; // 1e9 + 7;
/*----------------------------------------------------------------------------------------------*/

void solve(int tt){
  int n,m;cin>>n>>m;
  vi v(n);cin>>v;

  map<int,int> mp;
  map<int,int> pos;
  for(int i=0;i<n;i++){
    pos[v[i]]=i;
    mp[i]=n;
  }

  //suppose each i to be L, then store nearest R
  while(m--){
    int a,b;cin>>a>>b;
    int x=pos[a],y=pos[b];
    if(x>y) swap(x,y);
    mp[x]=min(mp[x],y);
  }

//   prntp(mp);


  int ans=0;
  int r=n; //to maintain ek L ke liye knha tk ja skta h R, kisi piche wale L ko apne aage wali L ke R se aage nhi jana h 
  //count for each L, valid R (R-L-1+1);
  for(int i=n-1;i>=0;i--){
    r=min(r,mp[i]); 
    ans+=r-i;
  }
  prnt(ans);
}

int32_t main(){
  ios_base::sync_with_stdio(false);cin.tie(NULL);
  //cout<<fixed<<setprecision(9);
 
  int tt=1;
//   cin>>tt;
  for(int i=1;i<=tt;i++)
  solve(i);
  
  return 0;
}


Comments

Submit
0 Comments
More Questions

275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome