Submission #361755

#TimeUsernameProblemLanguageResultExecution timeMemory
361755RyoPhamSterilizing Spray (JOI15_sterilizing)C++14
100 / 100
1567 ms8940 KiB
#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 1e5 + 5;

int n, Q, K;
int a[maxn];

struct segment_tree
{
    lli total[maxn * 4];
    pair<int, int> min_val[maxn * 4];

    void build(int x, int l, int r)
    {
        if(l == r)
        {
            total[x] = a[l];
            min_val[x] = make_pair(total[x] == 0 ? 1 : 0, l);
            return;
        }
        int mid = (l + r) / 2;
        build(x * 2, l, mid);
        build(x * 2 + 1, mid + 1, r);
        total[x] = total[x * 2] + total[x * 2 + 1];
        min_val[x] = min(min_val[x * 2], min_val[x * 2 + 1]);
    }

    pair<int, int> get(int x, int l, int r, int L, int R)
    {
        if(L > r || l > R || L > R) return make_pair(1, n + 1);
        if(l >= L && r <= R)
            return min_val[x];
        int mid = (l + r) / 2;
        return min(get(x * 2, l, mid, L, R),
                   get(x * 2 + 1, mid + 1, r, L, R));
    }

    void spray(int x, int l, int r, int p)
    {
        if(l == r)
        {
            total[x] /= K;
            min_val[x] = make_pair(total[x] == 0 ? 1 : 0, l);
            return;
        }
        int mid = (l + r) / 2;
        if(p <= mid) spray(x * 2, l, mid, p);
        else spray(x * 2 + 1, mid + 1, r, p);
        total[x] = total[x * 2] + total[x * 2 + 1];
        min_val[x] = min(min_val[x * 2], min_val[x * 2 + 1]);
    }

    void update(int x, int l, int r, int p, int k)
    {
        if(l == r)
        {
            total[x] = k;
            min_val[x] = make_pair(total[x] == 0 ? 1 : 0, l);
            return;
        }
        int mid = (l + r) / 2;
        if(p <= mid) update(x * 2, l, mid, p, k);
        else update(x * 2 + 1, mid + 1, r, p, k);
        total[x] = total[x * 2] + total[x * 2 + 1];
        min_val[x] = min(min_val[x * 2], min_val[x * 2 + 1]);
    }

    lli get_total(int x, int l, int r, int L, int R)
    {
        if(L > r || l > R) return 0;
        if(l >= L && r <= R)
            return total[x];
        int mid = (l + r) / 2;
        return get_total(x * 2, l, mid, L, R) + get_total(x * 2 + 1, mid + 1, r, L, R);
    }
} tree;

void read_input()
{
    cin >> n >> Q >> K;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
}

void solve()
{
    tree.build(1, 1, n);
    for(; Q > 0; --Q)
    {
        int type, x, y;
        cin >> type >> x >> y;
        if(type == 1)
            tree.update(1, 1, n, x, y);
        else if(type == 2)
        {
            if(K == 1) continue;
            auto t = tree.get(1, 1, n, x, y);
            while(t.fi == 0)
            {
                tree.spray(1, 1, n, t.se);
                t = tree.get(1, 1, n, t.se + 1, y);
            }
        }
        else cout << tree.get_total(1, 1, n, x, y) << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    read_input();
    solve();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...