Submission #501544

# Submission time Handle Problem Language Result Execution time Memory
501544 2022-01-03T21:33:47 Z gozonite Street Lamps (APIO19_street_lamps) C++14
100 / 100
4360 ms 93648 KB
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <deque>
#include <queue>
#include <tuple>
#include <cmath>
#include <cctype>
#include <stack>
#include <cassert>
using namespace std;
using ll = long long;

int n, q;
string s;
pair<int, int> inp[300001];
vector<pair<int, int> > pts;
bool cmpy(const pair<int, int>& a, const pair<int, int>& b) { return a.second < b.second; }
vector<int> val[300002], bit[300002];

void addv(int x, int y, int v) {
    for (; x <= n; x += x&-x) {
        for (int i = lower_bound(val[x].begin(), val[x].end(), y)-val[x].begin(); i < bit[x].size(); i += i&-i) {
            bit[x][i] += v;
        }
    }
}
int sumr(int x, int y) {
    int s = 0; // problem? ll
    for (; x >= 1; x -= x&-x) {
        for (int i = upper_bound(val[x].begin(), val[x].end(), y)-val[x].begin()-1; i >= 1; i -= i&-i) {
            s += bit[x][i];
        }
    }
    return s;
}
void addr(int fx, int sx, int fy, int sy, int v) {
    addv(fx, fy, v);
    addv(fx, sy+1, -v);
    addv(sx+1, fy, -v);
    addv(sx+1, sy+1, v);
}

int main() {
    cin >> n >> q >> s;
    s = " " + s;
    for (int i = 1; i <= q; i++) {
        string t; cin >> t;
        if (t == "query") {
            cin >> inp[i].first >> inp[i].second;
            inp[i].second--;
        } else {
            inp[i].first = -1;
            cin >> inp[i].second;
        }
    }
    set<int> off;
    off.insert(0); off.insert(n+1);
    for (int i = 1; i <= n; i++)
        if (s[i] == '0') off.insert(i);
    auto it = off.begin();
    while (*it != n+1) {
        int fv = *it+1; it++; int sv = *it - 1;
        if (fv > sv) continue;
        pts.push_back({fv, fv});
        pts.push_back({fv, sv+1});
        pts.push_back({sv+1, fv});
        pts.push_back({sv+1, sv+1});
    }
    for (int i = 1; i <= q; i++) {
        if (inp[i].first != -1) continue;
        if (off.find(inp[i].second) == off.end()) {
            it = off.lower_bound(inp[i].second);
            int sr = *it - 1; it--;  int fl = *it + 1;
            int fr = inp[i].second+1, sl = inp[i].second-1;
            pts.push_back({fl, fl});
            pts.push_back({fl, sl+1});
            pts.push_back({sl+1, fl});
            pts.push_back({sl+1, sl+1});

            pts.push_back({fr, fr});
            pts.push_back({fr, sr+1});
            pts.push_back({sr+1, fr});
            pts.push_back({sr+1, sr+1});

            off.insert(inp[i].second);
        } else {
            off.erase(inp[i].second);
            it = off.upper_bound(inp[i].second);
            int r = *it-1; it--; int l = *it+1;
            pts.push_back({l, l});
            pts.push_back({l, r+1});
            pts.push_back({r+1, l});
            pts.push_back({r+1, r+1});
        }
    }
    sort(pts.begin(), pts.end(), cmpy);
    for (int i = 1; i <= n; i++) val[i].push_back(0);
    for (auto pp : pts) {
        int x = pp.first, y = pp.second;
        for (; x <= n; x += x&-x) {
            if (val[x].back() != y) val[x].push_back(y);
        }
    }
    for (int i = 1; i <= n; i++) bit[i].resize(val[i].size(), 0);

    off.clear();
    off.insert(0); off.insert(n+1);
    for (int i = 1; i <= n; i++) {
        if (s[i] == '0') off.insert(i);
    }
    it = off.begin();
    while (*it != n+1) {
        int l = *it+1; it++; int r = *it-1;
        addr(l, r, l, r, q);
    }
    for (int i = 1; i <= q; i++) {
        if (inp[i].first == -1) {
            if (off.find(inp[i].second) == off.end()) {
                it = off.upper_bound(inp[i].second);
                int r = *it-1; it--; int l = *it+1;
                addr(l, r, l, r, -(q-i));
                int fm = inp[i].second-1, sm = inp[i].second+1;
                addr(l, fm, l, fm, q-i);
                addr(sm, r, sm, r, q-i);
                off.insert(inp[i].second);
            } else {
                off.erase(inp[i].second);
                it = off.upper_bound(inp[i].second);
                int r = *it-1; it--; int l = *it+1;
                int fm = inp[i].second-1, sm = inp[i].second+1;
                addr(l, fm, l, fm, -(q-i));
                addr(sm, r, sm, r, -(q-i));
                addr(l, r, l, r, q-i);
            }
        } else {
            int a = inp[i].first, b = inp[i].second;
            int ans = sumr(a, b);
            if (off.lower_bound(a) == off.upper_bound(b)) ans -= q-i;
            cout << ans << endl;
        }
    }

    return 0;
}

Compilation message

