Submission #1275520

#TimeUsernameProblemLanguageResultExecution timeMemory
1275520InvMODSails (IOI07_sails)C++17
0 / 100
16 ms2248 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}

const int N = 1e5 + 5;

ll dif[N], cnt[N], stk[N], _tp;

void Main()
{
    int n; cin >> n;

    FOR(i, 1, n){
        int h,k; cin >> h >> k;

        dif[h - k + 1] += 1ll * k, dif[h + 1] -= 1ll * k;
    }

    _tp = -1;
    FOR(i, 1, N){
        dif[i] += dif[i - 1];
        ll ccnt = dif[i];

        ll sum = dif[i], lp = i;
        while(_tp > - 1 && cnt[stk[_tp]] < ccnt){
            sum += 1ll * (lp - stk[_tp]) * cnt[stk[_tp]];

            ccnt = (sum + (i - stk[_tp] - 1)) / (i - stk[_tp]);
            lp = stk[_tp--];
        }

        if(_tp < 0 || cnt[stk[_tp]] != ccnt){
            stk[++_tp] = lp, cnt[lp] = ccnt;

            if(ccnt * (i - lp) > sum){
                --ccnt;
                sum -= 1ll * (i - lp) * ccnt;
                lp = lp + sum - 1;

                stk[++_tp] = lp, cnt[lp] = ccnt;
            }
        }
    }

    auto f = [&](ll x) -> ll{
        return 1ll * x * (x + 1) / 2;
    };

    ll lp = n + 1, ans = 0;
    while(_tp >= 0){
        ans += 1ll * (lp - stk[_tp]) * f(cnt[stk[_tp]]);
        lp = stk[_tp--];
    }
    cout << ans << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

sails.cpp: In function 'int32_t main()':
sails.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp: In function 'void Main()':
sails.cpp:48:14: warning: iteration 100004 invokes undefined behavior [-Waggressive-loop-optimizations]
   48 |         dif[i] += dif[i - 1];
      |         ~~~~~^
sails.cpp:20:41: note: within this loop
   20 | #define FOR(i, a, b) for(int i = (a); i <= (b); i++)
      |                                         ^
sails.cpp:47:5: note: in expansion of macro 'FOR'
   47 |     FOR(i, 1, N){
      |     ^~~
#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...
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...