Submission #516823

#TimeUsernameProblemLanguageResultExecution timeMemory
516823blueStreet Lamps (APIO19_street_lamps)C++17
20 / 100
99 ms4452 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;

const int mx = 100;

int n, q;
int s[1+mx];

set<int> offpoints;

struct segtree2
{
    vvi toggle_stamp = vvi(1+mx, vi(1+mx, 0));
    vvi total_time = vvi(1+mx, vi(1+mx, 0));
    vvi on = vvi(1+mx, vi(1+mx, 0));

    void init()
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = i; j <= n; j++)
            {
                if(s[j] == 0) break;
                on[i][j] = 1;
            }
        }
    }

    void switch_off(int i, int t)
    {
        auto it = offpoints.lower_bound(i);
        int r = *it;
        it--;
        int l = *it;

        offpoints.insert(i);

        for(int v = l+1; v <= i; v++)
            for(int w = i; w < r; w++)
            {
                on[v][w] = 0;
                total_time[v][w] += t - toggle_stamp[v][w];
                toggle_stamp[v][w] = t;
            }
    }

    void switch_on(int i, int t)
    {
        offpoints.erase(i);

        auto it = offpoints.lower_bound(i);
        int r = *it;
        it--;
        int l = *it;

        for(int v = l+1; v <= i; v++)
            for(int w = i; w < r; w++)
            {
                on[v][w] = 1;
                toggle_stamp[v][w] = t;
            }
    }

    int query(int l, int r, int t)
    {
        if(on[l][r]) return total_time[l][r] + t - toggle_stamp[l][r];
        else return total_time[l][r];
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> q;

    string t;
    cin >> t;

    for(int i = 0; i < n; i++)
        s[i+1] = (t[i] == '1');

    for(int i = 1; i <= n; i++)
        if(s[i] == 0)
            offpoints.insert(i);

    offpoints.insert(0);
    offpoints.insert(n+1);

    segtree2 S;
    S.init();


    for(int j = 1; j <= q; j++)
    {
        string qr;
        cin >> qr;
        if(qr == "toggle")
        {
            int i;
            cin >> i;

            if(s[i] == 0)
            {
                s[i] = 1;
                S.switch_on(i, j);
            }
            else
            {
                s[i] = 0;
                S.switch_off(i, j);
            }
        }
        else
        {
            int a, b;
            cin >> a >> b;

            cout << S.query(a, b-1, j) << '\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...