Submission #405208

# Submission time Handle Problem Language Result Execution time Memory
405208 2021-05-15T22:02:31 Z gevacrt Street Lamps (APIO19_street_lamps) C++17
100 / 100
4170 ms 293188 KB
#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

#define INF INT_MAX
#define MOD 1000000007
#define all(x) x.begin(), x.end()

namespace CC {
    set<ii> S;
    void merge(int x){ // merge x-1 and x
        auto r = S.lower_bound({x, x}), l = prev(r);

        S.insert({l->first, r->second});
        S.erase(l); S.erase(r);
    }

    void unmerge(int x){ // unmerge x-1 and x
        auto it = prev(S.lower_bound({x, x}));
        S.insert({it->first, x-1}); S.insert({x, it->second});
        S.erase(it);
    }

    ii query(int x){
        auto it = prev(S.lower_bound({x+1, x}));
        return {it->first, it->second};
    }

    void init(int N){
        for(int x = 1; x <= N; x++)
            S.insert({x, x});
    }
};

namespace BIT {
    gp_hash_table<int, int> arr[300010];
    int N;

    void update(int _x, int _y, int val){
        for(int x = _x; x <= N; x += x&-x)
            for(int y = _y; y <= N; y += y&-y)
                arr[x][y] += val;
    }

    int query(int _x, int _y){
        int ans = 0;
        for(int x = _x; x; x -= x&-x)
            for(int y = _y; y; y -= y&-y)
                if(arr[x].find(y) != arr[x].end())
                    ans += arr[x][y];
        return ans;
    }

    void update(int x1, int y1, int x2, int y2, int val){
        update(x1, y1, val); update(x2+1, y2+1, val); 
        update(x1, y2+1, -val); update(x2+1, y1, -val); 
    }
};

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int N, Q; cin >> N >> Q;
    BIT::N = N+10; CC::init(N+1); 


    vi isOn(N+1);
    for(int x = 1; x <= N; x++){
        char c; cin >> c;
        if(c == '1')
            CC::merge(x+1), isOn[x] = 1;
    }

    for(int q = 1; q <= Q; q++){
        string s; cin >> s;
        if(s == "toggle"){
            int x; cin >> x;
            auto l = CC::query(x).first, r = CC::query(x+1).second;
            
            if(isOn[x] == 1){
                BIT::update(l, x+1, x, r, q);
                CC::unmerge(x+1);
            }else{
                BIT::update(l, x+1, x, r, -q);
                CC::merge(x+1);
            }
            isOn[x] ^= 1;

        }else{
            int a, b; cin >> a >> b;
            int v = BIT::query(a,b);
            if(CC::query(a) == CC::query(b)) v += q;
            cout << v << "\n";
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 66 ms 63636 KB Output is correct
2 Correct 67 ms 63704 KB Output is correct
3 Correct 61 ms 63620 KB Output is correct
4 Correct 60 ms 63832 KB Output is correct
5 Correct 60 ms 63692 KB Output is correct
6 Correct 64 ms 63644 KB Output is correct
7 Correct 63 ms 63708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 64708 KB Output is correct
2 Correct 402 ms 64996 KB Output is correct
3 Correct 771 ms 68608 KB Output is correct
4 Correct 3168 ms 218480 KB Output is correct
5 Correct 2889 ms 218744 KB Output is correct
6 Correct 3211 ms 225108 KB Output is correct
7 Correct 1035 ms 79644 KB Output is correct
8 Correct 658 ms 81064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 64124 KB Output is correct
2 Correct 69 ms 64012 KB Output is correct
3 Correct 71 ms 63960 KB Output is correct
4 Correct 78 ms 63764 KB Output is correct
5 Correct 4170 ms 293188 KB Output is correct
6 Correct 3586 ms 253748 KB Output is correct
7 Correct 3144 ms 219088 KB Output is correct
8 Correct 644 ms 82116 KB Output is correct
9 Correct 183 ms 65700 KB Output is correct
10 Correct 193 ms 65824 KB Output is correct
11 Correct 183 ms 65804 KB Output is correct
12 Correct 1147 ms 80844 KB Output is correct
13 Correct 664 ms 82120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 63656 KB Output is correct
2 Correct 80 ms 63884 KB Output is correct
3 Correct 70 ms 63916 KB Output is correct
4 Correct 69 ms 64036 KB Output is correct
5 Correct 1641 ms 87888 KB Output is correct
6 Correct 2850 ms 199816 KB Output is correct
7 Correct 3475 ms 224856 KB Output is correct
8 Correct 3837 ms 242884 KB Output is correct
9 Correct 454 ms 64216 KB Output is correct
10 Correct 367 ms 63840 KB Output is correct
11 Correct 459 ms 64180 KB Output is correct
12 Correct 361 ms 63812 KB Output is correct
13 Correct 454 ms 64292 KB Output is correct
14 Correct 377 ms 63824 KB Output is correct
15 Correct 1119 ms 79568 KB Output is correct
16 Correct 662 ms 80912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 63636 KB Output is correct
2 Correct 67 ms 63704 KB Output is correct
3 Correct 61 ms 63620 KB Output is correct
4 Correct 60 ms 63832 KB Output is correct
5 Correct 60 ms 63692 KB Output is correct
6 Correct 64 ms 63644 KB Output is correct
7 Correct 63 ms 63708 KB Output is correct
8 Correct 304 ms 64708 KB Output is correct
9 Correct 402 ms 64996 KB Output is correct
10 Correct 771 ms 68608 KB Output is correct
11 Correct 3168 ms 218480 KB Output is correct
12 Correct 2889 ms 218744 KB Output is correct
13 Correct 3211 ms 225108 KB Output is correct
14 Correct 1035 ms 79644 KB Output is correct
15 Correct 658 ms 81064 KB Output is correct
16 Correct 68 ms 64124 KB Output is correct
17 Correct 69 ms 64012 KB Output is correct
18 Correct 71 ms 63960 KB Output is correct
19 Correct 78 ms 63764 KB Output is correct
20 Correct 4170 ms 293188 KB Output is correct
21 Correct 3586 ms 253748 KB Output is correct
22 Correct 3144 ms 219088 KB Output is correct
23 Correct 644 ms 82116 KB Output is correct
24 Correct 183 ms 65700 KB Output is correct
25 Correct 193 ms 65824 KB Output is correct
26 Correct 183 ms 65804 KB Output is correct
27 Correct 1147 ms 80844 KB Output is correct
28 Correct 664 ms 82120 KB Output is correct
29 Correct 62 ms 63656 KB Output is correct
30 Correct 80 ms 63884 KB Output is correct
31 Correct 70 ms 63916 KB Output is correct
32 Correct 69 ms 64036 KB Output is correct
33 Correct 1641 ms 87888 KB Output is correct
34 Correct 2850 ms 199816 KB Output is correct
35 Correct 3475 ms 224856 KB Output is correct
36 Correct 3837 ms 242884 KB Output is correct
37 Correct 454 ms 64216 KB Output is correct
38 Correct 367 ms 63840 KB Output is correct
39 Correct 459 ms 64180 KB Output is correct
40 Correct 361 ms 63812 KB Output is correct
41 Correct 454 ms 64292 KB Output is correct
42 Correct 377 ms 63824 KB Output is correct
43 Correct 1119 ms 79568 KB Output is correct
44 Correct 662 ms 80912 KB Output is correct
45 Correct 174 ms 66096 KB Output is correct
46 Correct 232 ms 66248 KB Output is correct
47 Correct 1547 ms 121012 KB Output is correct
48 Correct 3196 ms 223192 KB Output is correct
49 Correct 209 ms 67752 KB Output is correct
50 Correct 206 ms 67772 KB Output is correct
51 Correct 234 ms 68028 KB Output is correct
52 Correct 221 ms 68408 KB Output is correct
53 Correct 252 ms 68252 KB Output is correct
54 Correct 213 ms 68272 KB Output is correct