Submission #1323158

#TimeUsernameProblemLanguageResultExecution timeMemory
1323158adscodingTriple Jump (JOI19_jumps)C++20
46 / 100
4093 ms104192 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __prine_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        cerr << "  ,  ";
        ++s;
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = {0, (__prine_one(s, args), 0)...};
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X, class Y>
    bool maximize(const X &a, const X &b)
    {
        if (a < b)
        {
            a = b;
            return true;
        }
        return false;
    }

template<class X, class Y>
    bool minimize(const X &a, const X &b)
    {
        if (a > b)
        {
            a = b;
            return true;
        }
        return false;
    }


// --------------------------------------------------------------------------------------------

const int maxn = 5e5 + 3;
int n, q;
ll a[maxn];

// --------------------------------------------------------------------------------------------

namespace sub2
{
	bool approve()
	{
		return n <= 5000;
	}

	ll f[maxn][20];
	int lg[maxn], pre[5005][5005];

	ll get_max(int l, int r)
	{
		if (l > r) return 1e9;
		int k = lg[r - l + 1];
		return max(f[l][k], f[r - (1 << k) + 1][k]);
	}

	void solve()
	{	
		FOR(i, 1, n)
			f[i][0] = a[i];
		FOR(j, 1, 19)
			FOR(i, 1, n - (1 << j) + 1)
				f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);

		FOR(i, 2, n)
			lg[i] = lg[i >> 1] + 1;

		FOR(i, 1, n - 2)
			FOR(j, i + 2, n)
				pre[i][j] = a[i] + a[j] + get_max(i + 1, i + (j - i) / 2);

		FORD(l, n, 1)
			FOR(r, 1, n)
			{
				pre[l][r] = max(pre[l][r], pre[l + 1][r]);
				pre[l][r] = max(pre[l][r], pre[l][r - 1]);
			}

		while (q--)
		{
			int l, r; cin >> l >> r;
			if (l > r)
				return void(cout << 0 << '\n');
			cout << pre[l][r] << '\n';
		}
	}
}

namespace sub3
{
	ll suff[maxn];

	bool approve()
	{
		return true;
	}

	void solve()
	{
		while (q--)
		{
			vector<int> st;
			int L, R; cin >> L >> R;
			if (L > R)
				return void(cout << "0\n");

			suff[R] = a[R];
			FORD(i, R - 1, L)
				suff[i] = max(a[i], suff[i + 1]);

			vector<pii> cand;

			FOR(i, L, R)
			{
				while (st.size() && a[st.back()] <= a[i])
				{
					cand.push_back({st.back(), i});
					st.pop_back();
				}
				if (st.size()) cand.push_back({st.back(), i});

				st.push_back(i);
			}

			ll res = 0;
			for (const auto &p : cand)
			{
				int x = p.fi, y = p.se;
				if (y + y - x <= R)
					res = max(res, a[x] + a[y] + suff[y + y - x]);
			}

			cout << res << '\n';

			FOR(i, L, R)
				suff[i] = 0;
		}
	}
}

void solve()
{
	cin >> n;
	FOR(i, 1, n)
		cin >> a[i];
	cin >> q;

	if (sub2 :: approve()) return sub2 :: solve(), void();
	sub3 :: solve();
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

jumps.cpp: In function 'int main()':
jumps.cpp:196:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  196 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:197:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  197 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...