#include <iostream>
#include <algorithm>
#include <limits>
#include <vector>
#include <functional>
#include <queue>
using namespace std;
#pragma warning (disable: 4996)
class RangeMax {
public:
vector<int>dat; int size_ = 1;
void init(int sz) {
while (size_ <= sz) size_ *= 2;
dat.resize(size_ * 2, 0);
}
void update(int pos, int x) {
pos += size_; dat[pos] = x;
while (pos >= 2) {
pos /= 2;
dat[pos] = max(dat[pos * 2], dat[pos * 2 + 1]);
}
}
int query_(int l, int r, int a, int b, int u) {
if (l <= a && b <= r) return dat[u];
if (r <= a || b <= l) return -(1 << 30);
int v1 = query_(l, r, a, (a + b) >> 1, u * 2);
int v2 = query_(l, r, (a + b) >> 1, b, u * 2 + 1);
return max(v1, v2);
}
int query(int l, int r) {
return query_(l, r, 0, size_, 1);
}
};
int N, Q, A, P[1 << 19], C[1 << 19], q[1 << 19], D[1 << 19]; bool used[1 << 19];
int cnt1;
char op[1 << 19]; int x[1 << 19], e[1 << 19], deg[1 << 19], dist[1 << 19];
int Top10[11];
vector<int>G[11];
int main() {
// 入力
scanf("%d%d", &N, &A);
for (int i = 1; i <= N; i++) scanf("%d", &P[i]);
vector<pair<int, int>> S;
for (int i = 1; i <= N; i++) S.push_back(make_pair(P[i], i));
sort(S.begin(), S.end());
reverse(S.begin(), S.end());
for (int i = 0; i < 10; i++) { if (i < S.size()) Top10[i] = S[i].second; }
for (int i = 0; i < S.size() - 1; i++) G[S[i].second].push_back(S[i + 1].second);
// Top10 と番号を管理
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) {
cin >> op[i];
if (op[i] == 'E') {
// 入力部分
scanf("%d%d", &x[i], &e[i]);
// 変更部分
for (int j = 9; j >= e[i] - 1; j--) Top10[j + 1] = Top10[j];
Top10[e[i] - 1] = i + N;
if (e[i] >= 2) G[Top10[e[i] - 2]].push_back(i + N);
if (Top10[e[i]] >= 1) G[i + N].push_back(Top10[e[i]]);
}
if (op[i] == 'F') { scanf("%d", &x[i]); }
}
queue<int>que;
for (int i = 1; i <= N + Q; i++) { for (int j : G[i]) deg[j]++; }
for (int i = 1; i <= N + Q; i++) dist[i] = -(1 << 30);
for (int i = 1; i <= N + Q; i++) { if (deg[i] == 0) { dist[i] = 0; que.push(i); } }
while (!que.empty()) {
int pos = que.front(); que.pop();
for (int i : G[pos]) {
if (dist[i] < dist[pos] + 1) {
dist[i] = dist[pos] + 1;
que.push(i);
}
}
}
vector<pair<int, int>> T2;
for (int i = 1; i <= N + Q; i++) T2.push_back(make_pair(dist[i], i));
sort(T2.begin(), T2.end());
for (int i = 0; i < T2.size(); i++) {
if (T2[i].second <= N) D[T2[i].second] = N + Q - i;
else e[T2[i].second - N] = N + Q - i;
}
// 本質: RMQ におけるクエリ
RangeMax X; X.init(N + 2);
for (int i = 1; i <= N; i++) X.update(i, D[i]);
for (int i = 1; i <= Q; i++) {
if (op[i] == 'E') {
X.update(x[i], e[i]);
}
if (op[i] == 'F') {
if (x[i] == A) {
printf("0\n");
}
if (x[i] < A) {
int score = (A - x[i]), G = X.query(x[i], A);
int L = A + 1, R = N + 1, M, maxn = -(1 << 30);
for (int j = 0; j < 22; j++) {
M = (L + R) / 2;
int F = X.query(A + 1, M + 1);
if (F < G) { maxn = max(maxn, M); L = M; }
else { R = M; }
}
if (maxn != -(1 << 30) && A < N) score += (maxn - A);
printf("%d\n", score);
}
if (x[i] > A) {
int score = (x[i] - A), G = X.query(A + 1, x[i] + 1);
int L = 1, R = A, M, minx = (1 << 30);
for (int j = 0; j < 22; j++) {
M = (L + R) / 2;
int F = X.query(M, A);
if (F < G) { minx = min(minx, M); R = M; }
else { L = M; }
}
if (minx != (1 << 30) && 1 < A) score += (A - minx);
printf("%d\n", score);
}
}
}
return 0;
}
Compilation message
cake.cpp:8:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning (disable: 4996)
cake.cpp: In function 'int main()':
cake.cpp:53:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < 10; i++) { if (i < S.size()) Top10[i] = S[i].second; }
~~^~~~~~~~~~
cake.cpp:55:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < S.size() - 1; i++) G[S[i].second].push_back(S[i + 1].second);
~~^~~~~~~~~~~~~~
cake.cpp:92:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < T2.size(); i++) {
~~^~~~~~~~~~~
cake.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &A);
~~~~~^~~~~~~~~~~~~~~~
cake.cpp:47:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= N; i++) scanf("%d", &P[i]);
~~~~~^~~~~~~~~~~~~
cake.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
cake.cpp:63:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x[i], &e[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
cake.cpp:71:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
if (op[i] == 'F') { scanf("%d", &x[i]); }
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
7 ms |
1792 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
7 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
7 ms |
1792 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
16 ms |
3064 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
15 ms |
3836 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
10 ms |
2556 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
11 ms |
3836 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
40 ms |
11476 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
44 ms |
14032 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
54 ms |
14032 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
4 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
98 ms |
22496 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
125 ms |
31296 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
115 ms |
33896 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
4 ms |
1024 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
31 ms |
5336 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
21 ms |
7280 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
3 ms |
796 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
29 ms |
6888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
9 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
45 ms |
9324 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
127 ms |
26888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
4 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
9 ms |
2172 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
115 ms |
20680 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
101 ms |
20248 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |