Cod sursa(job #2517172)

Utilizator blatulInstitutul de Arta Gastronomica blatul Data 2 ianuarie 2020 22:55:00
Problema Secv8 Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <fstream>
#include <iostream>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;

struct Rope : rope<int> {
    template <typename T>
    void Insert(int position, T key) {
        if (position == 0) {
            insert(mutable_begin(), key);
            return;
        }
        
        auto before_position = substr(0, position);
        erase(0, position);
        insert(mutable_begin(), key);
        insert(mutable_begin(), before_position);
    }
};

int main() {
    #ifdef INFOARENA
    ifstream cin("secv8.in");
    ofstream cout("secv8.out");
    #endif
    
    int num_queries, has_reverse; cin >> num_queries >> has_reverse;
    Rope s, rs;
    while (num_queries--> 0) {
        char query_type; cin >> query_type;
        if (query_type == 'I') {
            int el, pos; cin >> pos >> el; --pos;
            s.Insert(pos, el);
            rs.Insert(rs.size() - pos, el);
        } else if (query_type == 'A') {
            int pos; cin >> pos; --pos;
            cout << s[pos] << '\n';
        } else if (query_type == 'R') {
            int from, to; cin >> from >> to; --from; --to;
            int reversed_from = rs.size() - to - 1;
            int length = to - from + 1;
            
            auto reversed_substring = rs.substr(reversed_from, length);
            auto substring = s.substr(from, length);
            
            s.erase(from, length);
            s.Insert(from, reversed_substring);
            
            rs.erase(reversed_from, length);
            rs.Insert(reversed_from, substring);
        } else {
            int from, to; cin >> from >> to; --from; --to;
            s.erase(from, to - from + 1);
            rs.erase(rs.size() - to - 1, to - from + 1);
        }
    }
    for (auto v : s) {
        cout << v << ' ';
    }
    cout << endl;
}