Submission #931481

# Submission time Handle Problem Language Result Execution time Memory
931481 2024-02-21T23:09:29 Z Ice_man Street Lamps (APIO19_street_lamps) C++14
0 / 100
145 ms 54836 KB
#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
#include <set>

#define maxn 200005
#define maxlog 20
#define INF 1000000010
#define LINF 1000000000000000005
#define endl '\n'
#define pb(x) push_back(x)
#define X first
#define Y second
#define control cout<<"passed"<<endl;

#pragma GCC optimize("O3" , "Ofast" , "unroll-loops" , "fast-math")
#pragma GCC target("avx2")

using namespace std;

std::chrono::high_resolution_clock::time_point startT, currT;
constexpr double TIME_MULT = 1;

double timePassed()
{
    using namespace std::chrono;
    currT = high_resolution_clock::now();
    double time = duration_cast<duration<double>>(currT - startT).count();
    return time * TIME_MULT;
}

struct que
{
    int type , l , r;

    que(){};
    que(int _type , int _l , int _r)
    {
        type = _type;
        l = _l;
        r = _r;
    }
};

vector <que> queries;

vector <int> v[maxn], fenwick[maxn];
int n, q;

void update(int line, int to, int val)
{
    for(int i = line; i < maxn; i += (i & -i))
    {
        /**if(v[i].size() == 0)
            continue;*/

        auto it = upper_bound(v[i].begin(), v[i].end(), to);

        for(int j = it - v[i].begin(); j <= v[i].size(); j += (j & -j))
            fenwick[i][j] += val;
    }
}


int query(int line, int to)
{
    int ans = 0;

    for(int i = line; i > 0; i -= (i & -i))
    {
        if(v[i].size() == 0)
            continue;

        auto it = upper_bound(v[i].begin(), v[i].end(), to);

        for(int j = it - v[i].begin(); j > 0; j -= (j & -j))
            ans += fenwick[i][j];

    }

    return ans;
}


void add(int line, int to)
{
    for(int i = line; i <= n; i += (i & -i))
        v[i].pb(to);
}

string s;

set <int> zeros;

int xo[maxn], ans[maxn];

void read()
{
    cin >> n >> q;
    cin >> s;

    queries.resize(q + 1);

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

    for(int i = 1; i <= n; i++)
    {
        xo[i] = s[i - 1] - '0';
        if(s[i - 1] == '0')
            zeros.insert(i);
    }

}


pair <int , int> timer[maxn];

void pre_solve()
{
    string type;
    int a , b;

    for(int i = 1; i <= q; i++)
    {
        cin >> type;

        if(type == "toggle")
        {
            cin >> a;

            if(xo[a] == 0)
                zeros.erase(zeros.find(a));
            else
                zeros.insert(a);

            xo[a] ^= 1;

            auto it = zeros.upper_bound(a);
            int pom = *it - 1;

            it--;
            if(*it == a)
                it--;

            int pom2 = *it + 1;

            add(pom2 , a);
            add(pom2 , pom + 1);
            add(a + 1 , a);
            add(a + 1 , pom + 1);

            queries[i] = {0 , pom2 , pom};

            timer[i] = {a , (xo[a]? -i : i)};

            continue;
        }

        cin >> a >> b;
        b--;

        queries[i] = {1 , a , b};
        if(*zeros.lower_bound(a) > b)
            ans[i] += i;

    }

    for(int i = 1; i <= n; i++)
    {
        sort(v[i].begin() , v[i].end());

        v[i].resize(unique(v[i].begin() , v[i].end()) - v[i].begin());
        fenwick[i].resize(v[i].size() + 1);
    }

}


void solve()
{
    int t , l , r;

    for(int i = 1; i <= q; i++)
    {
        t = queries[i].type;
        l = queries[i].l;
        r = queries[i].r;

        if(t == 0)
        {
            update(l , timer[i].X , timer[i].Y);
            update(l , r + 1 , -timer[i].Y);
            update(timer[i].X + 1 , timer[i].X , -timer[i].Y);
            update(timer[i].X + 1 , r + 1 , timer[i].Y);

            continue;
        }

        cout << query(l , r) + ans[i] << endl;
    }
}



int main()
{

    /**#ifdef ONLINE_JUDGE
        freopen("taxi.in", "r", stdin);
        freopen("taxi.out", "w", stdout);
    #endif*/

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    startT = std::chrono::high_resolution_clock::now();

    read();
    pre_solve();
    solve();


    return 0;
}

Compilation message

street_lamps.cpp: In function 'void update(int, int, int)':
street_lamps.cpp:60:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for(int j = it - v[i].begin(); j <= v[i].size(); j += (j & -j))
      |                                        ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 14 ms 25436 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 145 ms 54836 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 21848 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 25692 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 14 ms 25436 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -