Submission #496430

# Submission time Handle Problem Language Result Execution time Memory
496430 2021-12-21T07:56:20 Z dantoh000 Street Lamps (APIO19_street_lamps) C++14
100 / 100
1205 ms 56292 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int n,q;
char str[300005];
struct seg{
    int s, e, t;
    bool operator<(const seg &i)const{
		return s < i.s;
	}
};

struct evt{
    int s, e, t, type;
    bool operator<(const evt &i)const{
		return ii(s,e) < ii(i.s,i.e);
	}
};
int ans[300005];
vector<evt> ev;
set<seg> s;
struct fenwick{
    int a[300005];
    void add(int x, int v){
        while (x < 300005){
            a[x] += v;
            x += x&-x;
        }
    }
    int qu(int x){
        int ret = 0;
        while (x){
            ret += a[x];
            x -= x&-x;
        }
        return ret;
    }



}fw;

void solve(int l, int r){
    if (l == r) return;
    //printf("solving (%d %d)\n",l,r);
    int m = (l+r)/2;
    solve(l,m);
    solve(m+1,r);
    vector<evt> up, qu;
    for (int i = l; i <= m; i++){
        //printf("%d %d %d %d %d\n",i,ev[i].s, ev[i].e, ev[i].t, ev[i].type);
        if (ev[i].type == 1){
            up.push_back(ev[i]);
        }
    }
    for (int i = m+1; i <= r; i++){
        //printf("%d %d %d %d %d\n",i,ev[i].s, ev[i].e, ev[i].t, ev[i].type);
        if (ev[i].type == 2){
            qu.push_back(ev[i]);
        }
    }
    for (auto x : up){
        //printf("update %d %d %d\n",x.s,x.e,x.t);
    }
    for (auto x : qu){
        //printf("query %d %d %d\n",x.s,x.e,x.t);
    }
    sort(up.begin(),up.end());
    sort(qu.begin(),qu.end());
    int i = 0;
    for (auto Q : qu){
        while (i < up.size() && up[i].s <= Q.s){
            fw.add(up[i].e, up[i].t);
            i++;
        }
        ans[Q.t] += fw.qu(Q.e);
    }
    for (int j = 0; j < i; j++){
        fw.add(up[j].e, -up[j].t);
    }
    //printf("end solve (%d %d)\n",l,r);

}
void addrange(int s, int e, int v){
    ev.push_back({s,s,v,1});
    ev.push_back({s,e+1,-v,1});
    ev.push_back({e+1,s,-v,1});
    ev.push_back({e+1,e+1,v,1});
}




int main(){
    scanf("%d%d", &n,&q);
    scanf("%s",str+1);
    for (int i = 1; i <= n; i++){
        if (str[i] == '1'){
            int e = i;
            while (e <= n && str[e] == '1') e++;
            s.insert({i,e-1,0});
            i = e-1;
        }
    }
    for (int i = 0; i < q; i++){
        string Qs;
        cin >> Qs;
        if (Qs == "toggle"){
            int x; scanf("%d",&x);
            if (str[x] == '0'){
                int st = x, ed = x;
                auto it = s.lower_bound({x+1, -1, -1});
                if (it != s.end() && it->s == x+1){
                    addrange(it->s, it->e, i+1 - it->t);
                    ed = it->e;
                    it = s.erase(it);
                }
                if (it != s.begin() && prev(it)->e == x-1){
                    --it;
                    addrange(it->s, it->e, i+1 - it->t);
                    st = it->s;
                    it = s.erase(it);
                }
                s.insert({st,ed,i+1});
            }
            else{
                auto it = --s.lower_bound({x+1, -1, -1});
                addrange(it->s, it->e, i+1 - it->t);
                int S = it->s;
                int E = it->e;
                s.erase(it);
                if (S < x) s.insert({S,x-1,i+1});
                if (x < E) s.insert({x+1,E,i+1});
            }
            str[x] ^= 1;
            ans[i] = -1;
        }
        else{
            int a,b; scanf("%d%d",&a,&b);
            b--;
            ev.push_back({a,b,i,2});
            auto it = s.lower_bound({a+1,-1,-1});
            if (it != s.begin()){
                --it;
                if (it->s <= a && b <= it->e){
                    //printf("found existing range %d %d %d\n",it->s,it->e,it->t);
                    ans[i] += i+1-it->t;
                }
            }
        }
    }
    solve(0, ev.size()-1);
    for (int i = 0; i < q; i++){
        if (ans[i] != -1) printf("%d\n",ans[i]);
    }


}

Compilation message

street_lamps.cpp: In function 'void solve(int, int)':
street_lamps.cpp:62:15: warning: variable 'x' set but not used [-Wunused-but-set-variable]
   62 |     for (auto x : up){
      |               ^
street_lamps.cpp:65:15: warning: variable 'x' set but not used [-Wunused-but-set-variable]
   65 |     for (auto x : qu){
      |               ^
street_lamps.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<evt>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         while (i < up.size() && up[i].s <= Q.s){
      |                ~~^~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     scanf("%d%d", &n,&q);
      |     ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     scanf("%s",str+1);
      |     ~~~~~^~~~~~~~~~~~
street_lamps.cpp:109:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |             int x; scanf("%d",&x);
      |                    ~~~~~^~~~~~~~~
street_lamps.cpp:139:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |             int a,b; scanf("%d%d",&a,&b);
      |                      ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 907 ms 26080 KB Output is correct
2 Correct 865 ms 29660 KB Output is correct
3 Correct 886 ms 30240 KB Output is correct
4 Correct 933 ms 37644 KB Output is correct
5 Correct 1105 ms 40516 KB Output is correct
6 Correct 812 ms 40528 KB Output is correct
7 Correct 369 ms 18844 KB Output is correct
8 Correct 364 ms 18920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 448 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1096 ms 53780 KB Output is correct
6 Correct 1205 ms 53332 KB Output is correct
7 Correct 1062 ms 39836 KB Output is correct
8 Correct 417 ms 18872 KB Output is correct
9 Correct 220 ms 11808 KB Output is correct
10 Correct 269 ms 14800 KB Output is correct
11 Correct 291 ms 14976 KB Output is correct
12 Correct 405 ms 18856 KB Output is correct
13 Correct 404 ms 18848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 448 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 3 ms 444 KB Output is correct
5 Correct 570 ms 25128 KB Output is correct
6 Correct 710 ms 34704 KB Output is correct
7 Correct 841 ms 40488 KB Output is correct
8 Correct 1072 ms 56292 KB Output is correct
9 Correct 875 ms 35544 KB Output is correct
10 Correct 950 ms 48492 KB Output is correct
11 Correct 768 ms 35576 KB Output is correct
12 Correct 877 ms 48632 KB Output is correct
13 Correct 764 ms 35540 KB Output is correct
14 Correct 949 ms 48508 KB Output is correct
15 Correct 432 ms 18800 KB Output is correct
16 Correct 424 ms 18848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 907 ms 26080 KB Output is correct
9 Correct 865 ms 29660 KB Output is correct
10 Correct 886 ms 30240 KB Output is correct
11 Correct 933 ms 37644 KB Output is correct
12 Correct 1105 ms 40516 KB Output is correct
13 Correct 812 ms 40528 KB Output is correct
14 Correct 369 ms 18844 KB Output is correct
15 Correct 364 ms 18920 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 3 ms 460 KB Output is correct
18 Correct 3 ms 448 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1096 ms 53780 KB Output is correct
21 Correct 1205 ms 53332 KB Output is correct
22 Correct 1062 ms 39836 KB Output is correct
23 Correct 417 ms 18872 KB Output is correct
24 Correct 220 ms 11808 KB Output is correct
25 Correct 269 ms 14800 KB Output is correct
26 Correct 291 ms 14976 KB Output is correct
27 Correct 405 ms 18856 KB Output is correct
28 Correct 404 ms 18848 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 448 KB Output is correct
31 Correct 2 ms 432 KB Output is correct
32 Correct 3 ms 444 KB Output is correct
33 Correct 570 ms 25128 KB Output is correct
34 Correct 710 ms 34704 KB Output is correct
35 Correct 841 ms 40488 KB Output is correct
36 Correct 1072 ms 56292 KB Output is correct
37 Correct 875 ms 35544 KB Output is correct
38 Correct 950 ms 48492 KB Output is correct
39 Correct 768 ms 35576 KB Output is correct
40 Correct 877 ms 48632 KB Output is correct
41 Correct 764 ms 35540 KB Output is correct
42 Correct 949 ms 48508 KB Output is correct
43 Correct 432 ms 18800 KB Output is correct
44 Correct 424 ms 18848 KB Output is correct
45 Correct 560 ms 17716 KB Output is correct
46 Correct 586 ms 18276 KB Output is correct
47 Correct 575 ms 21304 KB Output is correct
48 Correct 985 ms 37448 KB Output is correct
49 Correct 251 ms 16160 KB Output is correct
50 Correct 317 ms 16116 KB Output is correct
51 Correct 319 ms 16196 KB Output is correct
52 Correct 250 ms 16564 KB Output is correct
53 Correct 261 ms 16532 KB Output is correct
54 Correct 279 ms 16628 KB Output is correct