L2-012 关于堆的判断

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    vector<int> v;
    make_heap(v.begin(), v.end(), greater<int>());
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        v.push_back(x);
        push_heap(v.begin(), v.end(), greater<int>());
    }
    unordered_map<int, int> hash;
    for (int i = 0; i < n; i++) {
        hash[v[i]] = i + 1;
    }
    getchar();
    while (q--) {
        string s;
        getline(cin, s);
        stringstream ss(s);
        vector<string> op;
        string all = s;
        while (ss >> s) {
            op.push_back(s);
        }
        if (all.find("root") != string::npos) {
            int x = stoi(op[0]);
            cout << (hash[x] == 1 ? "T" : "F") << "\n";
        } else if (all.find("siblings") != string::npos) {
            int x = stoi(op[0]), y = stoi(op[2]);
            cout << ((hash[x] / 2 == hash[y] / 2) ? "T" : "F") << "\n";
        } else if (all.find("parent") != string::npos) {
            int x = stoi(op[0]), y = stoi(op[5]);
            cout << ((hash[y] / 2 == hash[x]) ? "T" : "F") << "\n";
        } else if (all.find("child") != string::npos) {
            int x = stoi(op[0]), y = stoi(op[5]);
            cout << ((hash[x] / 2 == hash[y]) ? "T" : "F") << "\n";
        }
        
    }
    
    return 0;
}
posted @ 2022-03-17 10:27  Xxaj5  阅读(15)  评论(0编辑  收藏  举报