Submission #232784

# Submission time Handle Problem Language Result Execution time Memory
232784 2020-05-18T06:08:27 Z Mercenary Street Lamps (APIO19_street_lamps) C++14
100 / 100
1391 ms 70532 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const int maxn = 3e5 + 5;
const int logn = log2(maxn) + 1;

struct BIT{
    vector<int> val, bit;
    BIT(){val.clear();bit.clear();}
    void init(){
        sort(val.begin(),val.end());
        val.erase(unique(val.begin(),val.end()),val.end());
        bit.resize(val.size() , 0);
    }
    void update(int x , int delta){
        for( ; x <= val.size() ; x += x & -x){
            bit[x - 1] += delta;
        }
    }
    void update_range(int l , int h , int delta){
        if(val.size() == 0)return;
        update(lower_bound(val.begin(),val.end(),l)-val.begin()+1 , delta);
        update(upper_bound(val.begin(),val.end(),h)-val.begin()+1, -delta);
    }
    int query(int x){
        int res = 0;
        for( x = upper_bound(val.begin(),val.end(),x)-val.begin() ; x > 0 ; x &= x - 1){
            res += bit[x - 1];
        }
        return res;
    }
}BIT2d[maxn];

int type[maxn] , a[maxn] , b[maxn];
bool state[maxn];
int n , q;

void fakeupdate(int a , int b){
    for( ; a > 0 ; a &= a - 1){
        BIT2d[a].val.pb(b);
    }
}

void update(int l , int h , int L , int H , int delta){
    for( ; l <= n ; l += l & -l){
        BIT2d[l].update_range(L,H,delta);
    }
    for(++h ; h <= n ; h += h & -h){
        BIT2d[h].update_range(L,H,-delta);
    }
}

int query(int a , int b){
    int res = 0;
    for( ; a > 0 ; a &= a - 1){
        res += BIT2d[a].query(b);
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen(taskname".INP","r")){
		freopen(taskname".INP", "r",stdin);
		freopen(taskname".OUT", "w",stdout);
    }
    cin >> n >> q;
    string s;cin >> s;
    set<int> offset;offset.insert(n + 1);offset.insert(0);
    for(int i = 1 ; i <= n ; ++i){
        state[i] = (s[i - 1] == '1');
        if(!state[i])offset.insert(i);
    }
    for(int i = 1 ; i <= q ; ++i){
        cin >> s;
        if(s[0] == 'q'){
            type[i] = 0;
            cin >> a[i] >> b[i];
            fakeupdate(a[i],--b[i]);
        }else{
            type[i] = 1;
            cin >> a[i];
        }
    }
    for(int i = 1 ; i <= n ; ++i){
        BIT2d[i].init();
    }
    for(int i = 1 ; i <= q ; ++i){
        if(type[i] == 0){
            int res = query(a[i] , b[i]);
            auto it = offset.lower_bound(a[i]);
            if(*prev(it) < a[i] && (*it) > b[i])res += i;
            cout << res << '\n';
        }else{
            int x = a[i];
            if(state[x] == 0){//merge seq
                offset.erase(x);
                auto it = offset.lower_bound(x);
                update(*prev(it) + 1 , x , x, (*it) - 1 , -i);
            }else{//split seq
                auto it = offset.lower_bound(x);
                update(*prev(it) + 1 , x , x, (*it) - 1 , i);
//                cout << *prev(it) + 1 << " " << x << " " << (*it) - 1 << endl;
                offset.insert(x);
            }
            state[x] ^= 1;
        }
    }
}

Compilation message

