Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<map>
- using namespace std;
- class Streak
- {
- public:
- int streak_start, streak_end;
- Streak()
- {
- streak_start = streak_end = -1;
- }
- Streak(int streak_start, int streak_end)
- {
- this->streak_start = streak_start;
- this->streak_end = streak_end;
- }
- };
- Streak get_streak_details(map<int, int>& streaks, int idx)
- {
- Streak s;
- if(streaks.empty())return s;
- auto lb = streaks.lower_bound(idx);
- if(lb == streaks.begin() and lb->first != idx)return s;
- if(lb == streaks.end() or lb->first > idx)
- {
- lb--;
- }
- if(lb->first <= idx and idx <= lb->second)
- {
- s.streak_start = lb->first;
- s.streak_end = lb->second;
- }
- return s;
- }
- int main()
- {
- int n, q;
- cin >> n >> q;
- vector<int> left(q);
- vector<int> right(q);
- vector<int> color_to_fill(q);
- int idx = q - 1;
- while(q--)
- {
- int l, r, c;
- cin >> l >> r >> c;
- left[idx] = l;
- right[idx] = r;
- color_to_fill[idx] = c;
- idx--;
- }
- vector<int> color(n, 0);
- // A map, this contains start and end of each streak. initially it's empty and going forward we will maintain streaks
- // this also provides us
- // O(logN) searches for next idx to fill, start and end of a streak
- // O(logN) deletion and insertion for merging streaks
- map<int, int> streaks;
- for(int i = 0; i < left.size(); i++)
- {
- int curr_idx = left[i];
- int r = right[i];
- int c = color_to_fill[i];
- while(curr_idx <= r)
- {
- if(color[curr_idx - 1] != 0)
- {
- // that means this curr_idx is part of some streak
- // auto lb = streaks.lower_bound(curr_idx);
- // if(lb->first > curr_idx)
- // {
- // // then curr_idx is part of immediate prev streak
- // lb--;
- // }
- auto streak = get_streak_details(streaks, curr_idx);
- // curr_idx = lb->second + 1;
- curr_idx = streak.streak_end + 1;
- continue;
- }
- // now the curr_idx is not part of any streak
- // so we fill the color as c
- // and merge streaks as needed
- color[curr_idx - 1] = c;
- // merge streaks
- auto prev_streak = get_streak_details(streaks, curr_idx - 1);
- auto next_streak = get_streak_details(streaks, curr_idx + 1);
- bool prev_streak_exists = (prev_streak.streak_start != -1) ? true : false;
- bool next_streak_exists = (next_streak.streak_start != -1) ? true : false;
- if(prev_streak_exists and !next_streak_exists)
- {
- streaks.erase(prev_streak.streak_start);
- streaks.insert({prev_streak.streak_start, curr_idx});
- curr_idx++;
- }
- else if(!prev_streak_exists and next_streak_exists)
- {
- streaks.erase(next_streak.streak_start);
- streaks.insert({curr_idx, next_streak.streak_end});
- curr_idx = next_streak.streak_end + 1;
- }
- else if(prev_streak_exists and next_streak_exists)
- {
- streaks.erase(next_streak.streak_start);
- streaks.erase(prev_streak.streak_start);
- streaks.insert({prev_streak.streak_start, next_streak.streak_end});
- curr_idx = next_streak.streak_end + 1;
- }
- else
- {
- streaks.insert({curr_idx, curr_idx});
- curr_idx++;
- }
- }
- }
- for(int i = 0; i < n; i++) cout << color[i] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement