text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ",";
os << *it;
}
return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
cerr << arg << ' ';
faltu(rest...);
}
const int mx = 3e5 + 5;
int n, m;
int p[mx];
set<int> st[mx];
set<pair<int, int> > unvisited;
int cnt;
int rev[mx];
void dfs(int u, int idx) {
++cnt;
unvisited.erase(make_pair(idx, u));
int cur = 0;
int hmm = idx;
while (1) {
auto itr = unvisited.upper_bound(make_pair(hmm, -1));
if (itr == unvisited.begin()) return;
--itr;
int v = (*itr).second;
cur = v;
hmm = (*itr).first;
if (st[u].lower_bound(v) != st[u].upper_bound(v)) continue;
dfs(v, (*itr).first);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> p[i], rev[p[i]] = i;
for (int i = 1; i <= n; ++i) unvisited.insert(make_pair(i, p[i]));
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
st[v].insert(u);
}
dfs(p[n], n);
cout << n - cnt << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> a, was;
vector<vector<int> > g;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
a.resize(n);
g.resize(n);
was.resize(n);
for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
for (int i = 0; i < m; i++) {
int w1, w2;
cin >> w1 >> w2;
w1--;
w2--;
g[w1].push_back(w2);
}
reverse(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < n; i++) was[i] = 0;
was[a[0]] = 1;
int cnt = 1;
for (int i = 1; i < n; i++) {
int cnt2 = 0;
for (int to : g[a[i]]) {
if (was[to]) cnt2++;
}
if (cnt == cnt2) {
ans++;
} else {
was[a[i]] = 1;
cnt++;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T& a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T& a, T b) {
return a < b ? (a = b, true) : false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> cnt(N + 1);
vector<int> loc(N + 1);
vector<int> g[N + 1];
for (int i = 1; i <= N; ++i) {
int p;
cin >> p;
loc[p] = i;
}
for (int i = 1; i <= M; ++i) {
int a, b;
cin >> a >> b;
a = loc[a];
b = loc[b];
if (b > a) {
g[b].push_back(a);
++cnt[a];
}
}
int goal = N;
for (int i = N - 1; i >= 1; --i) {
if (cnt[i] + i == goal) {
--goal;
for (int j : g[i]) --cnt[j];
}
}
cout << N - goal << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 7;
const int MAXV = 207;
const int MAXE = 207;
const int INF = 0x3f3f3f3f;
int n, m;
int a[MAXN];
int idx[MAXN], cnt[MAXN];
int lst = -1;
bool vis[MAXN];
vector<int> v, g[MAXN];
bool cmp(int x, int y) { return idx[x] > idx[y]; }
int main() {
while (cin >> n >> m) {
for (int i = 0; i <= n; i++) g[i].clear();
v.clear();
memset(cnt, 0, sizeof(cnt));
;
memset(vis, 0, sizeof(vis));
;
for (int i = 1; i <= n; i++) {
cin >> a[i];
idx[a[i]] = i;
}
lst = a[n];
for (int i = 0; i < m; i++) {
int frt, bak;
cin >> frt >> bak;
if (bak == lst) {
v.push_back(frt);
vis[frt] = true;
}
g[frt].push_back(bak);
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < g[i].size(); j++) {
int now = g[i][j];
if (idx[i] < idx[now]) cnt[i]++;
}
}
sort(v.begin(), v.end(), cmp);
int ans = 0, pass = 0;
for (int i = 0; i < v.size(); i++) {
int now = v[i];
if (cnt[now] >= (n - idx[now] - pass)) {
ans++;
pass++;
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
const int inf = 2e9;
using namespace std;
long long MOD = 1000000007;
typedef struct {
int a, b, c;
} iii;
int a, b, n, q, t, k, m, res, suma, minimo, ans = 0, maxi, indi;
map<pair<int, int>, bool> M;
vector<int> ad;
int pos[300005], fila[300005];
bool ord(int a, int b) { return pos[b] < pos[a]; }
int main() {
int inicial, nay;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
fila[i] = a;
pos[a] = i;
if (i == n) {
inicial = i;
nay = a;
}
}
for (int i = 0; i < m; ++i) {
scanf("%d%d", &a, &b);
if (b == nay) {
M[{a, b}] = true;
ad.push_back(a);
} else
M[{a, b}] = true;
}
sort(ad.begin(), ad.end(), ord);
for (auto x : ad) {
while (pos[x] + 1 <= n && M[{x, fila[pos[x] + 1]}]) {
int px = pos[x], ps = pos[x] + 1, s = fila[ps];
pos[x] = ps;
pos[s] = px;
fila[ps] = x;
fila[px] = s;
}
}
printf("%d\n", inicial - pos[nay]);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
struct Edge {
int u, v;
};
int arr[MAX];
int arrpos[MAX];
int number[MAX];
map<pair<int, int>, int> mp;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
n = read();
m = read();
for (int i = 1; i <= n; ++i) {
arr[i] = read();
arrpos[arr[i]] = i;
}
int ans = 0;
for (int i = 0, u, v; i < m; ++i) {
u = read();
v = read();
if (!mp[make_pair(u, v)]) {
mp[make_pair(u, v)] = 1;
if (arrpos[u] < arrpos[v])
++number[u];
else
++number[v];
}
}
for (int i = n - 1; i >= 1; --i) {
if (!mp[make_pair(arr[i], arr[n])]) continue;
if (number[arr[i]] >= n - ans - i) ++ans;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[310000];
set<int> sw[310000];
vector<int> block;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
sw[u].insert(v);
}
int cur = 0;
for (int i = n - 2; i >= 0; i--) {
bool ok = sw[p[i]].count(p[n - 1]);
if (ok) {
for (int k : block) {
if (!sw[p[i]].count(k)) {
ok = false;
break;
}
}
}
if (!ok)
block.push_back(p[i]);
else
cur++;
}
cout << n - 1 - (int)block.size();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, i, j;
cin >> n >> m;
vector<long long int> v;
for (i = 0; i < n; i++) {
long long int y;
cin >> y;
v.push_back(y);
}
vector<set<long long int> > vv(n + 1);
for (i = 0; i < m; i++) {
long long int u, w;
cin >> u >> w;
vv[u].insert(w);
}
long long int last = n - 1;
for (i = n - 2; i >= 0; i--) {
if (vv[v[i]].find(v[last]) == vv[v[i]].end()) continue;
long long int xx = v[i];
for (j = i; j < last; j++) {
if (vv[v[j]].find(v[j + 1]) != vv[v[j]].end()) {
swap(v[j], v[j + 1]);
} else
break;
}
if (xx == v[last]) {
last--;
}
}
cout << n - last - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
void func() {
int n, m;
cin >> n >> m;
int arr[n + 1];
for (int i = 1; i <= (n); i++) cin >> arr[i];
unordered_map<int, unordered_set<int>> mm, vis;
int a, b;
for (int i = 1; i <= (m); i++) {
cin >> a >> b;
mm[a].insert(b);
}
int pos = n;
while (1) {
for (int i = pos; i >= 1;) {
if (mm[arr[i - 1]].count(arr[i])) {
if (vis[arr[i]].count(arr[i - 1])) {
i--;
continue;
}
vis[arr[i - 1]].insert(arr[i]);
swap(arr[i - 1], arr[i]);
i++;
if (i > pos) {
pos--;
break;
}
continue;
}
i--;
if (i == 0) {
cout << n - pos;
return;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
func();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300001;
vector<int> g[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i];
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
g[u].push_back(v);
}
map<int, int> need;
need[p[n - 1]]++;
int ans = 0, size = 1;
for (int i = n - 2; i > -1; i--) {
int cnt = 0;
for (auto it : g[p[i]]) {
if (need[it]) cnt++;
}
if (cnt == size)
ans++;
else
need[p[i]]++, size++;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
using namespace std;
long long pows(long long a, long long n, long long m) {
a = a % 1000000007;
long long res = 1;
while (n) {
if (n % 2 != 0) {
res = (res * a) % m;
n--;
} else {
a = (a * a) % m;
n = n / 2;
}
}
return res % m;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
bool isprime(long long n) {
if (n == 1) {
return false;
}
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool istrue(string s) {
int i = 0;
int j = s.size() - 1;
while (i < j) {
if (s[i] == s[j]) {
i++;
j--;
} else {
return false;
}
}
return true;
}
int n, m;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
vector<int> ar[n + 1];
vector<int> a(n);
vector<int> good(n + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
ar[x].push_back(y);
if (y == a[n - 1]) {
good[x] = true;
}
}
int ans = 0;
set<int> s;
for (int i = n - 2; i >= 0; i--) {
if (good[a[i]] == false) {
s.insert(a[i]);
} else {
int cnt = 0;
for (int val : ar[a[i]]) {
if (s.count(val)) {
cnt++;
}
}
if (cnt == s.size()) {
ans++;
} else {
s.insert(a[i]);
}
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 501234;
int n, m, ans;
vector<int> graph[maxN];
int vals[maxN];
map<int, int> isInP;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> vals[i];
vals[i]--;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
}
ans = 0;
isInP[vals[n - 1]] = 1;
int count = 1;
for (int i = n - 2; i >= 0; i--) {
int thisCount = 0;
for (int j = 0; j < graph[vals[i]].size(); j++) {
if (isInP[graph[vals[i]][j]]) thisCount++;
}
if (thisCount == count)
ans++;
else {
isInP[vals[i]] = 1;
count++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n, m;
int p[maxn];
vector<int> g[maxn];
set<int> st, tmp;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
}
for (auto x : g[p[n]]) st.insert(x);
int ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (st.empty()) break;
if (st.find(p[i]) != st.end())
ans++;
else {
tmp.clear();
for (auto x : st) tmp.insert(x);
st.clear();
for (auto x : g[p[i]])
if (tmp.find(x) != tmp.end()) st.insert(x);
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
std ::chrono ::high_resolution_clock::time_point t1 =
std ::chrono ::high_resolution_clock ::now();
double dur_() {
std ::chrono ::high_resolution_clock::time_point t2 =
std ::chrono ::high_resolution_clock ::now();
std ::chrono ::duration<double> time_span =
std ::chrono ::duration_cast<std ::chrono ::duration<double>>(t2 - t1);
return time_span.count();
}
using namespace std;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T1>
ostream &operator<<(ostream &out, const vector<T1> &v) {
for (auto it : v) out << it << ' ';
return out;
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void cinv(vector<T> &v, size_t n, istream &in) {
for (size_t i = 0; i < n; i++) {
T x;
in >> x;
v.push_back(x);
}
}
inline void read(long long &x) {
char ch;
while (1) {
ch = getchar();
if (ch > '9' | ch < '0') return;
x *= 10;
x = x + ch - '0';
}
}
inline void out(long long x) {
char buff[20];
size_t sizel = 0;
for (; x > 0; x /= 10, sizel++) buff[sizel] = x % 10 + '0';
for (long long i = sizel - 1; i >= 0; i--) putchar(buff[i]);
return;
}
struct cmp {
bool f = 1;
template <class T>
inline bool operator()(T x, T y) {
return x < y ? f : !f;
}
inline void setcmp(bool x) { f = x; }
};
const long long N = 3 * 1e5 + 1;
const long long M = 1e3 + 1;
const long long inf = INFINITY;
using namespace std;
long long n, m;
long long a[N];
vector<long long> cur;
signed main() {
ios_base ::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
cur.push_back(a[n - 1]);
map<pair<long long, long long>, char> mp;
for (long long i = 0; i < m; ++i) {
long long x, y;
cin >> x >> y;
mp[{x, y}] = 1;
}
bool f = 0;
for (long long i = n - 2; i >= 0; --i) {
f = 0;
long long pos = cur.size();
for (long long j = 0; j < pos; ++j) {
if (mp[{a[i], cur[j]}] == 0) {
f = 1;
break;
}
}
if (f == true) cur.push_back(a[i]);
}
cout << n - cur.size();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7, M = 2e6 + 7, INF = INT_MAX / 10;
long long powe(long long x, long long y) {
x = x % mod, y = y % (mod - 1);
long long ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
void solve() {
long long n, m, i, j, c = 0;
cin >> n >> m;
long long a[n];
for (i = 0; i < n; i++) cin >> a[i];
vector<long long> adj[n + 1];
vector<bool> vis(n + 1, false);
for (i = 0; i < m; i++) {
long long x, b;
cin >> x >> b;
adj[x].push_back(b);
if (b == a[n - 1]) {
vis[x] = true;
}
}
set<long long> s;
s.insert(a[n - 1]);
for (i = n - 2; i >= 0; i--) {
bool ch = true;
if (!vis[a[i]])
s.insert(a[i]);
else {
long long k = 0;
for (j = 0; j < adj[a[i]].size(); j++) {
if (s.find(adj[a[i]][j]) != s.end()) {
k++;
}
}
if (k == s.size())
c++;
else
s.insert(a[i]);
}
}
cout << c << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, i, j, x, u, v;
cin >> n >> m;
long long a[n + 1];
vector<long long> adj[n + 1];
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
}
map<long long, long long> mp;
mp[a[n]] = 1;
long long left = 1, ans = 0;
for (i = n - 1; i > 0; i--) {
long long cnt = 0;
for (auto j : adj[a[i]]) {
cnt += mp[j];
}
if (cnt == left)
ans++;
else {
left++;
mp[a[i]] = 1;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long mo = 1e9 + 7;
long long sx, sy, ex, ey, dx[6] = {0, 1, 0, -1, 0, 0},
dy[6] = {1, 0, -1, 0, 0, 0}, m, n, k,
dz[6]{0, 0, 0, 0, -1, 1};
long long p, sg, no, v, re, ans, w, moo;
int par[500005];
long long b[400005];
long long a[400006], c[500006], dp[400005];
struct mat {
long long a[40][40];
};
mat init, unit;
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
set<long long> se;
long long qu(long long a, long long b, long long m) {
long long ans = 1;
while (b) {
if (b & 1) {
ans = ans % m * a % m;
}
b >>= 1;
a = a % m * a % m;
}
return ans;
}
int su(int n) {
if (n == 1 || n == 0) return 0;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return 0;
}
return 1;
}
int fi(int a) {
if (a == par[a])
return a;
else
return par[a] = fi(par[a]);
}
vector<long long> ve, ve1;
string s1;
map<long long, long long> mp, mp1;
long long vis[500005];
int nu = 1, id = 1;
priority_queue<long long> que;
pair<long long, long long> a1[400000];
long long vis1[405][405], vis2[406][6];
int maze[505][505], maze1[505][505];
vector<long long> g[400000], g1[400000];
string s;
int cmp(pair<long long, long long> a, pair<long long, long long> b) {
return (a.first - a.second) > (b.first - b.second);
}
int main() {
int t, p2, p3;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string ss, sss;
long long l, r, n1;
while (cin >> n >> m) {
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
cin >> p >> w;
g[p].push_back(w);
}
vis[a[n - 1]] = 1;
sg = 1;
ans = 0;
for (int i = n - 2; i >= 0; i--) {
int ct = 0;
for (auto it : g[a[i]]) {
if (vis[it]) ct++;
}
if (ct == sg)
ans++, vis[a[i]] = 0;
else {
vis[a[i]] = 1;
sg++;
}
}
cout << ans << endl;
for (int i = 1; i <= n; i++) vis[i] = 0, g[i].clear();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
int a[n + 4];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
set<int> s[n + 5];
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
s[x].insert(y);
}
vector<int> v;
v.push_back(a[n - 1]);
for (int i = n - 2; i >= 0; i--) {
bool flag = true;
for (int j = 0; j < v.size(); j++) {
if (s[a[i]].find(v[j]) == s[a[i]].end()) {
flag = false;
break;
}
}
if (!flag) v.push_back(a[i]);
}
cout << n - v.size() << endl;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma warning(disable : 4996)
using namespace std;
mt19937 rnd(time(nullptr));
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long n, m;
cin >> n >> m;
vector<long long> p(n);
map<long long, long long> smth;
for (long long i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
smth[p[i]] = i;
}
vector<vector<long long> > graph(n);
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
graph[b].push_back(a);
}
vector<pair<long long, long long> > all;
vector<long long> need = graph[p.back()];
for (long long i = 0; i < need.size(); i++) {
all.push_back({smth[need[i]], need[i]});
}
sort(all.begin(), all.end());
reverse(all.begin(), all.end());
long long last = n - 1;
long long bad = 0;
vector<long long> used(n + 7);
long long ans = 0;
for (long long i = 0; i < all.size(); i++) {
for (long long j = last - 1; j > all[i].first; j--) {
for (auto to : graph[p[j]]) {
used[to]++;
}
}
long long now = all[i].first;
long long el = all[i].second;
if (used[el] == (n - 1) - now - i - 1 + bad)
ans++;
else {
for (auto to : graph[el]) {
used[to]++;
}
bad++;
}
last = all[i].first;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
vector<set<int>> q;
int v;
bool _s(int ind) {
bool was = false;
while (!q.empty() && ind > 0) {
auto it = q[a[ind]].find(a[ind - 1]);
if (it != q[a[ind]].end()) {
q[a[ind]].erase(it);
q[a[ind - 1]].erase(a[ind]);
swap(a[ind], a[ind - 1]);
ind--;
was = true;
if (v != a[ind]) return true;
} else {
if (!_s(ind - 1)) return was;
}
}
return was;
}
void Solve() {
int n, m;
cin >> n >> m;
a = vector<int>(n);
for (signed i = 0; i < (n); i++) cin >> a[i];
q = vector<set<int>>(n + 1);
v = a.back();
for (signed i = 0; i < (m); i++) {
int u, v;
cin >> u >> v;
q[v].insert(u);
}
_s(n - 1);
cout << n - (find((a).begin(), (a).end(), v) - a.begin()) - 1;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int p[300010];
set<int> vertices[300010], save;
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
vertices[u].insert(v);
}
save.insert(p[n]);
for (int i = n - 1; i >= 1; i--) {
int flag = 1;
for (auto v : save)
if (vertices[p[i]].find(v) == vertices[p[i]].end()) {
flag = 0;
break;
}
if (!flag) save.insert(p[i]);
}
cout << n - (int)save.size();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 3e18 + 5;
int add(int a, int b) { return (a += b) < mod ? a : a - mod; }
int mul(int a, int b) { return 1LL * a * b % mod; }
const int mxn = 3e5 + 5;
int a[mxn], cnt[mxn];
vector<int> g[mxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
}
int res = 0;
for (int i = n - 1; i >= 0; i--) {
for (int u : g[a[i]]) cnt[u]++;
if (i != n - 1) {
if (cnt[a[i]] >= n - 1 - i - res) {
for (int u : g[a[i]]) cnt[u]--;
res++;
}
}
}
cout << res;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m;
int mas[300300];
vector<int> edge[300300];
vector<int> nes;
int tim[300300];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> mas[i];
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
}
int t = 1e8;
tim[mas[n - 1]] = t;
int nes = 1;
int ans = 0;
for (int i = n - 2; i >= 0; i--) {
int kol = 0;
t--;
for (int j = 0; j < edge[mas[i]].size(); j++) {
if (tim[edge[mas[i]][j]] == 0) continue;
kol++;
}
if (kol == nes) {
ans++;
continue;
}
tim[mas[i]] = t;
nes++;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300001;
vector<int> g[maxn];
int need[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i];
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
g[u].push_back(v);
}
need[p[n - 1]]++;
int ans = 0, size = 1;
for (int i = n - 2; i > -1; i--) {
int cnt = 0;
for (auto it : g[p[i]]) {
if (need[it]) cnt++;
}
if (cnt == size)
ans++;
else
need[p[i]]++, size++;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
int change[MAX], p[MAX];
vector<int> adj[MAX];
int N, M, a, b;
int main(int argc, char** argv) {
scanf("%d%d", &N, &M);
for (int i = (1); i <= (N); i++) scanf("%d", &p[i]);
for (int k = (0); k < (M); k++) {
scanf("%d%d", &a, &b);
adj[b].push_back(a);
}
int cnt = 0, sz = 1;
for (int i = (0); i < (adj[p[N]].size()); i++) change[adj[p[N]][i]]++;
for (int i = (N - 1); i >= (1); i--) {
if (change[p[i]] == sz)
cnt++;
else {
sz++;
for (int j = (0); j < (adj[p[i]].size()); j++) change[adj[p[i]][j]]++;
}
}
printf("%d\n", cnt);
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
set<long long> g[300003];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n >> m;
long long p[n + 1], i, j;
for (i = 1; i <= n; ++i) cin >> p[i];
long long x, y;
for (i = 1; i <= m; ++i) {
cin >> x >> y;
g[x].insert(y);
}
list<long long> v;
v.clear();
long long ans = 0;
for (i = n - 1; i >= 1; --i) {
if (g[p[i]].find(p[n]) == g[p[i]].end()) {
v.push_front(p[i]);
continue;
} else {
long long f = 0;
for (auto it : v) {
if (g[p[i]].find(it) == g[p[i]].end()) {
f = 1;
break;
}
}
if (f == 1)
v.push_front(p[i]);
else
++ans;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[500001];
int num[300005];
int main() {
int n, m, uu, vv, ans = 0;
scanf("%d%d", &n, &m);
int a[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &uu, &vv);
v[vv].push_back(uu);
}
for (int i = 0; i < v[a[n]].size(); i++) num[v[a[n]][i]]++;
for (int i = n - 1; i >= 1; i--) {
if (num[a[i]] == n - i - ans)
ans++;
else {
for (int j = 0; j < v[a[i]].size(); j++) {
num[v[a[i]][j]]++;
}
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 3e5 + 9;
set<int> g[N], gr[N];
int a[N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int x = a[n];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].insert(v);
gr[v].insert(u);
}
int cur = n;
for (int i = n - 1; i > 0; i--) {
bool can = 1;
int u = a[i];
for (int j = i; j < cur; j++) {
if (g[u].find(a[j + 1]) == g[u].end()) {
can = 0;
break;
} else {
swap(a[j], a[j + 1]);
}
}
if (can) cur--;
}
cout << n - cur << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int bj[N];
vector<int> e[N];
int n, m;
int arr[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 1, x, y; i <= m; i++) {
scanf("%d%d", &x, &y);
e[x].push_back(y);
}
bj[arr[n]] = 1;
int ans = 1;
for (int i = n - 1; i >= 1; i--) {
int flag = 0;
for (int v : e[arr[i]]) {
if (bj[v]) flag++;
}
if (flag == ans) {
bj[arr[i]] = 0;
} else {
bj[arr[i]] = 1;
ans++;
}
}
cout << n - ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = (int)1e5 + 10;
int main(int argc, char* argv[]) {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i]--;
}
set<int> g[n];
vector<int> canPass(n);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].insert(y);
if (y == a[n - 1]) canPass[x] = true;
}
int res = 0;
set<int> mustByPass;
for (int i = n - 2; i >= 0; --i) {
if (canPass[a[i]]) {
bool ok = g[a[i]].size() >= mustByPass.size();
for (auto& x : mustByPass) ok &= g[a[i]].find(x) != g[a[i]].end();
res += ok;
if (!ok) mustByPass.insert(a[i]);
} else
mustByPass.insert(a[i]);
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int ai[300005];
int bi[300005];
int id[300005];
vector<int> vc[300005];
bool mrk[300005];
map<pair<int, int>, int> ma;
int taken[300005];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &ai[i]);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
if (v == ai[n]) {
mrk[u] = 1;
}
ma[{u, v}] = 1;
}
int ans = 0;
n--;
while (mrk[ai[n]] && n > 0) n--, ans++;
for (int i = n; i >= 1; i--) {
if (mrk[ai[i]]) {
int ok = 1;
for (int j = i + 1; j <= n; j++)
if (!taken[j] && !ma[{ai[i], ai[j]}]) ok = 0, j = n + 1;
if (ok) {
taken[i] = 1;
ans++;
}
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> r;
set<int> d[300005];
vector<int> in;
int main() {
int n, m;
cin >> n >> m;
in.resize(n);
for (int i = 0; i < n; i++) {
cin >> in[i];
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
d[u].insert(v);
}
int ret = 0;
r.insert(in.back());
for (int i = n - 2; i >= 0; i--) {
bool swappable = false;
if (d[in[i]].size() >= r.size()) {
swappable = true;
for (int x : r) {
if (d[in[i]].find(x) == d[in[i]].end()) {
swappable = false;
break;
}
}
}
if (swappable) {
ret++;
} else {
r.insert(in[i]);
}
}
cout << ret << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int sz = 3e5 + 5;
int n, m, q[sz] = {0};
vector<int> sw[sz];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> q[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
sw[u].push_back(v);
}
int steps = 0, tot = 0;
bool vis[sz] = {0};
vis[q[n - 1]] = 1;
tot = 1;
for (int i = n - 2; i >= 0; i--) {
int u = q[i], cnt = 0;
for (int j = 0; j < sw[u].size(); j++)
if (vis[sw[u][j]]) cnt++;
if (cnt == tot)
steps++;
else {
vis[u] = 1;
tot++;
}
}
cout << steps << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long mul(long long x, long long y, long long m) {
long long z = 1LL * x * y;
if (z >= m) z %= m;
return z;
}
inline long long add(long long x, long long y, long long m) {
long long z = x + y;
if (z >= m) z %= m;
return z;
}
inline long long sub(long long x, long long y, long long m) {
long long z = x - y;
if (z < 0) z += m;
z %= m;
return z;
}
vector<long long> adj[300050];
long long a[300050], n, m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long u, v;
cin >> n >> m;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < m; i++) cin >> u >> v, adj[v].push_back(u);
set<long long> s;
for (long long i = 0; i < adj[a[n - 1]].size(); i++)
s.insert(adj[a[n - 1]][i]);
long long cp = n - 2;
long long ans = 0;
while (cp >= 0) {
if (s.empty()) break;
long long k = a[cp];
cp--;
if (s.find(k) != s.end()) {
ans++;
continue;
}
set<long long> s1;
for (long long i = 0; i < adj[k].size(); i++) {
long long x = adj[k][i];
if (s.find(x) != s.end()) s1.insert(x);
}
s = s1;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int c[300005], q[300005];
set<int> s[300005];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
c[a] = i;
q[i] = i;
}
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a = c[a];
b = c[b];
s[a].insert(b);
}
int ans = 0;
for (int i = n - 2; i >= 0; i--) {
if (s[i].find(n - 1) == s[i].end()) continue;
for (int j = i + 1; j < n - ans; j++) {
if (s[i].find(q[j]) == s[i].end()) break;
swap(q[j - 1], q[j]);
}
if (q[n - ans - 1] != n - 1) ans++;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T pow(T a, T b, long long int m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans % m;
}
template <typename T>
void swap(T *a, T *b) {
T temp = *a;
*a = *b;
*b = temp;
return;
}
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);
}
};
unordered_map<long long, long long int, custom_hash> safe_map;
const long long int N = 1e6;
std::vector<long long int> h[N];
void solve() {
long long int n, m;
cin >> n >> m;
vector<long long int> stack;
long long int p[n];
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n)))
cin >> p[i];
for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
i += 1 - 2 * ((0) > (m))) {
long long int u, v;
cin >> u >> v;
h[u].push_back(v);
}
reverse(p, p + n);
stack.push_back(p[0]);
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
long long int val = p[i];
bool f = true;
if (h[val].size() < stack.size()) {
stack.push_back(val);
continue;
}
long long int count = 0;
for (auto i : h[val]) {
if (find(stack.begin(), stack.end(), i) != stack.end()) {
count++;
}
}
if (count < stack.size()) {
stack.push_back(val);
}
}
cout << n - stack.size() << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int p = 1000000001;
int main() {
int n, m;
cin >> n >> m;
vector<int> data;
vector<set<int>> chage(n + 1);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
data.push_back(x);
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
chage[x].insert(y);
}
set<int> kans;
kans.insert(data[data.size() - 1]);
for (int i = data.size() - 2; i >= 0; i--) {
if (kans == chage[data[i]]) {
continue;
}
int temp = 0;
for (const int &el : chage[data[i]]) {
if (kans.find(el) != kans.end()) {
temp++;
}
}
if (temp != kans.size()) {
kans.insert(data[i]);
}
}
cout << n - kans.size() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int MAXN = 300010;
vector<int> adj[MAXN];
int arr[MAXN];
bool toMove[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < (n); ++i) cin >> arr[i];
int v1, v2;
for (int i = 0; i < (m); ++i) {
cin >> v1 >> v2;
adj[v1].push_back(v2);
}
toMove[arr[n - 1]] = true;
int cnt = 1, tmp;
for (int i = (n - 2); i >= (0); --i) {
tmp = 0;
for (int ch : adj[arr[i]])
if (toMove[ch]) ++tmp;
if (tmp != cnt) {
++cnt;
toMove[arr[i]] = true;
}
}
cout << n - cnt << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 4 * 1e18;
const int INFi = 2 * 1e9;
long long mod = 1e9 + 7;
long double eps = 1e-8;
vector<int> p;
vector<int> pmax;
vector<vector<int>> edge;
bool cmp(int x, int y) { return p[x] < p[y]; }
bool cmp2(int x, int y) { return pmax[x] > pmax[y]; }
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
p.resize(n);
edge.resize(n);
pmax.resize(n);
int t;
vector<int> h(n);
int c = 0;
for (int i = 0; i < n; i++) {
cin >> t;
t--;
p[t] = i + 1;
pmax[t] = i + 1;
h[i] = t;
if (i == (n - 1)) c = t;
}
int t1, t2;
set<int> f;
for (int i = 0; i < m; i++) {
cin >> t1 >> t2;
t1--;
t2--;
edge[t1].push_back(t2);
if (t2 == c) f.insert(t1);
}
int cur = n;
int cnt = 1;
for (int i = n - 2; i >= 0; i--) {
int t = h[i];
sort(edge[t].begin(), edge[t].end(), cmp);
for (int j : edge[t]) {
if (p[j] < p[t]) continue;
if (p[j] <= (pmax[t] + cnt)) pmax[t]++;
}
if (f.find(t) != f.end() && pmax[t] >= (cur - 1)) {
cnt++;
cur--;
}
}
cout << n - cur << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void InputSetup() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void solve() {
int n, m;
cin >> n >> m;
int p[n];
for (int i = 0; i < n; ++i) {
cin >> p[i];
--p[i];
}
vector<vector<int>> move(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
move[--u].push_back(--v);
}
set<int> youShallNotPass;
for (int i = n - 2; i >= 0; --i) {
int ct = 0;
for (auto x : move[p[i]]) {
++ct;
if (x == p[n - 1]) continue;
if (youShallNotPass.find(x) != youShallNotPass.end()) continue;
--ct;
}
if ((int)youShallNotPass.size() + 1 == ct) continue;
youShallNotPass.insert(p[i]);
}
cout << n - 1 - youShallNotPass.size();
}
int main(void) {
auto start = chrono::high_resolution_clock::now();
InputSetup();
solve();
auto finish = chrono::high_resolution_clock::now();
cerr << "Time elapsed: "
<< (chrono::duration<long double>(finish - start)).count() << "s\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = int(3e5) + 10;
const int K = int(2e6) + 10;
const int MOD = int(1e9) + 7;
const int INF = int(1e9) + 5;
const long long INF64 = 1e18;
int p[N];
vector<int> adj[N];
set<int> s;
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
int ret = 0;
s.insert(p[n]);
for (int i = n - 1; i > 0; i--) {
int cnt = 0;
for (auto x : adj[p[i]]) {
if (s.find(x) != s.end()) cnt++;
}
if (int((s).size()) == cnt) {
ret++;
} else {
s.insert(p[i]);
}
}
cout << ret << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct con {
int left;
int right;
};
con connections[300005];
vector<int> q;
vector<int> candidates;
int pos[300005];
int n, m, v, a, b, cont;
bool candidates_sort(int a, int b) { return pos[a] > pos[b]; }
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &v);
q.push_back(v);
pos[v] = i;
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
if (b == q[q.size() - 1]) {
candidates.push_back(a);
}
if (pos[b] > pos[a])
connections[a].right++;
else
connections[a].left++;
}
sort(candidates.begin(), candidates.end(), candidates_sort);
for (int c : candidates) {
if (connections[c].right >= n - pos[c] - 1 - cont) {
cont++;
}
}
printf("%d\n", cont);
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
long long n, m, a[300005];
vector<long long> v[300005];
map<long long, long long> viz;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long i = 1; i <= m; i++) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
}
long long cnt = 1, ans = 0;
viz[a[n]] = 1;
for (long long i = n - 1; i >= 1; i--) {
long long x = 0;
for (auto it : v[a[i]]) {
if (viz[it]) {
x++;
}
}
if (x < cnt) {
viz[a[i]] = 1;
cnt++;
} else
ans++;
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n, i, j, m, k, l, h, g, ye, a, b, rez;
int p[300005], f[300005], ct[300005];
int v[300005], nv;
bool comp(int x, int y) { return f[x] < f[y]; }
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++) cin >> p[i], f[p[i]] = i;
ye = p[n];
for (i = 1; i <= m; i++) {
cin >> a >> b;
if (f[a] < f[b]) ct[a]++;
if (b == p[n]) v[++nv] = a;
}
sort(v + 1, v + nv + 1, comp);
for (i = nv; i >= 1; i--) {
if (ct[v[i]] >= (n - f[v[i]]) - rez) rez++;
}
cout << rez << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int _max = 3e5 + 10;
int p[_max];
vector<int> G[_max];
vector<bool> vis(_max);
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", p + i);
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
}
vis[p[n - 1]] = 1;
int ans = 0, cnt = 1;
for (int i = n - 2; i >= 0; i--) {
int cnt2 = 0;
vector<int>& viG = G[p[i]];
for (int j = 0; j < viG.size(); j++)
if (vis[viG[j]]) cnt2++;
if (cnt2 == cnt)
ans++;
else {
vis[p[i]] = 1;
cnt++;
}
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mxn = 3e5 + 5;
const int mxm = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-7;
const double pii = 3.1415926535898;
int gcd(int x, int y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
int qpow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
int lowbit(int x) { return (x & (-x)); }
int qmul(int a, int b) {
int ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % mod;
b >>= 1;
a = (a + a) % mod;
}
return ans;
}
int pos[300005];
vector<int> g[mxn];
int cnt[mxn], fan[mxn];
signed main(void) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &pos[i]);
fan[pos[i]] = i;
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[v].push_back(u);
}
for (int i = 0; i < g[pos[n]].size(); i++) {
int v = g[pos[n]][i];
cnt[fan[v]]++;
}
int now = n;
for (int i = n - 1; i >= 1; i--) {
if (cnt[i] == now - i) {
now--;
} else {
for (int j = 0; j < g[pos[i]].size(); j++) {
int v = g[pos[i]][j];
cnt[fan[v]]++;
}
}
}
printf("%d\n", n - now);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, u, v;
vector<int> p;
set<pair<int, int> > s;
int main() {
std::ios_base::sync_with_stdio(false);
cin >> n >> m;
p = vector<int>(n);
for (int i = 0; i < n; i++) cin >> p[i];
int pn = p[n - 1];
for (int i = 0; i < m; i++) {
cin >> u >> v;
s.insert(make_pair(u, v));
}
int i = n - 2;
while (i >= 0) {
auto it = s.find(make_pair(p[i], p[i + 1]));
if (it != s.end()) {
swap(p[i], p[i + 1]);
s.erase(it);
if (p[i] == pn)
i--;
else
i++;
} else {
i--;
}
}
for (int i = 0; i < n; i++)
if (p[i] == pn) {
cout << (n - i - 1) << endl;
break;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6;
set<long long> g[N];
long long a[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, lol, kol = 0;
cin >> n >> m;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
lol = a[n - 1];
for (long long i = 0; i < m; i++) {
long long l, r;
cin >> l >> r;
g[r].insert(l);
}
long long kek = n - 1;
for (long long i = n - 2; i >= 0; i--) {
for (long long j = i + 1; j <= kek; j++) {
if (g[a[j]].find(a[j - 1]) == g[a[j]].end()) {
break;
} else {
swap(a[j - 1], a[j]);
if (a[j - 1] == lol) {
kek = j - 1;
break;
}
}
}
}
for (long long i = 0; i < n; i++) {
if (a[i] == lol) {
cout << n - i - 1;
return 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> edge[300001];
int p[300001];
set<int> now;
bool check(int x) {
int cnt = 0;
for (int it : edge[x])
if (now.find(it) != now.end()) cnt++;
return cnt == now.size();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1, x, y; i <= m; i++) {
cin >> x >> y;
edge[x].emplace_back(y);
}
now.insert(p[n]);
int ans = 0;
for (int i = n - 1; i >= 1; i--)
if (check(p[i]))
ans++;
else
now.insert(p[i]);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300001;
vector<int> g[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i];
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
g[u].push_back(v);
}
set<int> need;
need.insert(p[n - 1]);
int ans = 0;
for (int i = n - 2; i > -1; i--) {
int cnt = 0;
for (auto it : g[p[i]]) {
if (need.count(it)) cnt++;
}
if (cnt == need.size())
ans++;
else
need.insert(p[i]);
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> adj;
int a[300005], rnk[300005];
int main() {
int n, m, k, x, y;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
rnk[a[i]] = i;
}
k = a[n];
for (int i = 1; i <= m; i++) {
cin >> x >> y;
if (rnk[x] < rnk[y]) adj[make_pair(x, y)] = 1;
}
int rp = n;
for (int i = n - 1; i >= 1; i--) {
int j = i;
while (j < rp && adj[make_pair(a[j], a[j + 1])]) {
swap(a[j], a[j + 1]);
j++;
}
if (j == rp) rp--;
}
cout << n - rp << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
return a * a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
return a > b ? a : b;
}
template <class T>
T aabs(T a) {
return a < 0 ? -a : a;
}
template <class T>
bool dcmp(T a, T b) {
return a > b;
}
template <int *a>
bool cmp_a(bool first, bool second) {
return a[first] < a[second];
}
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char first) {
*oS++ = first;
if (oS == oT) flush();
}
inline bool read(signed &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(long long &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(char &first) {
first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
return first != EOF;
}
inline bool read(char *first) {
while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == '\n' ||
*first == ' ' || *first == '\r')
if (*first == EOF) return 0;
while (!(*first == '\n' || *first == ' ' || *first == '\r'))
*(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
*first = 0;
return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
return read(first) && read(second...);
}
inline bool write(signed first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(long long first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(char first) {
putc(first);
return 0;
}
inline bool write(const char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
inline bool write(char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
return write(first) || write(second...);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
set<pair<int, int> > ok;
int p[300005];
vector<int> qwq;
signed main() {
int n, m, u, v;
read(n, m);
for (int i = 1; i <= n; ++i) {
read(p[i]);
}
for (int i = 1; i <= m; ++i) {
read(u, v);
ok.insert(make_pair(u, v));
}
qwq.push_back(p[n]);
int s = 0;
for (int i = n - 1; i; --i) {
bool c = 1;
for (auto j : qwq) {
if (!ok.count(make_pair(p[i], j))) {
c = 0;
break;
}
}
s += c;
if (!c) qwq.push_back(p[i]);
}
write(s, '\n');
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int ans;
int a[400050];
int back[400050];
vector<int> edge[400050];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
edge[v].push_back(u);
}
for (auto iter : edge[a[n]]) back[iter]++;
for (int i = n - 1; i >= 1; i--) {
if (back[a[i]] == n - i - ans)
ans++;
else {
for (auto iter : edge[a[i]]) back[iter]++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma disable : 4996)
using namespace std;
const int inf = 0x7f7f7f7f;
const long long mod = 1e9 + 7;
int a[510000], cnt[510000];
vector<int> g[510000];
void add(int u) {
for (auto v : g[u]) cnt[v]++;
}
int main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
}
int ans = 0;
add(a[n]);
for (int i = n - 1; i >= 1; i--) {
if (cnt[a[i]] == n - i - ans)
ans++;
else
add(a[i]);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t N>
int SIZE(const T (&t)[N]) {
return N;
}
template <typename T>
int SIZE(const T(&t)) {
return t.size();
}
string to_string(char b) { return "'" + string({b}) + "'"; }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(const string &t, int x1 = 0, int x2 = 1e9) {
string ret = "";
for (int i = min(x1, SIZE(t)), _i = min(x2, SIZE(t) - 1); i <= _i; ++i) {
ret += t[i];
}
return '"' + ret + '"';
}
string to_string(const char *t) {
string ret(t);
return to_string(ret);
}
template <size_t N>
string to_string(const bitset<N> &t, int x1 = 0, int x2 = 1e9) {
string ret = "";
for (int i = min(x1, SIZE(t)), _i = min(x2, SIZE(t) - 1); i <= _i; ++i) {
ret += t[i] + '0';
}
return '"' + ret + '"';
}
template <typename T, typename... Coords>
string to_string(const T(&t), int x1 = 0, int x2 = 1e9, Coords... C);
template <typename T, typename S>
string to_string(const pair<T, S>(&t)) {
return "(" + to_string(t.first) + ", " + to_string(t.second) + ")";
}
template <typename T, typename... Coords>
string to_string(const T(&t), int x1, int x2, Coords... C) {
string ret = "[";
x1 = min(x1, SIZE(t));
auto e = begin(t);
advance(e, x1);
for (int i = x1, _i = min(x2, SIZE(t) - 1); i <= _i; ++i) {
ret += (i != x1 ? ", " : "") + to_string(*e, C...);
e = next(e);
}
return ret + "]";
}
void dbgm() { ; }
template <typename T, typename... S>
void dbgm(T t, S... s) {
cout << to_string(t) << " | ";
dbgm(s...);
}
void dbgs() { ; }
template <typename T, typename... S>
void dbgs(T t, S... s) {
cout << t << " ";
dbgs(s...);
}
const long long N = 3e5 + 3, M = 1e9 + 7;
int let[N], a[N], pos[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1, _i = n; i <= _i; ++i) {
cin >> a[i];
pos[a[i]] = i;
}
int start = a[n];
set<pair<int, int>> g;
int pt = 0;
for (int i = 1, _i = m; i <= _i; ++i) {
int u, v;
cin >> u >> v;
g.insert({u, v});
if (v == start) {
let[++pt] = u;
}
}
m = pt;
sort(let + 1, let + 1 + m,
[&](const int &l, const int &r) { return pos[l] > pos[r]; });
for (int i = 1, _i = m; i <= _i; ++i) {
for (int j = pos[let[i]], _i = pos[start] - 1; j <= _i; ++j) {
if (g.count(pair<int, int>{a[j], a[j + 1]})) {
pos[a[j]]++;
pos[a[j + 1]]--;
swap(a[j], a[j + 1]);
} else {
break;
}
}
}
dbgs(n - pos[start]);
}
|
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 5e5;
const int Inf = 1 << 30;
int mpx[MaxN + 5];
vector<int> s, t[MaxN + 5];
bool cmp(int x, int y) { return x > y; }
int main() {
int n, m, x, u, v, ans, S, p;
while (~scanf("%d %d", &n, &m)) {
ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
mpx[x] = i;
t[i].clear();
}
for (int i = 1; i <= m; i++) {
scanf("%d %d", &u, &v);
t[mpx[u]].push_back(mpx[v]);
}
for (int i = 1; i <= n; i++) sort(t[i].begin(), t[i].end(), cmp);
s.clear();
s.push_back(n);
p = 1;
for (int i = n - 1; i >= 1; i--) {
x = 0;
S = t[i].size();
for (int j = 0; j < S; j++) {
if (s[x] == t[i][j]) {
if (++x == p) {
ans++;
break;
}
} else if (s[x] > t[i][j])
break;
}
if (x != p) {
p++;
s.push_back(i);
}
}
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 3e5 + 4;
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
int a[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
vector<int> v[n + 1];
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
v[x].push_back(y);
}
vector<int> will_block(n + 1, 0);
int blocks = 1;
will_block[a[n]] = 1;
int ans = 0;
for (int i = n - 1; i > 0; i--) {
int can_skip = 0;
for (int it : v[a[i]])
if (will_block[it]) can_skip++;
if (can_skip == blocks)
ans++;
else {
will_block[a[i]] = 1;
blocks++;
}
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int x, y, s = 0, k = 1;
cin >> n >> m;
vector<vector<int>> ma(n + 1);
vector<int> a(n, 0), b(n + 1, 0), c(n + 1, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
c[a[i]] = i;
}
for (int i = 0; i < m; i++) {
cin >> x >> y;
ma[y].push_back(x);
if (c[x] < c[y]) b[x]++;
}
for (int i = n - 2; i >= 0; i--) {
if (b[a[i]] == k) {
s++;
for (int j = 0; j < ma[a[i]].size(); j++) b[ma[a[i]][j]]--;
} else
k++;
}
cout << s;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int arr[N];
vector<int> adj[N];
set<int> ss;
int ok[N];
int n, m;
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
if (y == arr[n - 1]) ok[x] = 1;
adj[x].push_back(y);
}
int ans = 0;
for (int i = n - 2; i >= 0; i--) {
if (ok[arr[i]]) {
int co = 0;
for (int j : adj[arr[i]]) {
if (ss.find(j) != ss.end()) co++;
}
if (co == ss.size())
ans++;
else
ss.insert(arr[i]);
} else
ss.insert(arr[i]);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
void solve() {
int n, m;
cin >> n >> m;
int p[n];
for (int i = 0; i <= n - 1; i++) cin >> p[i];
set<int> st[n + 1];
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
st[a].insert(b);
}
set<int> s;
vector<bool> flag(n + 1, false);
int ans = 0;
for (int i = n - 2; i >= 0; i--) {
if (st[p[i]].find(p[n - 1]) != st[p[i]].end()) {
int cnt = 0;
for (auto j : st[p[i]]) {
if (flag[j]) cnt++;
}
if (cnt == ((int)(s).size()))
ans++;
else {
flag[p[i]] = true;
s.insert(p[i]);
}
} else {
flag[p[i]] = true;
s.insert(p[i]);
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M, P[1000006], C[1000006], x, y, r;
vector<int> g[1000006];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++) cin >> P[i];
for (int i = 0; i < M; i++) {
cin >> x >> y;
g[y].push_back(x);
}
for (int i = N - 1; i >= 0; i--)
if (C[P[i]] == N - 1 - i - r && i != N - 1)
r++;
else
for (int f : g[P[i]]) C[f]++;
cout << r;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<int> g[N];
int cnt[N], og[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
og[a] = i;
}
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
x = og[x];
y = og[y];
g[y].push_back(x);
}
int ans = 0;
for (int i = n; i >= 1; i--) {
if (cnt[i] == n - i - ans && i < n) {
ans++;
} else {
for (auto& y : g[i]) {
cnt[y]++;
}
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
for (; b; a %= b, swap(a, b))
;
return a;
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
long long n, m;
cin >> n >> m;
vector<long long> a(n);
set<pair<long long, long long> > st;
for (long long i = 0; i < n; i++) cin >> a[i];
long long last = a[n - 1];
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
st.insert({x, y});
}
long long ans = 0;
for (long long i = n - 2; i >= 0; i--) {
bool flag = false;
long long j = i;
while (j <= n - 2 && st.count({a[j], a[j + 1]}) && !flag) {
if (a[j + 1] == last) flag = true;
swap(a[j], a[j + 1]);
j++;
}
if (flag) ans++;
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
void error(istream_iterator<string> _iterat) {}
template <typename T, typename... args>
void error(istream_iterator<string> _iterat, T a, args... arguments) {
cerr << *_iterat << "=" << a << "\n";
error(++_iterat, arguments...);
}
template <typename T, typename V>
void error(string pri, const pair<T, V> &z) {
cerr << pri << "="
<< "{" << z.first << "," << z.second << "}"
<< "\n";
}
template <typename T, typename V>
void error(string pri, const map<T, V> &z) {
cerr << pri << " is={";
for (auto it = z.begin(); it != z.end(); ++it) {
cerr << it->first << ":" << it->second;
auto _it = z.end();
_it--;
if (it != _it) cerr << ",";
}
cerr << "}";
cerr << "\n";
}
template <typename T>
void error(string pri, const vector<T> &z, long long st, long long ed) {
cerr << pri << " in range[" << st << "," << ed << ")=[";
for (long long i = st; i < ed; ++i) {
cerr << z[i];
if (i != ed - 1) cerr << " ";
}
cerr << "]";
cerr << "\n";
}
template <typename T>
void error(string pri, const set<T> &z, long long st, long long ed) {
cerr << pri << " in range[" << st << "," << ed << ")=[";
long long tempcount = 0;
for (auto it = z.begin(); it != z.end(); ++it) {
if (tempcount >= st and tempcount < ed) {
cerr << *it;
if (tempcount != ed - 1) cerr << " ";
}
tempcount++;
}
cerr << "]";
cerr << "\n";
}
template <typename T>
void error(string pri, const T z[], long long st, long long ed) {
cerr << pri << " in range[" << st << "," << ed << ")=[";
for (long long i = st; i < ed; ++i) {
cerr << z[i];
if (i != ed - 1) cerr << " ";
}
cerr << "]";
cerr << "\n";
}
void logic() {
int n, k;
cin >> n >> k;
int arr[n];
int count = 0;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
map<pair<int, int>, bool> m;
int u, v;
for (int i = 0; i < k; ++i) {
cin >> u >> v;
m[{u, v}] = 1;
}
set<int> s;
s.insert(arr[n - 1]);
for (int i = n - 2; i >= 0; --i) {
int temp = arr[i];
bool flag = 0;
for (auto it = s.begin(); it != s.end(); ++it) {
if (m[{temp, *it}] == 0) {
flag = 1;
break;
}
}
if (flag == 0)
count++;
else
s.insert(temp);
}
cout << count;
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long t;
{ logic(); }
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
int cnt[maxn];
vector<int> c[maxn];
int a[maxn];
int main() {
int n, m, u, v;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
c[v].push_back(u);
}
for (int i = 0; i < c[a[n]].size(); i++) cnt[c[a[n]][i]]++;
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
if (n - i - ans == cnt[a[i]])
ans++;
else {
for (int j = 0; j < c[a[i]].size(); j++) cnt[c[a[i]][j]]++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int const maxn = 400000;
int p[maxn];
bool is_good[maxn];
bool block[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, s;
cin >> n >> m;
vector<vector<int>> adj(n);
for (int i = 0; i < n; ++i) {
cin >> p[i];
p[i]--;
}
s = p[n - 1];
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
adj[u].push_back(v);
if (v == s) is_good[u] = true;
}
int ans = 0;
int bad = 0;
for (int i = n - 2; i >= 0; --i) {
int u = p[i];
if (!is_good[u]) {
bad++;
block[u] = true;
continue;
}
if (bad > adj[u].size()) {
bad++;
block[u] = true;
continue;
}
int cnt = 0;
for (int j = 0; j < adj[u].size(); ++j) {
cnt += block[adj[u][j]];
}
if (cnt == bad)
++ans;
else {
bad++;
block[u] = true;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
int n, m, cont[maxn];
set<int> g[maxn];
set<int> first;
set<int> can;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
while (cin >> n >> m) {
for (int i = 0; i < n; i++) cin >> cont[i];
for (int i = 1; i < n + 1; i++) g[i].clear();
can.clear();
int st = cont[n - 1];
for (int i = 0; i < m; i++) {
int add1, add2;
cin >> add1 >> add2;
if (add2 != st)
g[add2].insert(add1);
else
first.insert(add1);
}
if (n == 1) {
cout << 0 << endl;
exit(0);
}
int ans = 0;
for (int i = 1; i < n + 1; i++) {
if (i != st) {
can.insert(i);
}
}
for (int i = n - 2; i >= 0; i--) {
if (can.find(cont[i]) != can.end() &&
first.find(cont[i]) != first.end()) {
ans++;
} else {
set<int> era;
for (auto j : can) {
if (g[cont[i]].find(j) == g[cont[i]].end()) {
era.insert(j);
}
}
for (auto j : era) {
can.erase(j);
}
if (can.size() == 0) {
break;
}
}
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt = 1, p[300010], good[300010];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
vector<vector<int>> s(n);
for (int i = 0; i < n; ++i) cin >> p[i];
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
s[v - 1].push_back(u - 1);
}
for (int i = 0; i < s[p[n - 1] - 1].size(); ++i) ++good[s[p[n - 1] - 1][i]];
for (int i = n - 1 - 1; i >= 0; --i)
if (good[p[i] - 1] != cnt) {
for (int j = 0; j < s[p[i] - 1].size(); ++j) ++good[s[p[i] - 1][j]];
++cnt;
}
cout << n - cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int i, j, ft;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long int n, m;
cin >> n >> m;
long long int a[n];
for (i = 0; i < (n); i++) {
long long int b;
cin >> b;
a[i] = b - 1;
}
vector<long long int> v[n];
vector<long long int> number;
long long int num = a[n - 1];
for (i = 0; i < (m); i++) {
long long int b, c;
cin >> b >> c;
b--;
c--;
v[c].push_back(b);
if (b == num) number.push_back(c);
if (c == num) number.push_back(b);
}
vector<long long int> vv;
long long int sum = 0;
long long int allow = 1;
vector<long long int> allowed(n, 0);
for (auto it = v[a[n - 1]].begin(); it != v[a[n - 1]].end(); it++) {
allowed[*it]++;
}
for (long long int i = n - 2; i >= 0; i--) {
if (allowed[a[i]] != allow) {
for (auto it = v[a[i]].begin(); it != v[a[i]].end(); it++) {
allowed[*it]++;
}
allow++;
} else {
sum++;
}
}
cout << sum << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
set<int> can;
vector<int> p;
vector<int> g[MAXN];
int n, m, res;
signed main() {
cin >> n >> m;
p.resize(n);
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
}
for (int x : g[p[n - 1]]) {
can.insert(x);
}
for (int i = n - 2; i >= 0; --i) {
if (can.count(p[i]))
++res, can.erase(p[i]);
else {
vector<int> to;
for (int x : g[p[i]]) {
if (can.count(x)) to.push_back(x);
}
can.clear();
for (int x : to) {
can.insert(x);
}
}
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace std {
template <>
struct hash<pair<int, int>> {
size_t operator()(const pair<int, int> &x) const {
long long P = 38923, Q = 109797901;
return (size_t)((x.first * P + x.second) % Q);
}
};
}; // namespace std
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
void print(T t) {
cout << t << endl;
}
template <typename T, typename... Args>
void print(T t, Args... args) {
cout << t << " ";
print(args...);
}
const int MAXN = 300005;
int n, m, p[MAXN], u, v;
vector<set<int>> g(MAXN, set<int>());
set<int> blockers;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = min((int)0, (int)n); i < max((int)0, (int)n); i += 1) {
cin >> p[i];
p[i]--;
}
for (int i = min((int)0, (int)m); i < max((int)0, (int)m); i += 1) {
cin >> u >> v;
u--;
v--;
g[u].emplace(v);
}
blockers.emplace(p[n - 1]);
int c = 0;
for (int i = n - 1; i >= 0; i--) {
int u = p[i];
bool pos = true;
if ((int)(g[u]).size() < (int)(blockers).size()) pos = false;
if (pos) {
for (auto &v : blockers) {
if (g[u].find(v) == g[u].end()) {
pos = false;
break;
}
}
}
if (pos) {
c++;
} else {
blockers.emplace(u);
}
}
cout << c << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, a[300005], b1[500005], b2[500005],
cnt = 0, pl[300005], c[300005], ans = 0, plc[300005], m, d, can[300005], r,
ne[300005];
pair<int, int> p;
pair<int, int> p1;
pair<int, int> p2;
map<pair<int, int>, int> m1;
string s;
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pl[a[i]] = i;
can[i] = 0;
ne[i] = i + 1;
}
for (int i = 0; i < m; i++) {
cin >> b1[i] >> b2[i];
p.first = b1[i];
p.second = b2[i];
m1[p] = 1;
if (b2[i] == a[n]) {
c[cnt] = b1[i];
plc[cnt] = pl[b1[i]];
cnt++;
}
}
sort(plc, plc + cnt);
for (int i = 0; i < cnt; i++) {
c[i] = a[plc[i]];
}
for (int i = cnt - 1; i >= 0; i--) {
d = 1;
r = ne[pl[c[i]]];
while (r < n) {
p1.first = c[i];
p1.second = a[r];
if (m1[p1] == 0) {
d = 0;
break;
}
r = ne[r];
}
if (d) {
ans++;
if (c[i] > 0) {
ne[pl[c[i]] - 1] = ne[pl[c[i]]];
}
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
long long nMod = 1e9 + 7;
inline long long GCD(long long a, long long b) {
while (b != 0) {
long long c = a % b;
a = b;
b = c;
}
return a;
};
inline long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; };
int n, m;
vector<set<int> > back(300005), forw(300005);
vector<int> arr(300005);
int res = 0;
bool canMove(int pos) {
int dstPos = n - 1 - res;
for (int i = pos + 1; i <= dstPos; i++) {
if (*back[arr[pos]].lower_bound(arr[i]) == arr[i]) {
} else {
return false;
}
}
for (int i = pos; i < dstPos; i++) {
arr[pos] = arr[pos + 1];
}
return true;
}
void vietnakid() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
forw[v].insert(u);
back[u].insert(v);
}
for (int i = n - 2; i >= 0; i--) {
if (back[arr[i]].count(arr[n - 1])) {
int j = i;
while (j < n - 2 && back[arr[j]].count(arr[j + 1])) {
swap(arr[j], arr[j + 1]);
j++;
}
}
}
int i = n - 1;
while (i > 0 && forw[arr[i]].count(arr[i - 1])) {
res++;
swap(arr[i], arr[i - 1]);
i--;
}
cout << res << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
vietnakid();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 100;
int a[maxn];
map<pair<int, int>, bool> mp;
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
int M;
cin >> M;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = 1; i <= M; i++) {
int x;
int y;
cin >> x >> y;
mp[pair<int, int>(x, y)] = 1;
}
int pos = N;
for (int i = N - 1; i >= 1; i--) {
if (mp[pair<int, int>(a[i], a[pos])] == true) {
int j;
for (j = i; j < pos; j++) {
if (mp[pair<int, int>(a[j], a[j + 1])])
swap(a[j], a[j + 1]);
else
break;
}
if (j == pos) pos -= 1;
}
}
cout << N - pos;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
int a[maxn], num[maxn];
vector<int> v[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
v[y].push_back(x);
}
for (int i = 0; i < v[a[n]].size(); i++) {
num[v[a[n]][i]]++;
}
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
if (num[a[i]] == n - i - ans)
ans++;
else {
for (int j = 0; j < v[a[i]].size(); j++) {
num[v[a[i]][j]]++;
}
}
}
printf("%d", ans);
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long md = 1000 * 1000 * 1000 + 7;
long long pw(long long a, long long b) {
long long c = 1, m = a;
while (b) {
if (b & 1) c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
long long mul(long long a, long long b, long long c, long long d) {
return (((((a * b) % md) * c) % md) * d) % md;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int u, v, i, j, k, l, n, m, f;
cin >> n >> m;
int p[n + 1], inv[n + 1], is[n + 1];
for (i = 1; i < 1 + n; ++i) cin >> p[i], inv[p[i]] = i, is[i] = 0;
vector<int> adj[n + 1];
for (i = 0; i < m; ++i) {
cin >> u >> v;
adj[inv[u]].push_back(inv[v]);
if (inv[v] == n) is[inv[u]] = 1;
}
for (i = 1; i < n; ++i) sort(adj[i].begin(), adj[i].end());
vector<int> next(n + 1);
int ans = 0;
j = n;
for (i = n - 1; i >= 1; --i) {
next[i] = j;
if (!is[i] || adj[i].empty()) {
j = i;
continue;
}
int ind = lower_bound(adj[i].begin(), adj[i].end(), i) - adj[i].begin();
if (adj[i][ind] > j) {
j = i;
continue;
}
f = 1;
for (l = ind + 1; l < adj[i].size(); ++l) {
if (adj[i][l] > next[adj[i][l - 1]]) f = 0;
if (!f) break;
}
if (f)
ans++;
else
j = i;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int64_t n, m;
deque<int64_t> d[300001];
int64_t p[300001];
bool mark[300001];
int64_t ans;
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int64_t i = 1; i <= n; i++) {
int64_t x;
cin >> x;
p[x] = i;
}
for (int64_t i = 1; i <= m; i++) {
int64_t x, y;
cin >> x >> y;
d[p[x]].push_back(p[y]);
}
mark[n] = true;
for (int64_t i = n - 1; i >= 1; i--) {
int64_t c = 0;
for (int64_t j = 0; j < d[i].size(); j++) {
if (mark[d[i][j]]) c++;
}
if (c + ans < n - i)
mark[i] = true;
else
ans++;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
int cnt[maxn];
vector<int> c[maxn];
int a[maxn];
int main() {
int n, m, x, y;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
c[y].push_back(x);
}
for (int i = 0; i < c[a[n]].size(); i++) cnt[c[a[n]][i]]++;
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
if (n - i - ans == cnt[a[i]])
ans++;
else {
for (int j = 0; j < c[a[i]].size(); j++) cnt[c[a[i]][j]]++;
}
}
printf("%d\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> graph[300000 + 1];
long long int pupil[300000 + 1];
int main() {
ios_base::sync_with_stdio(false);
int n, m, x, y;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> pupil[i];
}
for (int i = 0; i < m; i++) {
cin >> x >> y;
graph[x].push_back(y);
}
set<long long int> obs;
obs.insert(pupil[n - 1]);
long long int ans = 0;
long long int found = 0;
for (int i = n - 2; i >= 0; i--) {
int curr = pupil[i];
found = 0;
for (auto x : graph[curr]) {
if (obs.find(x) != obs.end()) {
found++;
}
}
if (found == obs.size()) {
ans++;
} else {
obs.insert(curr);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = int;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n, m;
cin >> n >> m;
vector<ll> p(n);
for (auto &a : p) cin >> a, a--;
vector<vector<ll> > adj(n);
vector<bool> good(n);
for (ll i = 0; i < m; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
adj[a].push_back(b);
if (b == p[n - 1]) good[a] = true;
}
vector<bool> bad(n);
ll sizeofbad = 0;
for (ll i = n - 2; i >= 0; i--) {
ll ourbad = 0;
for (ll a : adj[p[i]])
if (bad[a]) ourbad++;
if (ourbad != sizeofbad || !good[p[i]]) bad[p[i]] = true, sizeofbad++;
}
cout << n - 1 - sizeofbad << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, M = 3e5 + 7;
int powe(int x, int y) {
x = x % mod;
int ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
vector<vector<int> > edg(M);
int arr[M];
int pas[M];
set<int> pass, lis;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, m, ctr = 0, a, b;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
edg[a].push_back(b);
if (b == arr[n - 1]) pas[a] = 1;
}
for (int i = n - 2; i >= 0; i--) {
if (pas[arr[i]]) {
bool pass = false;
if (edg[arr[i]].size() < lis.size()) {
lis.insert(arr[i]);
continue;
}
sort(edg[arr[i]].begin(), edg[arr[i]].end());
auto i2 = lis.begin();
int i1 = 0, lim = edg[arr[i]].size();
for (; i2 != lis.end() && i1 < lim;) {
while (i1 < n && edg[arr[i]][i1] < *i2) {
i1++;
}
if (edg[arr[i]][i1] == *i2) {
i1++, i2++;
} else
break;
}
if (i2 == lis.end()) {
pass = true;
}
if (pass)
ctr++;
else {
lis.insert(arr[i]);
}
} else
lis.insert(arr[i]);
}
cout << ctr;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> ma;
int a[300005];
int main() {
int n, m, x, y;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
ma[make_pair(x, y)] = 1;
}
int last = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (ma[make_pair(a[i], a[last])]) {
int j;
for (j = i; j < last; j++) {
if (ma[make_pair(a[j], a[j + 1])]) {
swap(a[j], a[j + 1]);
} else
break;
}
if (j == last) last--;
}
}
printf("%d\n", n - 1 - last);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int md = 1e9 + 7;
ll pw(ll a, ll b) {
ll r = 1, base = a;
while (b != 0) {
if (b & 1) r = (r * base) % md;
base = base * base % md;
b >>= 1;
}
return r;
}
int n, m;
vector<int> a, was;
vector<vector<int> > g;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
a.resize(n);
g.resize(n);
was.resize(n);
for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
for (int i = 0; i < m; i++) {
int w1, w2;
cin >> w1 >> w2;
w1--;
w2--;
g[w1].push_back(w2);
}
reverse(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < n; i++) was[i] = 0;
was[a[0]] = 1;
int cnt = 1;
for (int i = 1; i < n; i++) {
int cnt2 = 0;
for (int to : g[a[i]]) {
if (was[to]) cnt2++;
}
if (cnt == cnt2) {
ans++;
} else {
was[a[i]] = 1;
cnt++;
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
int SIZE(T(&t)) {
return t.size();
}
template <typename T, size_t N>
int SIZE(T (&t)[N]) {
return N;
}
string to_string(char t) { return "'" + string({t}) + "'"; }
string to_string(bool t) { return t ? "true" : "false"; }
string to_string(const string& t, int x1 = 0, int x2 = 1e9) {
string ret = "";
for (int i = min(x1, SIZE(t)), _i = min(x2, SIZE(t) - 1); i <= _i; ++i) {
ret += t[i];
}
return '"' + ret + '"';
}
string to_string(const char* t) {
string ret(t);
return to_string(ret);
}
template <size_t N>
string to_string(const bitset<N>& t, int x1 = 0, int x2 = 1e9) {
string ret = "";
for (int i = min(x1, SIZE(t)); i <= min(x2, SIZE(t) - 1); ++i) {
ret += t[i] + '0';
}
return to_string(ret);
}
template <typename T, typename... Coords>
string to_string(const T(&t), int x1 = 0, int x2 = 1e9, Coords... C);
template <typename T, typename S>
string to_string(const pair<T, S>& t) {
return "(" + to_string(t.first) + ", " + to_string(t.second) + ")";
}
template <typename T, typename... Coords>
string to_string(const T(&t), int x1, int x2, Coords... C) {
string ret = "[";
x1 = min(x1, SIZE(t));
auto e = begin(t);
advance(e, x1);
for (int i = x1, _i = min(x2, SIZE(t) - 1); i <= _i; ++i) {
ret += to_string(*e, C...) + (i != _i ? ", " : "");
e = next(e);
}
return ret + "]";
}
template <int Index, typename... Ts>
struct print_tuple {
string operator()(const tuple<Ts...>& t) {
string ret = print_tuple<Index - 1, Ts...>{}(t);
ret += (Index ? ", " : "");
return ret + to_string(get<Index>(t));
}
};
template <typename... Ts>
struct print_tuple<0, Ts...> {
string operator()(const tuple<Ts...>& t) { return to_string(get<0>(t)); }
};
template <typename... Ts>
string to_string(const tuple<Ts...>& t) {
const auto Size = tuple_size<tuple<Ts...>>::value;
return print_tuple<Size - 1, Ts...>{}(t);
}
void dbgr() { ; }
template <typename Heads, typename... Tails>
void dbgr(Heads H, Tails... T) {
cout << to_string(H) << " | ";
dbgr(T...);
}
void dbgs() { ; }
template <typename Heads, typename... Tails>
void dbgs(Heads H, Tails... T) {
cout << H << " ";
dbgs(T...);
}
const long long N = 3e5, M = 998244353;
int p[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1, _i = n; i <= _i; ++i) {
cin >> p[i];
}
set<pair<int, int>> is;
for (int i = 1, _i = m; i <= _i; ++i) {
int a, b;
cin >> a >> b;
is.insert({a, b});
}
int ans{0};
vector<int> t{p[n]};
for (int i = n - 1, _i = 1; i >= _i; --i) {
bool ok = 1;
for (int j : t) {
ok &= is.count({p[i], j});
if (!ok) {
break;
}
}
if (!ok) {
t.emplace_back(p[i]);
} else {
++ans;
}
}
dbgs(ans);
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, u, v;
cin >> n >> m;
vector<int> p(n), pos(n + 1);
for (int i = 0; i < n; i++) {
cin >> p[i];
pos[p[i]] = i;
}
vector<int> g[n];
vector<bool> good(n);
for (int i = 0; i < m; i++) {
cin >> u >> v;
if (v == p.back()) {
good[pos[u]] = true;
}
g[pos[v]].emplace_back(pos[u]);
}
int ans = 0, k = 0;
vector<int> s(n);
for (int i = n - 2; i >= 0; i--) {
if (good[i]) {
if (s[i] == k) {
ans++;
} else {
for (auto &u : g[i]) {
s[u]++;
}
k++;
}
} else {
for (auto &u : g[i]) {
s[u]++;
}
k++;
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, i;
cin >> n >> m;
long long int a[n];
for (i = 0; i < n; i++) cin >> a[i];
vector<long long int> vect;
vector<long long int> v[n + 5];
for (i = 0; i < m; i++) {
long long int u, vv;
cin >> u >> vv;
v[u].push_back(vv);
if (vv == a[n - 1]) {
vect.push_back(u);
}
}
sort(vect.begin(), vect.end());
for (i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end());
}
for (i = n - 2; i >= 0; i--) {
if (!binary_search(vect.begin(), vect.end(), a[i])) continue;
for (long long int j = i + 1; j < n - 1; j++) {
if (binary_search(v[a[j - 1]].begin(), v[a[j - 1]].end(), a[j])) {
swap(a[j - 1], a[j]);
} else
break;
}
}
long long int ans = 0;
for (i = n - 2; i >= 0; i--) {
if (binary_search(vect.begin(), vect.end(), a[i])) {
ans++;
} else
break;
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
void read(int &val) {
int x = 0;
int bz = 1;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
if (c == '-') {
bz = -1;
c = getchar();
}
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
val = x * bz;
}
vector<int> a, was;
vector<vector<int>> g;
int main() {
int n, m;
read(n);
read(m);
a.resize(n);
g.resize(n);
was.resize(n);
for (int i = 0; i <= n - 1; i++) {
read(a[i]);
a[i]--;
}
for (int i = 0; i <= m - 1; i++) {
int u, v;
read(u);
read(v);
u--;
v--;
g[u].push_back(v);
}
reverse(a.begin(), a.end());
for (int i = 0; i <= n - 1; i++) was[i] = 0;
was[a[0]] = 1;
int cnt = 1, ans = 0;
for (int i = 1; i <= n - 1; i++) {
int cnt2 = 0;
for (auto it : g[a[i]]) {
if (was[it]) cnt2++;
}
if (cnt == cnt2)
ans++;
else {
was[a[i]] = 1;
cnt++;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int N, M;
int P[MAXN];
int pos[MAXN];
vector<int> adj[MAXN];
int cnt[MAXN];
int main() {
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i++) {
scanf("%d", &P[i]);
pos[P[i]] = i;
}
for (int i = 0, a, b; i < M; i++) {
scanf("%d%d", &a, &b);
if (pos[b] > pos[a]) {
adj[b].push_back(a);
cnt[a]++;
}
}
int ans = 0;
for (int i = N - 1, cur = N; i >= 1; i--)
if (cnt[P[i]] == cur - i) {
ans++;
for (auto x : adj[P[i]]) cnt[x]--;
cur--;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t N>
int SIZE(const T (&t)[N]) {
return N;
}
template <typename T>
int SIZE(const T &t) {
return t.size();
}
string to_string(string &second, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < second.size()) ? second.substr(x1, x2 - x1 + 1) : "") +
'"';
}
string to_string(const char *second) {
string tmp(second);
return to_string(tmp);
}
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return string({c}); }
template <size_t N>
string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = ((x1 < b.size()) ? (b.to_string()).substr(x1, x2 - x1 + 1) : "");
reverse(begin(t), end(t));
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B>
string to_string(pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0) res += '\n';
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += '\n';
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0) res += '\n';
return res;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
clock_t startTime = clock();
long long T = 1;
while (T--) {
long long n, m;
cin >> n >> m;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
set<pair<long long, long long> > mm;
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
mm.insert({x, y});
}
long long idx = n - 2;
long long cnt = 0;
while (idx >= 0) {
long long flag = 0;
if (mm.find({a[idx], a[n - 1]}) == mm.end()) {
idx--;
continue;
}
for (long long i = idx + 1; i < n - cnt - 1; i++) {
if (mm.find({a[idx], a[i]}) == mm.end()) {
flag = 1;
break;
}
}
if (!flag) {
swap(a[idx], a[n - cnt - 2]);
cnt++;
}
idx--;
}
cout << cnt << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int max_n = 300005;
int n, m;
int p[max_n];
int rk[max_n];
bool vis[max_n];
vector<int> G[max_n];
set<int> s;
void dfs(int u) {
for (auto v : G[u]) s.erase(rk[v]);
vector<int> can;
for (auto v : s) {
if (v < rk[u]) {
v = p[v];
can.push_back(v);
vis[rk[v]] = true;
} else
break;
}
for (auto v : G[u])
if (!vis[rk[v]]) s.insert(rk[v]);
for (auto v : can) s.erase(rk[v]);
for (auto v : can) dfs(v);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", p + i), rk[p[i]] = i;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[v].push_back(u);
}
for (int i = 1; i <= n - 1; i++) s.insert(i);
vis[n] = true;
dfs(p[n]);
printf("%d\n", s.size());
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
long long inf = 1LL << 31;
long long mod = 1000000007;
using namespace std;
vector<int> a, b;
vector<vector<int> > g;
int main() {
int n, m;
scanf("%d%d", &n, &m);
a.resize(n);
b.assign(n + 1, 0);
g.resize(n + 1);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
}
int ans = 1;
b[a[n - 1]]++;
for (int i = n - 2; i >= 0; i--) {
int cur = a[i];
int bad = ans;
for (int j = 0; j < g[cur].size(); j++) {
int pre = g[cur][j];
bad -= b[pre];
}
if (bad) ans++, b[cur]++;
}
printf("%d", n - ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long n, k, a[1000001], d[1000001], vt[1000001];
vector<long> g[1000001];
void nhap() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= k; ++i) {
long x, y;
cin >> x >> y;
g[y].push_back(x);
}
}
void sol() {
long kq = 0, sl = 0;
for (int i = n; i >= 1; --i) {
if (vt[a[i]] == sl - kq && i != n)
++kq;
else
for (int j = 0; j < g[a[i]].size(); ++j) vt[g[a[i]][j]]++;
++sl;
}
cout << kq;
}
int main(int argc, const char* argv[]) {
nhap();
sol();
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n, 0);
for (long long i = 0; i < n; ++i) {
cin >> p[i];
--p[i];
}
set<int> fw[n];
for (long long i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
fw[--v].insert(--u);
}
int id = p[n - 1];
int ans = 0;
for (int i = n - 1; i > 0; --i) {
if (fw[p[i]].find(p[i - 1]) != fw[p[i]].end()) {
fw[p[i]].erase(p[i - 1]);
swap(p[i], p[i - 1]);
if (p[i - 1] == id) {
++ans;
} else {
i += 2;
}
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long ok[1000009], n, m, num[1000009], did, siz, u, v, p[1000009];
vector<long long> vec[1000009];
void solve() {
for (long long i = n - 1; i > 0; i--) {
if (ok[p[i]] and num[p[i]] + did == n - i - 1)
did++;
else {
siz = vec[p[i]].size();
for (long long j = 0; j < siz; j++) num[vec[p[i]][j]]++;
}
}
cout << did << endl;
}
int main() {
cin >> n >> m;
for (long long i = 1; i <= n; i++) scanf("%lld", &p[i]);
for (long long i = 1; i <= m; i++) {
scanf("%lld", &u);
scanf("%lld", &v);
vec[v].push_back(u);
if (v == p[n]) ok[u] = 1;
}
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int a[N], cnt, res;
vector<int> change[N];
bool t[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
change[u].push_back(v);
}
t[a[n]] = 1;
cnt = 1;
for (int i = n - 1; i >= 1; i--) {
int k = 0;
for (__typeof(change[a[i]].begin()) it = change[a[i]].begin();
it != change[a[i]].end(); it++)
k += t[*it];
if (k < cnt) {
cnt++;
t[a[i]] = 1;
} else
res++;
}
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
--v[i];
}
vector<vector<int>> willPass(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
willPass[a].push_back(b);
}
vector<int> isBarrier(n);
isBarrier[v.back()] = true;
int barrierSize = 1;
int answer = 0;
for (int step = 1; step < n; ++step) {
int currentStartPos = n - step - 1;
int passSuccess = 0;
for (auto toPass : willPass[v[currentStartPos]]) {
if (isBarrier[toPass]) {
++passSuccess;
}
}
if (passSuccess == barrierSize) {
++answer;
} else {
isBarrier[v[currentStartPos]] = true;
++barrierSize;
}
}
cout << answer << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long n, m, res = 0;
vector<long long> pos, mas, cnt;
set<long long> suff;
vector<vector<long long>> cons;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
mas.resize(n);
pos.resize(n);
cons.resize(n);
for (long long i = 0; i < n; i++) {
cin >> mas[i];
mas[i]--;
pos[mas[i]] = i;
}
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
if (pos[b] > pos[a]) cons[pos[a]].push_back(pos[b]);
}
suff.insert(n - 1);
for (long long i = n - 2; i >= 0; i--) {
long long s = 0;
for (auto j : cons[i]) {
if (suff.count(j)) s++;
}
if (s == suff.size()) {
res++;
} else {
suff.insert(i);
}
}
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int n, m;
int p[1000010];
set<int> st[1000010];
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d %d", &x, &y);
st[x].insert(y);
}
int x = p[n];
for (int i = n - 1; i >= 1; i--) {
int j = i;
while (j < n && st[p[j]].find(p[j + 1]) != st[p[j]].end()) {
swap(p[j], p[j + 1]);
j++;
}
}
int pos = 0;
for (int i = 1; i <= n; i++)
if (p[i] == x) pos = i;
printf("%d", n - pos);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300001;
vector<int> g[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < n; i++) cin >> p[i];
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
g[u].push_back(v);
}
unordered_set<int> need;
need.insert(p[n - 1]);
int ans = 0;
for (int i = n - 2; i > -1; i--) {
int cnt = 0;
for (auto it : g[p[i]]) {
if (need.count(it)) cnt++;
}
if (cnt == need.size())
ans++;
else
need.insert(p[i]);
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3 * 100100, MAXM = 5 * 100100;
int n, m, p[MAXN];
set<int> x[MAXN];
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &p[i]);
for (int i = 1, u, v; i <= m; ++i) scanf("%d %d", &u, &v), x[u].insert(v);
int pos = n;
for (int i = n - 1; i >= 1; --i) {
bool flg = true;
for (int j = i + 1; j <= pos; ++j)
if (x[p[i]].find(p[j]) == x[p[i]].end()) {
flg = false;
break;
}
if (!flg) continue;
for (int j = i + 1; j <= pos; ++j) swap(p[j - 1], p[j]);
pos--;
}
printf("%d\n", n - pos);
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.