Submission #739366

#TimeUsernameProblemLanguageResultExecution timeMemory
739366nguyentunglamStreet Lamps (APIO19_street_lamps)C++17
100 / 100
2414 ms105792 KiB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 3e5 + 10;
int a[N], ans[N];
int n, q;
tuple<int, int, int> query[N];
pair<int, int> tt[N];
vector<int> node[N], bit[N];
void fakeup (int x, int y) {
    for(x; x <= n; x += x & -x) node[x].push_back(y);
}
void up(int x, int yy, int val) {
    for(x; x <= n; x += x & -x) {
        for(int y = upper_bound(node[x].begin(), node[x].end(), yy) - node[x].begin(); y <= node[x].size(); y += y & -y) {
            bit[x][y] += val;
        }
    }
}
int get(int x, int yy) {
    int ret = 0;
    for(x; x; x -= x & -x) {
        for(int y = upper_bound(node[x].begin(), node[x].end(), yy) - node[x].begin(); y; y -= y & -y) {
            ret += bit[x][y];
        }
    }
    return ret;
}
void compress() {
    for(int i = 1; i <= n; i++) {
        sort(node[i].begin(), node[i].end());
        node[i].resize(unique(node[i].begin(), node[i].end()) - node[i].begin());
        bit[i].resize(node[i].size() + 1);
    }
}
void fakeinc(int x, int y, int u, int v) {
    fakeup(x, y);
    fakeup(x, v + 1);
    fakeup(u + 1, y);
    fakeup(u + 1, v + 1);
}
void inc(int x, int y, int u, int v, int val) {
    up(x, y, val);
    up(x, v + 1, -val);
    up(u + 1, y, -val);
    up(u + 1, v + 1, val);
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n >> q;
    string str; cin >> str; str = " " + str;
    set<int> s = {0, n + 1};
    for(int i = 1; i <= n; i++) {
        a[i] = str[i] - '0';
        if (!a[i]) s.insert(i);
    }
    for(int timer = 1; timer <= q; timer++) {
        string ask; cin >> ask;
        if (ask[0] == 't') {
            int i; cin >> i;
            a[i] ^= 1;
            if (a[i]) s.erase(s.find(i));
            else s.insert(i);
            auto it = s.upper_bound(i);
            int r = *it - 1;
            it--;
            if (*it == i) it--;
            int l = *it + 1;
            fakeinc(l, i, i, r);
            query[timer] = {0, l, r};
            tt[timer].fi = i;
            tt[timer].se = a[i] ? -timer : timer;
        }
        else {
            int l, r; cin >> l >> r; r--;
            query[timer] = {1, l, r};
            if (*s.lower_bound(l) > r) ans[timer] += timer;
        }
    }
    compress();
    for(int timer = 1; timer <= q; timer++) {
        int type, l, r; tie(type, l, r) = query[timer];
        if (type == 0) {
            int i, val; tie(i, val) = tt[timer];
            inc(l, i, i, r, val);
        }
        else cout << get(l, r) + ans[timer] << endl;
    }
}

Compilation message (stderr)

street_lamps.cpp: In function 'void fakeup(int, int)':
street_lamps.cpp:14:9: warning: statement has no effect [-Wunused-value]
   14 |     for(x; x <= n; x += x & -x) node[x].push_back(y);
      |         ^
street_lamps.cpp: In function 'void up(int, int, int)':
street_lamps.cpp:17:9: warning: statement has no effect [-Wunused-value]
   17 |     for(x; x <= n; x += x & -x) {
      |         ^
street_lamps.cpp:18:90: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         for(int y = upper_bound(node[x].begin(), node[x].end(), yy) - node[x].begin(); y <= node[x].size(); y += y & -y) {
      |                                                                                        ~~^~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int get(int, int)':
street_lamps.cpp:25:9: warning: statement has no effect [-Wunused-value]
   25 |     for(x; x; x -= x & -x) {
      |         ^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:55:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:56:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:59:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:60:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...