[JSOI2008] The maximum number of explanations

Foreword

Giant guy said: to have a tree line, then started playing konjac tree line.

Konjac can only take before 0 surgery board questions the QAQ.

answer

At first I thought insertion operation without modulo, then hit the following mentally retarded stuff
following code would 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;
}

Wave turned over to WA 0.
Then look right, ah, ah samples have not been (I did not test sample confident).
Carefully looked at the titleFound insert with modulo. QAQ
really toxic.
Then again too ...

#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;
}

Guess you like

Origin www.cnblogs.com/linzhengmin/p/11128242.html