Solutions of k-th element - MarisaOJ: Marisa Online Judge

Solutions of k-th element

Select solution language

Write solution here.


User Avatar toshirohitsugaya    Created at    4 likes

Nên sử dụng C++ STL Vector

Note:

Sử dụng bắt đầu từ chỉ số 1 trong vector

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,q;
    cin >> n >> q;
    vector<int> a(n + 1);
    for(int i = 1 ;i <= n;i++){
        cin >> a[i];
    }
    while(q--){
        int c;
        cin >> c;
        if(c == 1){
            int tmp;
            cin >> tmp;
            a.push_back(tmp);
        }
        if(c == 2){
            if(a.size()){
                a.pop_back();
            }
        }
        if(c == 3){
            int x;
            cin >> x;
            cout << a[x] << "\n";
        }
        
    }
    return 0;
}