#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
const int maxn = 2e5+5;
int n, q;
int H[maxn], A[maxn], B[maxn];
int ql[maxn], qr[maxn];
ll res[maxn];
vector<int> buck[maxn];
vector<int> que[maxn];
ll tmp[maxn];
const int inf = 1234567890;
struct segtree
{
struct node
{
ll a = -1, b = inf;
ll res = -1;
};
node st[4*maxn];
void clear()
{
for(int i = 0; i<= 4*n; i++) st[i] = node();
}
void build(ll *arr, int p = 1, int L = 1, int R = n)
{
if(L == R)
{
st[p].a = arr[L];
return;
}
int M = (L+R)/2;
build(arr, 2*p, L, M);
build(arr, 2*p+1, M+1, R);
}
void modb(int p, ll x)
{
st[p].b = min(st[p].b, x);
st[p].res = max(st[p].res, st[p].a-st[p].b);
}
void push(int p)
{
modb(2*p, st[p].b);
modb(2*p+1, st[p].b);
st[p].b = inf;
}
void update(int p)
{
st[p].a = max(st[2*p].a, st[2*p+1].a);
st[p].res = max(max(st[p].res, st[2*p].res), max(st[2*p+1].res, st[p].a-st[p].b));
}
void rngb(int i, int j, ll dx, int p = 1, int L = 1, int R = n)
{
if(i> R || j< L) return;
if(i<= L && R<= j)
{
modb(p, dx);
return;
}
push(p);
int M = (L+R)/2;
rngb(i, j, dx, 2*p, L, M);
rngb(i, j, dx, 2*p+1, M+1, R);
update(p);
}
void pointa(int x, int dx, int p = 1, int L = 1, int R = n)
{
if(L == R)
{
st[p].a += dx;
st[p].b = inf;
return;
}
push(p);
int M = (L+R)/2;
if(x<= M) pointa(x, dx, 2*p, L, M);
else pointa(x, dx, 2*p+1, M+1, R);
update(p);
}
ll ask(int i, int j, int p = 1, int L = 1, int R = n)
{
if(i> R || j< L) return -1;
if(i<= L && R<= j) return st[p].res;
push(p);
int M = (L+R)/2;
ll x = ask(i, j, 2*p, L, M);
ll y = ask(i, j, 2*p+1, M+1, R);
return max(x, y);
}
};
segtree foo;
void solve()
{
foo.clear();
for(int i = 1; i<= n; i++) buck[i].clear();
for(int i = 1; i<= n; i++) que[i].clear();
for(int i = 1; i<= n; i++)
{
buck[max(0, i-A[i])].pb(i);
buck[max(0, i-B[i]-1)].pb(-i);
}
for(int i = 1; i<= q; i++)
{
que[ql[i]].pb(i);
}
for(int i = 1; i<= n; i++) tmp[i] = H[i]-inf;
foo.build(tmp);
for(int i = n; i>= 1; i--)
{
for(int x : buck[i])
{
if(x> 0) foo.pointa(x, inf);
else foo.pointa(-x, -inf);
}
foo.rngb(min(i+A[i], n), min(i+B[i], n), H[i]);
for(int k : que[i])
{
res[k] = max(res[k], foo.ask(ql[k], qr[k]));
}
}
}
int main()
{
scanf("%d", &n);
for(int i = 1; i<= n; i++)
{
scanf("%d %d %d", &H[i], &A[i], &B[i]);
}
scanf("%d", &q);
for(int i = 1; i<= q; i++)
{
scanf("%d %d", &ql[i], &qr[i]);
}
memset(res, -1, sizeof res);
solve();
reverse(H+1, H+n+1);
reverse(A+1, A+n+1);
reverse(B+1, B+n+1);
for(int i = 1; i<= q; i++)
{
int l = ql[i], r = qr[i];
ql[i] = n+1-r;
qr[i] = n+1-l;
}
solve();
for(int i = 1; i<= q; i++) printf("%lld\n", res[i]);
}
Compilation message
antennas.cpp: In function 'int main()':
antennas.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
antennas.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &H[i], &A[i], &B[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:144:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
antennas.cpp:147:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &ql[i], &qr[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
11256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
11256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
469 ms |
37204 KB |
Output is correct |
2 |
Correct |
535 ms |
40124 KB |
Output is correct |
3 |
Correct |
349 ms |
31592 KB |
Output is correct |
4 |
Correct |
518 ms |
40036 KB |
Output is correct |
5 |
Correct |
223 ms |
24656 KB |
Output is correct |
6 |
Correct |
522 ms |
40036 KB |
Output is correct |
7 |
Correct |
441 ms |
36324 KB |
Output is correct |
8 |
Correct |
517 ms |
40164 KB |
Output is correct |
9 |
Correct |
69 ms |
15860 KB |
Output is correct |
10 |
Correct |
534 ms |
40208 KB |
Output is correct |
11 |
Correct |
319 ms |
29592 KB |
Output is correct |
12 |
Correct |
511 ms |
40100 KB |
Output is correct |
13 |
Correct |
347 ms |
38848 KB |
Output is correct |
14 |
Correct |
340 ms |
38488 KB |
Output is correct |
15 |
Correct |
345 ms |
37732 KB |
Output is correct |
16 |
Correct |
326 ms |
37860 KB |
Output is correct |
17 |
Correct |
356 ms |
39268 KB |
Output is correct |
18 |
Correct |
343 ms |
38208 KB |
Output is correct |
19 |
Correct |
351 ms |
38180 KB |
Output is correct |
20 |
Correct |
345 ms |
38656 KB |
Output is correct |
21 |
Correct |
323 ms |
38884 KB |
Output is correct |
22 |
Correct |
343 ms |
38572 KB |
Output is correct |
23 |
Correct |
347 ms |
38432 KB |
Output is correct |
24 |
Correct |
320 ms |
37560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
11256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |