Submission #1170103

#TimeUsernameProblemLanguageResultExecution timeMemory
1170103jerzykTwo Antennas (JOI19_antennas)C++20
0 / 100
1 ms836 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1000'000'007LL;
const int N = 5'007;
int tab[N];
pair<int, int> ran[N];
vector<int> poc[N], kon[N];
int dp[N];
vector<pair<int, int>> que[N];
int ans[N];
bool czy[N];

void Do(int n)
{
    for(int i = 0; i <= n + 1; ++i) dp[i] = -1;
    for(int i = 0; i <= n + 1; ++i) czy[i] = 0;
    //cout << "Do:\n";
    for(int i = n; i >= 1; --i)
    {
        for(int j = 0; j < (int)poc[i].size(); ++j)
            czy[poc[i][j]] = 1;
        /*cout << "i: " << "\n";
        for(int j = 1; j <= n; ++j)
            cout << czy[j] << " ";
        cout << "\n";*/
        for(int j = i + ran[i].st; j <= min(n, i + ran[i].nd); ++j)
        {
            dp[j] = max(dp[j], dp[j - 1]);
            if(czy[j])
                dp[j] = max(dp[j], tab[i] - tab[j]);
        }

        /*for(int j = 1; j <= n; ++j)
            cout << dp[j] << " ";
        cout << "\n";*/

        for(int j = 0; j < (int)que[i].size(); ++j)
            ans[que[i][j].nd] = max(ans[que[i][j].nd], dp[que[i][j].st]);
        for(int j = 0; j < (int)kon[i].size(); ++j)
            czy[kon[i][j]] = 0;
    }
}

void Solve()
{
    int n, q, a, b;
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
        cin >> tab[i] >> a >> b;
        ran[i] = make_pair(a, b);
        poc[max(0, i - a)].pb(i);
        kon[max(0, i - b)].pb(i);
    }
    cin >> q;
    for(int i = 1; i <= q; ++i)
    {
        cin >> a >> b;
        ans[i] = -1;
        que[a].pb(make_pair(b, i));
    }
    Do(n);
    for(int i = 1; i <= n; ++i) tab[i] *= -1;
    Do(n);
    for(int i = 1; i <= q; ++i)
        cout << ans[i] << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...