Submission #968877

#TimeUsernameProblemLanguageResultExecution timeMemory
968877nguyentunglamRadio Towers (IOI22_towers)C++17
0 / 100
4027 ms10304 KiB
#include "towers.h" #include<bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int h[N], lg[N], sp[20][N], L[N], R[N]; int n; int get(int l, int r) { if (l > r) return -1e9; int k = lg[r - l + 1]; return max(sp[k][l], sp[k][r - (1 << k) + 1]); } void init(int N, std::vector<int> H) { n = N; for(int i = 0; i < n; i++) h[i] = H[i]; stack<int> st; for(int i = 0; i < n; i++) { while (!st.empty() && h[st.top()] >= h[i]) st.pop(); L[i] = st.empty() ? -1 : st.top(); st.push(i); } while (!st.empty()) st.pop(); for(int i = n - 1; i >= 0; i--) { while (!st.empty() && h[st.top()] >= h[i]) st.pop(); R[i] = st.empty() ? n : st.top(); st.push(i); } for(int i = 0; i < n; i++) sp[0][i] = h[i]; for(int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1; for(int j = 1; j <= lg[n]; j++) for(int i = 0; i + (1 << j) - 1 < n; i++) { sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]); } } int max_towers(int l, int r, int d) { int ans = 0; for(int i = l; i <= r; i++) { if (get(L[i] + 1, i - 1) - d < h[i]) continue; if (get(i + 1, R[i] - 1) - d < h[i]) continue; ans++; } int left = -1, right = -1; for(int i = l; i <= r; i++) { if (L[i] < l) { if (R[i] > r || get(i + 1, R[i] - 1) - d >= h[i]) { left = i; break; } } } for(int i = r; i >= l; i--) { if (R[i] > r) { if (L[i] < l || get(L[i] + 1, i - 1) - d >= h[i]) { right = i; break; } } } if (get(L[left] - 1, left - 1) - d < h[left]) ans += left >= 0; if (right != left && get(right + 1, R[right] - 1) - d < h[right]) ans += right >= 0; return ans; } #ifdef ngu int main() { freopen ("task.inp", "r", stdin); freopen ("task.ans", "w", stdout); int N, Q; assert(2 == scanf("%d %d", &N, &Q)); std::vector<int> H(N); for (int i = 0; i < N; ++i) { assert(1 == scanf("%d", &H[i])); } init(N, H); for (int i = 0; i < Q; ++i) { int L, R, D; assert(3 == scanf("%d %d %d", &L, &R, &D)); printf("%d\n", max_towers(L, R, D)); } return 0; } #endif // ngu

Compilation message (stderr)

towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:36:56: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   36 |     sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
      |                                                      ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...