#include<bits/stdc++.h> #define mod (int)(1e9 + 7) #define int long long #define endl '\n' #define pb push_back #define PII pair<int, int> #define PLL pair<ll, ll> #define fst first #define snd second typedeflonglong ll; typedefunsignedlonglong ull; typedeflongdouble ld; constint MAX = 0x3f3f3f3f; const ll LLMAX = 9223372036854775807; constdouble PI = acos(-1); usingnamespace std; int diff[(int)1e5 + 10]; voidsolve(){ int L, M; cin >> L >> M; int l, r; for (int i = 0; i < M; i++) { cin >> l >> r; if (l > r) swap(l, r); diff[l]++; diff[r + 1]--; } // a_(-1) = 0 // a[0] = 0 + d[0] // a[1] = a[0] + d[1] // a [n] = a[n-1] + d[n] int a = 0, ans = 0; for (int i = 0; i <= L; i++) { a += diff[i]; ans += a == 0; } cout << ans << endl; } signedmain(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return0; }
#include<bits/stdc++.h> #define mod (int)(1e9 + 7) #define int long long #define endl '\n' #define pb push_back #define PII pair<int, int> #define PLL pair<ll, ll> #define fst first #define snd second typedeflonglong ll; typedefunsignedlonglong ull; typedeflongdouble ld; constint MAX = 0x3f3f3f3f; const ll LLMAX = 9223372036854775807; constdouble PI = acos(-1); usingnamespace std; int a[(int)1e5 + 10]; map<int, int> mmp; voidsolve(){ int n, b, idx, sum, ans; cin >> n >> b; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == b) idx = i; if (a[i] > b) a[i] = 1; elseif (a[i] < b) a[i] = -1; else a[i] = 0; } sum = 0; for (int i = idx; i < n; i++) { sum += a[i]; mmp[sum]++; } sum = ans = 0; for (int i = idx; i >= 0; i--) { sum += a[i]; ans += mmp[-sum]; } cout << ans << endl; } signedmain(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return0; }