제출 #1324425

#제출 시각아이디문제언어결과실행 시간메모리
1324425adscodingBigger segments (IZhO19_segments)C++20
100 / 100
120 ms19996 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>
bool maximize(X &a, const X &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

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

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

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

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

namespace sub1
{
    bool check(const int &mask)
    {
        vector<int> bits;
        bits.push_back(0);
        ll lst = 0;
        FOR(i, 0, n - 1)
            if (mask >> i & 1)
            {
                ll cur = pre[i + 1] - pre[bits.back()];
                if (cur >= lst)
                    lst = cur;
                else
                    return false;
                bits.push_back(i + 1);
            }

        return true;
    }

    void solve()
    {
        int res = 1;
        FOR(mask, 0, (1 << n) - 1)
            if (check(mask))
                res = max(res, __builtin_popcount(mask) + 1);
        cout << res;
    }
}

namespace AC
{
    ll near[maxn], dp[maxn];
    vector<ll> ids;

    int lower(ll x) {return lower_bound(all(ids), x) - ids.begin() + 1;}

    struct FenTree
    {
        vector<int> fw;
        void init(int n)
        {
            fw.assign(n + 3, 0);
        }
        void upd(int pos, int val)
        {
            for (; pos < (int)fw.size(); pos += pos & -pos)
                fw[pos] = max(fw[pos], val);
        }
        int get(int pos)
        {
            int res = 0;
            for (; pos; pos &= pos - 1)
                res = max(res, fw[pos]);
            return res;
        }
    };


    void solve()
    {
        if (n == 1) return void(cout << 1);

        FOR(i, 0, n)
            ids.push_back(pre[i]);
        uni(ids);

        FenTree fw;
        fw.init((int)ids.size());

        dp[0] = 0;
        near[0] = 0;

        FOR(i, 1, n)
        {
            int x = fw.get(lower(pre[i]));
            dp[i] = dp[x] + 1;
            near[i] = pre[i] - pre[x];
            fw.upd(lower(near[i] + pre[i]), i);
        }

        cout << dp[n];
    }
}

void solve()
{
    cin >> n;
    FOR(i, 1, n)
    {
        cin >> a[i];
        pre[i] = pre[i - 1] + a[i];
    }
    AC :: 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;
}

컴파일 시 표준 에러 (stderr) 메시지

segments.cpp: In function 'int main()':
segments.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:182:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |         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...
#Verdict Execution timeMemoryGrader output
Fetching results...