Submission #172193

# Submission time Handle Problem Language Result Execution time Memory
172193 2019-12-31T14:40:43 Z errorgorn Street Lamps (APIO19_street_lamps) C++14
100 / 100
3306 ms 317048 KB
#include <cstdio>
#include <set>
#include <utility>
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;

const int MAXN=300005;

int __CLOCK=0; ///global time
///also __CLOCK is cool variable name :sunglasses:

ii lca(int s,int e,int l,int r,int pos){ ///find lca of 2 nodes
    int m=s+e>>1;
    if (r<=m && m<pos) return ii(s,e);
    else if (pos<=m && m<l) return ii(s,e);
    if (pos<=m) return lca(s,m,l,r,pos);
    else return lca(m+1,e,l,r,pos);
}

struct node{
    int s,e,m;
    int val=0; ///stores the length that section is on on "on and off" sections

    node *l=nullptr,*r=nullptr;

    node(int _s,int _e){
        s=_s,e=_e,m=s+e>>1;
    }

    inline bool inside(int i){
        return s<=i && i<=e;
    }

    void update(int i,int j){
        val+=j;

        if (s==e) return;
        if (i<=m){
            if (l==nullptr) l=new node(i,i);
            else if (!l->inside(i)){
                node *temp=l;
                ii child=lca(s,e,l->s,l->e,i);
                l=new node(child.first,child.second);
                l->val=temp->val;
                if (temp->e<=l->m) l->l=temp;
                else l->r=temp;
            }
            l->update(i,j);
        }
        else{
            if (r==nullptr) r=new node(i,i);
            else if (!r->inside(i)){
                node *temp=r;
                ii child=lca(s,e,r->s,r->e,i);
                r=new node(child.first,child.second);
                r->val=temp->val;
                if (temp->e<=r->m) r->l=temp;
                else r->r=temp;
            }
            r->update(i,j);
        }
    }

    int query(int i,int j){
        if (i<=s && e<=j) return val;
        else if (j<=m) return (l==nullptr)?0:l->query(i,j);
        else if (m<i) return (r==nullptr)?0:r->query(i,j);
        else return ((l==nullptr)?0:l->query(i,j))+((r==nullptr)?0:r->query(i,j));
    }

    node* clone(){ ///cloning seg tree for 2d seg tree
        node* res=new node(s,e);
        res->val=val;
        res->l=(l==nullptr)?nullptr:l->clone();
        res->r=(r==nullptr)?nullptr:r->clone();
        return res;
    }

};

struct node2d{
    int s,e,m;
    node *val;

    node2d *l=nullptr,*r=nullptr;

    node2d(int _s,int _e, node *_val){
        s=_s,e=_e,m=s+e>>1;
        val=_val;
    }

    inline bool inside(int i){
        return s<=i && i<=e;
    }

    void update(int i,int j,int k){
        val->update(j,k);
        if (s==e) return;
        if (i<=m){
            if (l==nullptr) l=new node2d(i,i,new node(0,MAXN));
            else if (!l->inside(i)){
                node2d *temp=l;
                ii child=lca(s,e,l->s,l->e,i);
                l=new node2d(child.first,child.second,temp->val->clone());
                if (temp->e<=l->m) l->l=temp;
                else l->r=temp;
            }
            l->update(i,j,k);
        }
        else{
            if (r==nullptr) r=new node2d(i,i,new node(0,MAXN));
            else if (!r->inside(i)){
                node2d *temp=r;
                ii child=lca(s,e,r->s,r->e,i);
                r=new node2d(child.first,child.second,temp->val->clone());
                if (temp->e<=r->m) r->l=temp;
                else r->r=temp;
            }
            r->update(i,j,k);
        }
    }