street_lamps.cpp: In member function 'void BIT::update(int, int)':
street_lamps.cpp:29:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for( ; x <= val.size() ; x += x & -x){
                ~~^~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:79:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".INP", "r",stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:80:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen(taskname".OUT", "w",stdout);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 21232 KB Output is correct
2 Correct 220 ms 25080 KB Output is correct
3 Correct 375 ms 27508 KB Output is correct
4 Correct 1193 ms 50700 KB Output is correct
5 Correct 1153 ms 52996 KB Output is correct
6 Correct 1085 ms 46728 KB Output is correct
7 Correct 1391 ms 70016 KB Output is correct
8 Correct 993 ms 57472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 14 ms 14592 KB Output is correct
3 Correct 13 ms 14592 KB Output is correct
4 Correct 15 ms 14592 KB Output is correct
5 Correct 668 ms 37772 KB Output is correct
6 Correct 1039 ms 46220 KB Output is correct
7 Correct 1165 ms 51468 KB Output is correct
8 Correct 978 ms 57860 KB Output is correct
9 Correct 140 ms 24688 KB Output is correct
10 Correct 150 ms 25320 KB Output is correct
11 Correct 160 ms 25572 KB Output is correct
12 Correct 1342 ms 70532 KB Output is correct
13 Correct 1002 ms 58116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14592 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14592 KB Output is correct
5 Correct 1261 ms 63236 KB Output is correct
6 Correct 1214 ms 55308 KB Output is correct
7 Correct 1087 ms 46092 KB Output is correct
8 Correct 611 ms 29580 KB Output is correct
9 Correct 264 ms 22520 KB Output is correct
10 Correct 202 ms 19868 KB Output is correct
11 Correct 270 ms 22648 KB Output is correct
12 Correct 187 ms 19832 KB Output is correct
13 Correct 270 ms 22520 KB Output is correct
14 Correct 194 ms 19832 KB Output is correct
15 Correct 1384 ms 70532 KB Output is correct
16 Correct 1016 ms 57932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 13 ms 14464 KB Output is correct
6 Correct 13 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 180 ms 21232 KB Output is correct
9 Correct 220 ms 25080 KB Output is correct
10 Correct 375 ms 27508 KB Output is correct
11 Correct 1193 ms 50700 KB Output is correct
12 Correct 1153 ms 52996 KB Output is correct
13 Correct 1085 ms 46728 KB Output is correct
14 Correct 1391 ms 70016 KB Output is correct
15 Correct 993 ms 57472 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 14 ms 14592 KB Output is correct
18 Correct 13 ms 14592 KB Output is correct
19 Correct 15 ms 14592 KB Output is correct
20 Correct 668 ms 37772 KB Output is correct
21 Correct 1039 ms 46220 KB Output is correct
22 Correct 1165 ms 51468 KB Output is correct
23 Correct 978 ms 57860 KB Output is correct
24 Correct 140 ms 24688 KB Output is correct
25 Correct 150 ms 25320 KB Output is correct
26 Correct 160 ms 25572 KB Output is correct
27 Correct 1342 ms 70532 KB Output is correct
28 Correct 1002 ms 58116 KB Output is correct
29 Correct 13 ms 14592 KB Output is correct
30 Correct 13 ms 14592 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 13 ms 14592 KB Output is correct
33 Correct 1261 ms 63236 KB Output is correct
34 Correct 1214 ms 55308 KB Output is correct
35 Correct 1087 ms 46092 KB Output is correct
36 Correct 611 ms 29580 KB Output is correct
37 Correct 264 ms 22520 KB Output is correct
38 Correct 202 ms 19868 KB Output is correct
39 Correct 270 ms 22648 KB Output is correct
40 Correct 187 ms 19832 KB Output is correct
41 Correct 270 ms 22520 KB Output is correct
42 Correct 194 ms 19832 KB Output is correct
43 Correct 1384 ms 70532 KB Output is correct
44 Correct 1016 ms 57932 KB Output is correct
45 Correct 102 ms 20084 KB Output is correct
46 Correct 151 ms 20616 KB Output is correct
47 Correct 627 ms 32508 KB Output is correct
48 Correct 1197 ms 49616 KB Output is correct
49 Correct 187 ms 26344 KB Output is correct
50 Correct 177 ms 26204 KB Output is correct
51 Correct 192 ms 26744 KB Output is correct
52 Correct 212 ms 28700 KB Output is correct
53 Correct 214 ms 28776 KB Output is correct
54 Correct 213 ms 28772 KB Output is correct