Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define endl '\n'
- using namespace std;
- #ifdef ONLINE_JUDGE
- #define eprintf(...) 42
- #else
- #define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
- #endif
- clock_t startTime;
- double getCurrentTime()
- {
- return (double)(clock() - startTime) / CLOCKS_PER_SEC;
- }
- class tracker_inter
- {
- public:
- set<pair<int, int>> forbidden;
- int pt_add(int stuff)
- {
- pair<int, int> previn = {stuff, stuff};
- if(!forbidden.empty() && forbidden.upper_bound({stuff, 2e9})!=forbidden.begin())
- {
- previn = *prev(forbidden.upper_bound({stuff, 2e9}));
- }
- if(previn.second==stuff-1)
- {
- forbidden.erase(previn);
- previn.second++;
- }
- else
- {
- previn = {stuff, stuff};
- }
- forbidden.insert(previn);
- if(next(forbidden.find(previn))!=forbidden.end())
- {
- auto thing = *next(forbidden.find(previn));
- if(thing.first==previn.second+1)
- {
- forbidden.erase(thing); forbidden.erase(previn);
- previn.second = thing.second;
- forbidden.insert(previn);
- }
- }
- return 1;
- }
- };
- long long solve(vector<int> heights)
- {
- tracker_inter Rail;
- int n = heights.size();
- long long ans = 0;
- map<int, vector<int>> posi;
- for(int i=0; i<n; i++)
- {
- posi[heights[i]].push_back(i);
- }
- for(auto lin : posi)
- {
- for(auto stuff : lin.second)
- {
- Rail.pt_add(stuff);
- }
- for(auto stuff : lin.second)
- {
- auto thing = *prev(Rail.forbidden.upper_bound(make_pair(stuff, 2e5)));
- ans += thing.second - thing.first + 1;
- }
- }
- return ans;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int t=1;
- //cin>>t;
- startTime = clock();
- while(t--)
- {
- startTime = clock();
- int n;
- cin>>n;
- vector<int> heights(n);
- for(int i=0; i<n; i++)
- {
- cin>>heights[i];
- }
- cout<<solve(heights)<<endl;
- }
- eprintf("time = %.5lf\n", getCurrentTime());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment