1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| void solve(vector<int>& heights, map<int, vector<pair<int, int>>> queries, int m) { vector<int> pre; vector<int> LIS; vector<int> res(m, 0); for (int i = 0; i < heights.size(); i++) { if (queries.contains(i)) { for (auto indexHeightPair : queries[i]) { int idx = indexHeightPair.first; int h = indexHeightPair.second; int index = find(LIS, h); res[idx] += index + 1; } } int height = heights[i]; int index = find(LIS, height); if (index == LIS.size()) { LIS.push_back(height); } else { LIS[index] = height; } pre.push_back(index + 1); } int longest = LIS.size(); for (int& height : heights) { height = -height; } vector<int> post(heights.size(), 0); LIS.clear(); for (int i = heights.size() - 1; i >= 0; i--) { if (queries.contains(i)) { for (auto indexHeightPair : queries[i]) { int idx = indexHeightPair.first; int h = -indexHeightPair.second; int index = find(LIS, h); res[idx] += index; } } int height = heights[i]; int index = find(LIS, height); if (index == LIS.size()) { LIS.push_back(height); } else { LIS[index] = height; } post[i] = index + 1; } map<int, vector<int>> numOfPre; unordered_set<int> indexOfLongest; for (int i = 0; i < heights.size(); i++) { if (pre[i] + post[i] - 1 == longest) { numOfPre[pre[i]].push_back(i); indexOfLongest.insert(i); } } for (int i = 0; i < heights.size(); i++) { if (!queries.contains(i)) { continue; } for (auto indexHeightPair : queries[i]) { int idx = indexHeightPair.first; if (!indexOfLongest.contains(i)) { res[idx] = max(res[idx], longest); } else { if (numOfPre[pre[i]].size() > 1) { res[idx] = max(res[idx], longest); } else { res[idx] = max(res[idx], longest - 1); } } } } for (int i = 0; i < m; i++) { out << res[i] << endl; } }
int main() { int n, m; in >> n >> m; vector<int> heights; map<int, vector<pair<int, int>>> queries; for (int i = 1; i <= n; i++) { int height; in >> height; heights.push_back(height); } for (int i = 0; i < m; i++) { int index, height; in >> index >> height; index--; queries[index].push_back({i, height}); } solve(heights, queries, m); return 0; }
|