    int query(int i,int j,int i2,int j2){
        if (i<=s && e<=j) return val->query(i2,j2);
        else if (j<=m) return (l==nullptr)?0:l->query(i,j,i2,j2);
        else if (m<i) return (r==nullptr)?0:r->query(i,j,i2,j2);
        else return ((l==nullptr)?0:l->query(i,j,i2,j2))+((r==nullptr)?0:r->query(i,j,i2,j2));
    }
}*root=new node2d(0,MAXN,new node(0,MAXN));

int n,q;
char lamps[MAXN];
set<iii> range;

int main(){
    //freopen("input.txt","r",stdin);
    scanf("%d%d",&n,&q);
    char ch;
    ii curr=ii(-1,-1); ///sentinal
    for (int x=1;x<=n;x++){
        scanf(" %c",&ch);
        lamps[x]=ch;
        if (ch=='1'){
            if (curr.first==-1) curr.second=x;
            curr.first=x;
        }
        else{
            if (curr!=ii(-1,-1)){
                range.insert(iii(curr,__CLOCK));
                curr=ii(-1,-1);
            }
        }
    }

    if (curr!=ii(-1,-1)) range.insert(iii(curr,__CLOCK));

    char buf[10];
    set<iii>::iterator it;
    ii temp;
    int a,b;
    while (q--){
        __CLOCK++; ///update global current time
        scanf("%s",&buf);
        if (buf[0]=='q'){ ///query
            scanf("%d%d",&a,&b);
            it=range.lower_bound(iii(ii(b-1,-1),-1));
            if (it==range.end() || (*it).first.second>a) printf("%d\n",root->query(1,a,b-1,MAXN));
            else printf("%d\n",root->query(1,a,b-1,MAXN)+__CLOCK-(*it).second);
            
        }
        else{ ///toggle
            scanf("%d",&a);
            if (lamps[a]=='1'){
                it=range.lower_bound(iii(ii(a,-1),-1));
                temp=(*it).first;
                if (temp.first!=a){
                    range.insert(iii(ii(temp.first,a+1),__CLOCK));
                }
                if (temp.second!=a){
                    range.insert(iii(ii(a-1,temp.second),__CLOCK));
                }
                root->update(temp.second,temp.first,__CLOCK-(*it).second);
                range.erase(it);
                lamps[a]='0';
            }
            else{
                temp=ii(a,a);
                if (lamps[a+1]=='1'){
                    it=range.lower_bound(iii(ii(a+1,-1),-1));
                    temp.first=(*it).first.first;
                    root->update((*it).first.second,(*it).first.first,__CLOCK-(*it).second);
                    range.erase(it);
                }
                if (lamps[a-1]=='1'){
                    it=range.lower_bound(iii(ii(a-1,-1),-1));
                    temp.second=(*it).first.second;
                    root->update((*it).first.second,(*it).first.first,__CLOCK-(*it).second);
                    range.erase(it);
                }
                range.insert(iii(temp,__CLOCK));
                lamps[a]='1';
            }
        }
    }
}

Compilation message

street_lamps.cpp: In function 'ii lca(int, int, int, int, int)':
street_lamps.cpp:14:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int m=s+e>>1;
           ~^~
street_lamps.cpp: In constructor 'node::node(int, int)':
street_lamps.cpp:28:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         s=_s,e=_e,m=s+e>>1;
                     ~^~
street_lamps.cpp: In constructor 'node2d::node2d(int, int, node*)':
street_lamps.cpp:89:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         s=_s,e=_e,m=s+e>>1;
                     ~^~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:164:24: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[10]' [-Wformat=]
         scanf("%s",&buf);
                    ~~~~^
street_lamps.cpp:138:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&q);
     ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:142:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %c",&ch);
         ~~~~~^~~~~~~~~~~
street_lamps.cpp:164:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s",&buf);
         ~~~~~^~~~~~~~~~~
street_lamps.cpp:166:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d",&a,&b);
             ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:173:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d",&a);
             ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 252 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 283 ms 4856 KB Output is correct
2 Correct 411 ms 5620 KB Output is correct
3 Correct 953 ms 18172 KB Output is correct
4 Correct 2414 ms 190584 KB Output is correct
5 Correct 2459 ms 203384 KB Output is correct
6 Correct 2526 ms 216532 KB Output is correct
7 Correct 160 ms 2808 KB Output is correct
8 Correct 167 ms 4364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 988 KB Output is correct
2 Correct 5 ms 1016 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3045 ms 317048 KB Output is correct
6 Correct 2992 ms 288908 KB Output is correct
7 Correct 2264 ms 202520 KB Output is correct
8 Correct 167 ms 8540 KB Output is correct
9 Correct 116 ms 4080 KB Output is correct
10 Correct 124 ms 4364 KB Output is correct
11 Correct 125 ms 4472 KB Output is correct
12 Correct 161 ms 7160 KB Output is correct
13 Correct 168 ms 8712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 5 ms 1000 KB Output is correct
5 Correct 573 ms 10828 KB Output is correct
6 Correct 1622 ms 125344 KB Output is correct
7 Correct 2357 ms 213176 KB Output is correct
8 Correct 3306 ms 303644 KB Output is correct
9 Correct 442 ms 5084 KB Output is correct
10 Correct 304 ms 3700 KB Output is correct
11 Correct 443 ms 5144 KB Output is correct
12 Correct 308 ms 3576 KB Output is correct
13 Correct 439 ms 5188 KB Output is correct
14 Correct 320 ms 3684 KB Output is correct
15 Correct 163 ms 7316 KB Output is correct
16 Correct 171 ms 8568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 252 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 283 ms 4856 KB Output is correct
9 Correct 411 ms 5620 KB Output is correct
10 Correct 953 ms 18172 KB Output is correct
11 Correct 2414 ms 190584 KB Output is correct
12 Correct 2459 ms 203384 KB Output is correct
13 Correct 2526 ms 216532 KB Output is correct
14 Correct 160 ms 2808 KB Output is correct
15 Correct 167 ms 4364 KB Output is correct
16 Correct 5 ms 988 KB Output is correct
17 Correct 5 ms 1016 KB Output is correct
18 Correct 4 ms 760 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 3045 ms 317048 KB Output is correct
21 Correct 2992 ms 288908 KB Output is correct
22 Correct 2264 ms 202520 KB Output is correct
23 Correct 167 ms 8540 KB Output is correct
24 Correct 116 ms 4080 KB Output is correct
25 Correct 124 ms 4364 KB Output is correct
26 Correct 125 ms 4472 KB Output is correct
27 Correct 161 ms 7160 KB Output is correct
28 Correct 168 ms 8712 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 4 ms 504 KB Output is correct
31 Correct 4 ms 760 KB Output is correct
32 Correct 5 ms 1000 KB Output is correct
33 Correct 573 ms 10828 KB Output is correct
34 Correct 1622 ms 125344 KB Output is correct
35 Correct 2357 ms 213176 KB Output is correct
36 Correct 3306 ms 303644 KB Output is correct
37 Correct 442 ms 5084 KB Output is correct
38 Correct 304 ms 3700 KB Output is correct
39 Correct 443 ms 5144 KB Output is correct
40 Correct 308 ms 3576 KB Output is correct
41 Correct 439 ms 5188 KB Output is correct
42 Correct 320 ms 3684 KB Output is correct
43 Correct 163 ms 7316 KB Output is correct
44 Correct 171 ms 8568 KB Output is correct
45 Correct 104 ms 2736 KB Output is correct
46 Correct 181 ms 2936 KB Output is correct
47 Correct 1264 ms 102768 KB Output is correct
48 Correct 2173 ms 190192 KB Output is correct
49 Correct 135 ms 4396 KB Output is correct
50 Correct 138 ms 4472 KB Output is correct
51 Correct 134 ms 4756 KB Output is correct
52 Correct 124 ms 4856 KB Output is correct
53 Correct 122 ms 4856 KB Output is correct
54 Correct 126 ms 4920 KB Output is correct