[JSOI2008] solution to the problem of maximum number

Preface

Giant said: to have line trees, so konjaku began to fight line trees.

Konjaku can only take the previous 0-point board problem to open the QAQ.

Title Solution

At first, I thought that the insertion operation did not take the mold, so I hit the following retarded device.
The following code will be WA

#include <cstdio>
#include <algorithm>
#define ll long long

using namespace std;

ll read(){
    ll x = 0; int zf = 1; char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}

ll s[800005];
ll _max[800005], cnt[800005];
int m, d, pos = 1;
const int MAXM = 200001;

pair<ll, ll> query(int pos, int l, int r, int x, int y){
    if (x <= l && r <= y)
        return make_pair(cnt[pos], _max[pos]);
    pair<ll, ll> ans = make_pair(-(1ll << 62), -(1ll << 62));
    int mid = (l + r) >> 1;
    if (x <= mid)
        ans = max(ans, query(pos << 1, l, mid, x, y));
    if (mid < y)
        ans = max(ans, query(pos << 1 | 1, mid + 1, r, x, y));
    return ans;
}

void add(int pos, int l, int r, int x, ll val){
    if (l == r){
        s[pos] += val, cnt[pos] += s[pos] / d; s[pos] %= d;
        _max[pos] = s[pos];
        return ;
    }
    int mid = (l + r) >> 1;
    if (x <= mid)
        add(pos << 1, l, mid, x, val);
    else if (mid < x)
        add(pos << 1 | 1, mid + 1, r, x, val);
    s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % d;
    if (cnt[pos << 1] > cnt[pos << 1 | 1])
        _max[pos] = _max[pos << 1], cnt[pos] = cnt[pos << 1];
    else if (cnt[pos << 1] < cnt[pos << 1 | 1])
        _max[pos] = _max[pos << 1 | 1], cnt[pos] = cnt[pos << 1 | 1];
    else{
        cnt[pos] = cnt[pos << 1];
        _max[pos] = max(_max[pos << 1], _max[pos << 1 | 1]);
    }
}

int main(){
    m = read(), d = read(); char op[1]; ll t = 0;
    while (m--){
        scanf("%s", op); int n = read();
        if (op[0] == 'Q')
            printf("%lld\n", t = query(1, 1, MAXM, pos - n + 1, pos).second);
        else if (op[0] == 'A')
            add(1, 1, MAXM, ++pos, n + t);
    }
    return 0;
}

A wave of turn in WA 0.
Then I can't pass all the examples (I'm confident that I didn't test the examples).
After a careful look at the problem, I found that the insertion operation belt takes the mold. QAQ
It's toxic.
And then again...

#include <cstdio>
#include <algorithm>
#define ll long long

using namespace std;

ll read(){
    ll x = 0; int zf = 1; char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}

ll s[800005];
ll _max[800005];
int m, d, pos = 1;
const int MAXM = 200001;

ll query(int pos, int l, int r, int x, int y){
    if (x <= l && r <= y)
        return _max[pos];
    ll ans = -(1ll << 62);
    int mid = (l + r) >> 1;
    if (x <= mid)
        ans = max(ans, query(pos << 1, l, mid, x, y));
    if (mid < y)
        ans = max(ans, query(pos << 1 | 1, mid + 1, r, x, y));
    return ans;
}

void add(int pos, int l, int r, int x, ll val){
    if (l == r){
        (s[pos] += val) %= d, _max[pos] = s[pos];
        return ;
    }
    int mid = (l + r) >> 1;
    if (x <= mid)
        add(pos << 1, l, mid, x, val);
    else if (mid < x)
        add(pos << 1 | 1, mid + 1, r, x, val);
    s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % d;
    _max[pos] = max(_max[pos << 1], _max[pos << 1 | 1]);
}

int main(){
    m = read(), d = read(); char op[1]; ll t = 0;
    while (m--){
        scanf("%s", op); int n = read();
        if (op[0] == 'Q')
            printf("%lld\n", t = query(1, 1, MAXM, pos - n + 1, pos));
        else if (op[0] == 'A')
            add(1, 1, MAXM, ++pos, (n + t) % d);
    }
    return 0;
}

Keywords: PHP

Added by Ryaan on Fri, 01 Nov 2019 09:12:28 +0200