Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <fstream>
- #include <string>
- #include <algorithm>
- #include <sstream>
- #include <cmath>
- using namespace std;
- #define JUDJE
- #ifndef JUDJE
- ifstream in("input.txt");
- ofstream out("output.txt");
- #define cin in
- #define cout out
- #endif
- #define ll long long
- #define MOD 1000000007
- #define mp(a, b) make_pair(a, b)
- #define pii pair<int, int>
- ll depth[223456];
- int main()
- {
- ios_base::sync_with_stdio(0);
- int n;
- cin >> n;
- set<int> st;
- int t;
- cin >> t;
- st.insert(t);
- depth[t] = 0;
- ll answer = 0;
- for(int i = 2; i <= n; ++i)
- {
- int a;
- cin >> a;
- set<int>::iterator ubound = upper_bound(st.begin(), st.end(), a);
- if(ubound == st.end())
- answer += depth[a] = depth[*(--ubound)] + 1;
- else
- {
- if(ubound == st.begin())
- answer += depth[a] = depth[*ubound] + 1;
- else
- {
- set<int>::iterator lbound = --upper_bound(st.begin(), st.end(), a);
- if(depth[*lbound] > depth[*ubound])
- answer += depth[a] = depth[*lbound] + 1;
- else
- answer += depth[a] = depth[*ubound] + 1;
- }
- }
- st.insert(a);
- }
- cout << answer;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement