PAT L2-012. 关于堆的判断
原题链接
简单
作者:
青丝蛊
,
2021-04-13 15:33:54
,
所有人可见
,
阅读 250
#include <bits/stdc++.h>
using namespace std;
vector<int> heap;
unordered_map<int, int> mp;
void adjust(int t)
{
if (!t) return;
if (heap[t] < heap[(t - 1) / 2]) swap(heap[t], heap[(t - 1) / 2]);
adjust((t - 1) / 2);
}
int main()
{
int n, m;
cin >> n >> m;
while (n--)
{
int x; scanf("%d", &x);
heap.push_back(x);
adjust(heap.size() - 1);
}
for (int i = 0; i < heap.size(); i++) mp[heap[i]] = i; // 存一下每个结点的下标
while (m--)
{
int a, b;
string s;
scanf("%d", &a);
cin >> s;
if (s == "and")
{
cin >> b;
string t;
getline(cin, t);
a = mp[a], b = mp[b];
cout << (heap[(a - 1) / 2] == heap[(b - 1) / 2] ? "T\n" : "F\n");
}
else
{
string t1, t2;
cin >> t1 >> t2;
if (t2 == "root") cout << (a == heap.front() ? "T\n" : "F\n");
else
{
string t3;
cin >> t3 >> b;
if (t2 == "parent")
{
b = mp[b];
cout << (a == heap[(b - 1) / 2] ? "T\n" : "F\n");
}
else
{
a = mp[a];
cout << (b == heap[(a - 1) / 2] ? "T\n" : "F\n");
}
}
}
}
return 0;
}