street_lamps.cpp: In function 'void addv(int, int, int)':
street_lamps.cpp:33:85: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         for (int i = lower_bound(val[x].begin(), val[x].end(), y)-val[x].begin(); i < bit[x].size(); i += i&-i) {
      |                                                                                   ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14400 KB Output is correct
2 Correct 7 ms 14272 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 10 ms 14372 KB Output is correct
5 Correct 8 ms 14396 KB Output is correct
6 Correct 9 ms 14288 KB Output is correct
7 Correct 7 ms 14284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 682 ms 28324 KB Output is correct
2 Correct 858 ms 28460 KB Output is correct
3 Correct 1176 ms 30192 KB Output is correct
4 Correct 2805 ms 77680 KB Output is correct
5 Correct 2511 ms 72556 KB Output is correct
6 Correct 2971 ms 79804 KB Output is correct
7 Correct 1140 ms 56432 KB Output is correct
8 Correct 681 ms 43692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14532 KB Output is correct
2 Correct 11 ms 14540 KB Output is correct
3 Correct 10 ms 14536 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 4360 ms 93648 KB Output is correct
6 Correct 3551 ms 84588 KB Output is correct
7 Correct 2559 ms 72020 KB Output is correct
8 Correct 706 ms 43744 KB Output is correct
9 Correct 455 ms 19864 KB Output is correct
10 Correct 533 ms 20448 KB Output is correct
11 Correct 499 ms 20744 KB Output is correct
12 Correct 1252 ms 56492 KB Output is correct
13 Correct 706 ms 43652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14540 KB Output is correct
2 Correct 12 ms 14536 KB Output is correct
3 Correct 10 ms 14536 KB Output is correct
4 Correct 13 ms 14512 KB Output is correct
5 Correct 1470 ms 63992 KB Output is correct
6 Correct 2221 ms 72480 KB Output is correct
7 Correct 2966 ms 79276 KB Output is correct
8 Correct 3980 ms 87436 KB Output is correct
9 Correct 862 ms 36600 KB Output is correct
10 Correct 736 ms 36196 KB Output is correct
11 Correct 882 ms 36532 KB Output is correct
12 Correct 689 ms 36212 KB Output is correct
13 Correct 936 ms 36604 KB Output is correct
14 Correct 695 ms 36136 KB Output is correct
15 Correct 1314 ms 56432 KB Output is correct
16 Correct 708 ms 43660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14400 KB Output is correct
2 Correct 7 ms 14272 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 10 ms 14372 KB Output is correct
5 Correct 8 ms 14396 KB Output is correct
6 Correct 9 ms 14288 KB Output is correct
7 Correct 7 ms 14284 KB Output is correct
8 Correct 682 ms 28324 KB Output is correct
9 Correct 858 ms 28460 KB Output is correct
10 Correct 1176 ms 30192 KB Output is correct
11 Correct 2805 ms 77680 KB Output is correct
12 Correct 2511 ms 72556 KB Output is correct
13 Correct 2971 ms 79804 KB Output is correct
14 Correct 1140 ms 56432 KB Output is correct
15 Correct 681 ms 43692 KB Output is correct
16 Correct 12 ms 14532 KB Output is correct
17 Correct 11 ms 14540 KB Output is correct
18 Correct 10 ms 14536 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 4360 ms 93648 KB Output is correct
21 Correct 3551 ms 84588 KB Output is correct
22 Correct 2559 ms 72020 KB Output is correct
23 Correct 706 ms 43744 KB Output is correct
24 Correct 455 ms 19864 KB Output is correct
25 Correct 533 ms 20448 KB Output is correct
26 Correct 499 ms 20744 KB Output is correct
27 Correct 1252 ms 56492 KB Output is correct
28 Correct 706 ms 43652 KB Output is correct
29 Correct 9 ms 14540 KB Output is correct
30 Correct 12 ms 14536 KB Output is correct
31 Correct 10 ms 14536 KB Output is correct
32 Correct 13 ms 14512 KB Output is correct
33 Correct 1470 ms 63992 KB Output is correct
34 Correct 2221 ms 72480 KB Output is correct
35 Correct 2966 ms 79276 KB Output is correct
36 Correct 3980 ms 87436 KB Output is correct
37 Correct 862 ms 36600 KB Output is correct
38 Correct 736 ms 36196 KB Output is correct
39 Correct 882 ms 36532 KB Output is correct
40 Correct 689 ms 36212 KB Output is correct
41 Correct 936 ms 36604 KB Output is correct
42 Correct 695 ms 36136 KB Output is correct
43 Correct 1314 ms 56432 KB Output is correct
44 Correct 708 ms 43660 KB Output is correct
45 Correct 347 ms 26148 KB Output is correct
46 Correct 423 ms 26416 KB Output is correct
47 Correct 1376 ms 40688 KB Output is correct
48 Correct 2881 ms 77228 KB Output is correct
49 Correct 507 ms 20860 KB Output is correct
50 Correct 543 ms 20696 KB Output is correct
51 Correct 530 ms 21000 KB Output is correct
52 Correct 610 ms 21340 KB Output is correct
53 Correct 567 ms 21200 KB Output is correct
54 Correct 579 ms 21364 KB Output is correct