1. 程式人生 > 實用技巧 >P3613 【深基15.例2】寄包櫃

P3613 【深基15.例2】寄包櫃

注意點:

  1. 為了保證複雜度,所以放棄去重,所以不可以用二分離散化
  2. 由於1的原因,所以find需要從後逐個往前找,因為同一個櫃子可能多次放入,後面的是最新的記錄
#include<iostream>
#include<vector>

using namespace std;

#define PII pair<int, int>
#define x first
#define y second

const int N = 100010;

int n, q;

vector<PII> tank[N];

int find(int a, int b){
    for(int i = tank[a].size() - 1; i >= 0; i --)
        if(tank[a][i].x == b) return i;
        
    return -1;
}

int main(){
    cin >> n >> q;
    
    while(q --){
        int op, a, b; // 操作,櫃子號,櫃子格
        
        cin >> op >> a >> b;
        
        if(op == 1){
            int x;
            cin >> x;
            
            if(x){
                tank[a].push_back({b, x});
                continue;
            }
            
            int u = find(a, b);
            if(~u) tank[a][u].second = 0;
        }else{
            int u = find(a, b);
            if(~u) cout << tank[a][u].second << endl;
        }
    }
    
    return 0;
}