Submission #568851

# Submission time Handle Problem Language Result Execution time Memory
568851 2022-05-26T09:25:21 Z maomao90 Fish 2 (JOI22_fish2) C++17
0 / 100
60 ms 17668 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;

int n;
int a[MAXN];
int q;
int lft[MAXN], rht[MAXN];
vi ev[MAXN];
vii qry[MAXN];
int ans[MAXN];
int par[MAXN];

int rp[MAXN], rnk[MAXN], rl[MAXN], rr[MAXN];
ll sm[MAXN];
void init() {
    REP (i, 1, n + 1) {
        rp[i] = i;
        rnk[i] = 1;
        rl[i] = rr[i] = i;
        sm[i] = a[i];
    }
}
int findp(int i) {
    if (rp[i] == i) return i;
    return rp[i] = findp(rp[i]);
}
bool join(int a, int b) {
    int pa = findp(a), pb = findp(b);
    if (pa == pb) return 0;
    if (rnk[pa] < rnk[pb]) swap(pa, pb);
    if (rnk[pa] == rnk[pb]) rnk[pa]++;
    rp[pb] = pa;
    sm[pa] += sm[pb];
    mxto(rr[pa], rr[pb]);
    mnto(rl[pa], rl[pb]);
    return 1;
}
inline int getr(int i) {
    return rr[findp(i)];
}
inline int getl(int i) {
    return rl[findp(i)];
}
inline int getsm(int i) {
    return sm[findp(i)];
}

int fw[MAXN];
void incre(int i, int x) {
    for (; i <= n; i += i & -i) fw[i] += x;
}
int qsm(int i) {
    int res = 0;
    for (; i > 0; i -= i & -i) res += fw[i];
    return res;
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n;
    REP (i, 1, n + 1) {
        cin >> a[i];
    }
    vi id(n, 0);
    iota(ALL(id), 1);
    sort(ALL(id), [&] (int l, int r) {
            return a[l] < a[r];
            });
    init();
    for (int i : id) {
        while (i) {
            bool done = 1;
            while (getr(i) + 1 <= n && a[getr(i) + 1] <= getsm(i) &&
                    a[getr(i) + 1] <= a[i]) {
                done = 0;
                join(i, getr(i) + 1);
            }
            while (getl(i) - 1 >= 1 && a[getl(i) - 1] <= getsm(i) &&
                    a[getl(i) - 1] <= a[i]) {
                done = 0;
                join(i, getl(i) - 1);
            }
            if (getr(i) + 1 <= n && a[getr(i) + 1] <= getsm(i)) {
                par[i] = getr(i) + 1;
            } else if (getl(i) - 1 >= 1 && a[getl(i) - 1] <= getsm(i)) {
                par[i] = getl(i) - 1;
            }
            if (done) {
                break;
            }
        }
        lft[i] = getl(i);
        rht[i] = getr(i);
    }
    reverse(ALL(id));
    for (int i : id) {
        if (par[i] != 0) {
            lft[i] = lft[par[i]];
            rht[i] = rht[par[i]];
        }
        ev[rht[i]].pb(lft[i]);
        cerr << i << ": " << lft[i] << ' ' << rht[i] << '\n';
    }
    cin >> q;
    REP (i, 0, q) {
        int t; cin >> t;
        if (t == 1) {
            int x, y; cin >> x >> y;
            a[x] = y;
        } else {
            int l, r; cin >> l >> r;
            qry[r].pb({l, i});
        }
    }
    RREP (i, n, 1) {
        for (int l : ev[i]) {
            incre(l, 1);
        }
        for (auto [l, j] : qry[i]) {
            ans[j] = qsm(l);
        }
    }
    REP (i, 0, q) {
        cout << ans[i] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9732 KB Output is correct
2 Incorrect 60 ms 17668 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9732 KB Output is correct
2 Incorrect 60 ms 17668 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9732 KB Output is correct
2 Incorrect 60 ms 17668 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -