Submission #973467

# Submission time Handle Problem Language Result Execution time Memory
973467 2024-05-02T04:20:09 Z simona1230 Street Lamps (APIO19_street_lamps) C++17
100 / 100
4964 ms 324132 KB
#include <bits/stdc++.h>

using namespace std;
const int maxn=3*1e5+5;
int n,q;
struct node
{
    int l,r;
    int val;
    node() {}
    node(int _l,int _r,int v)
    {
        l=_l;
        r=_r;
        val=v;
    }
};

struct segmentTree
{
    vector<node> v= {{-1,-1,0}};

    void makeLeft(int i)
    {
        v[i].l=v.size();
        v.push_back({-1,-1,0});
    }

    void makeRight(int i)
    {
        v[i].r=v.size();
        v.push_back({-1,-1,0});
    }

    int query(int i,int l,int r,int ql,int qr)
    {
        if(ql>qr||i==-1)return 0;
        if(ql<=l&&r<=qr)return v[i].val;

        int m=(l+r)/2;
        return query(v[i].l,l,m,ql,min(qr,m))+query(v[i].r,m+1,r,max(ql,m+1),qr);
    }

    void update(int i,int l,int r,int idx,int val)
    {
        //cout<<l<<" nml "<<r<<endl;
        if(l==r)
        {
            if(val>0)v[i].val+=val;
            else v[i].val=-val;
            return;
        }

        int lf=v[i].l,rt=v[i].r;
        int m=(l+r)/2;
        if(idx<=m)
        {
            if(lf==-1)makeLeft(i);
            lf=v[i].l;
            update(lf,l,m,idx,val);
        }
        else
        {
            if(rt==-1)makeRight(i);
            rt=v[i].r;
            update(rt,m+1,r,idx,val);
        }

        if(lf==-1)v[i].val=v[rt].val;
        else if(rt==-1)v[i].val=v[lf].val;
        else v[i].val=v[lf].val+v[rt].val;
    }
};

struct twoDnode
{
    segmentTree s;
    int l,r;
    twoDnode() {}
    twoDnode(segmentTree _s,int _l,int _r)
    {
        s=_s;
        l=_l;
        r=_r;
    }
};

struct twoD
{
    segmentTree g;
    vector<twoDnode> v= {{g,-1,-1}};


    void makeLeft(int i)
    {
        v[i].l=v.size();
        v.push_back({g,-1,-1});
    }

    void makeRight(int i)
    {
        v[i].r=v.size();
        v.push_back({g,-1,-1});
    }

    void update(int i,int l,int r,int idx,int val,int other)
    {
        //cout<<l<<" 2d "<<r<<endl;
        if(l==r)
        {
            v[i].s.update(0,1,n,other,val);
            return;
        }

        int lf=v[i].l,rt=v[i].r;
        //cout<<lf<<" "<<rt<<endl;
        int m=(l+r)/2;
        if(idx<=m)
        {
            if(lf==-1)makeLeft(i);
            lf=v[i].l;
            update(lf,l,m,idx,val,other);
        }
        else
        {
            if(rt==-1)makeRight(i);
            rt=v[i].r;
            update(rt,m+1,r,idx,val,other);
        }

        int val1=0;
        int val2=0;

        if(lf!=-1)
            val1=v[lf].s.query(0,1,n,other,other);

        if(rt!=-1)
            val2=v[rt].s.query(0,1,n,other,other);

        int curr=val1+val2;
        v[i].s.update(0,1,n,other,-curr);
    }

    int query(int i,int l,int r,int ql,int qr,int h1,int h2)
    {
        if(ql>qr||i==-1)return 0;
        if(ql<=l&&r<=qr)return v[i].s.query(0,1,n,h1,h2);

        int lf=v[i].l,rt=v[i].r;
        int m=(l+r)/2;
        return query(lf,l,m,ql,min(qr,m),h1,h2)+query(rt,m+1,r,max(m+1,ql),qr,h1,h2);
    }
};

twoD s;
set<int> itv;
int l[maxn],r[maxn],tmr[maxn];
char c[maxn];
int t[4*maxn];

void update(int i,int l,int r,int idx,int val)
{
    if(l==r)
    {
        if(val==0)t[i]=l;
        else t[i]=n+1;
        return;
    }

    int m=(l+r)/2;
    if(idx<=m)update(i*2,l,m,idx,val);
    else update(i*2+1,m+1,r,idx,val);

    t[i]=min(t[i*2],t[i*2+1]);
}

int previous(int i,int l,int r,int idx)
{
    //cout<<l<<" "<<r<<" "<<t[i]<<endl;
    //cout<<l<<" "<<r<<" "<<t[i]<<endl;
    if(l==r)return l;
    int m=(l+r)/2;
    //cout<<"+ "<<m<<" "<<t[i*2+1]<<endl;
    if(t[i*2+1]<idx)return previous(i*2+1,m+1,r,idx);
    else return previous(i*2,l,m,idx);
}

void read()
{
    cin>>n>>q;
    for(int i=1;i<=4*n;i++)
        t[i]=n+1;

    int bg=-1;
    for(int i=1; i<=n; i++)
    {
        cin>>c[i];
        if(c[i]=='0')
        {
            update(1,0,n+1,i,0);
            if(bg!=-1)
            {
                r[bg]=i-1;
                tmr[bg]=0;
            }
            bg=-1;
        }
        else
        {
            update(1,0,n+1,i,1);
            if(bg==-1)bg=i;
        }
    }
    if(bg!=-1)
    {
        r[bg]=n;
        tmr[bg]=0;
    }
}

void solve()
{
    c[0]=c[n+1]='0';
    for(int i=1; i<=q; i++)
    {
        string tp;
        int x,y;
        cin>>tp;
        if(tp=="toggle")
        {
            cin>>x;
            int prv=previous(1,0,n+1,x)+1;
            //cout<<prv<<endl;
            //cout<<prv<<endl;
            if(c[x]=='0')
            {
                c[x]='1';
                update(1,0,n+1,x,1);
                if(c[x-1]=='0'&&c[x+1]=='0')
                {
                    r[x]=x;
                    tmr[x]=i;
                }
                else if(c[x-1]=='0')
                {
                    s.update(0,1,n,x+1,i-tmr[x+1],r[x+1]);
                    r[x]=r[x+1];
                    tmr[x]=i;
                }
                else if(c[x+1]=='0')
                {
                    int lf=prv;
                    s.update(0,1,n,lf,i-tmr[lf],x-1);
                    r[lf]=x;
                    tmr[lf]=i;
                }
                else
                {
                    //cout<<"in"<<endl;
                    int lf=prv;
                    s.update(0,1,n,lf,i-tmr[lf],x-1);
                    s.update(0,1,n,x+1,i-tmr[x+1],r[x+1]);
                    r[lf]=r[x+1];
                    tmr[lf]=i;
                    //cout<<lf<<" "<<r[x+1]<<endl;
                }
            }
            else
            {
                c[x]='0';
                update(1,0,n+1,x,0);
                s.update(0,1,n,prv,i-tmr[prv],r[prv]);
                if(c[x-1]=='0'&&c[x+1]=='0')
                {
                    ;
                }
                else if(c[x-1]=='0')
                {
                    tmr[x+1]=i;
                    r[x+1]=r[x];
                }
                else if(c[x+1]=='0')
                {
                    tmr[prv]=i;
                    r[prv]=x-1;
                }
                else
                {
                    tmr[prv]=tmr[x+1]=i;
                    r[x+1]=r[prv];
                    r[prv]=x-1;
                    itv.insert(x+1);
                }
            }
        }
        if(tp=="query")
        {
            cin>>x>>y;
            int add=0;
            int prv=previous(1,0,n+1,x)+1;
            if(c[x]=='1'&&prv<=x&&r[prv]>=y-1)add=i-tmr[prv];
            //cout<<prv<<" "<<r[prv]<<endl;
            cout<<add+s.query(0,1,n,1,x,y-1,n)<<'\n';
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read();
    solve();
    return 0;
}
/*
7 10
0110101
query 2 3
toggle 2
query 2 3
toggle 2
query 2 3
toggle 6
query 5 7
query 6 6
toggle 5
query 5 5

1
2
3
1
8
9
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 6656 KB Output is correct
2 Correct 340 ms 7400 KB Output is correct
3 Correct 721 ms 14412 KB Output is correct
4 Correct 2670 ms 227032 KB Output is correct
5 Correct 3261 ms 258212 KB Output is correct
6 Correct 3006 ms 240260 KB Output is correct
7 Correct 151 ms 13104 KB Output is correct
8 Correct 129 ms 14672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3160 KB Output is correct
2 Correct 3 ms 3160 KB Output is correct
3 Correct 3 ms 2908 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 4537 ms 324132 KB Output is correct
6 Correct 4274 ms 309604 KB Output is correct
7 Correct 2933 ms 257744 KB Output is correct
8 Correct 129 ms 14560 KB Output is correct
9 Correct 74 ms 5972 KB Output is correct
10 Correct 85 ms 6748 KB Output is correct
11 Correct 81 ms 6712 KB Output is correct
12 Correct 151 ms 13136 KB Output is correct
13 Correct 127 ms 14676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 3 ms 2908 KB Output is correct
4 Correct 4 ms 2904 KB Output is correct
5 Correct 255 ms 27644 KB Output is correct
6 Correct 1500 ms 171556 KB Output is correct
7 Correct 2935 ms 241548 KB Output is correct
8 Correct 4964 ms 284568 KB Output is correct
9 Correct 480 ms 6480 KB Output is correct
10 Correct 298 ms 5560 KB Output is correct
11 Correct 423 ms 6552 KB Output is correct
12 Correct 296 ms 5472 KB Output is correct
13 Correct 424 ms 6484 KB Output is correct
14 Correct 303 ms 5460 KB Output is correct
15 Correct 148 ms 13136 KB Output is correct
16 Correct 129 ms 14528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 232 ms 6656 KB Output is correct
9 Correct 340 ms 7400 KB Output is correct
10 Correct 721 ms 14412 KB Output is correct
11 Correct 2670 ms 227032 KB Output is correct
12 Correct 3261 ms 258212 KB Output is correct
13 Correct 3006 ms 240260 KB Output is correct
14 Correct 151 ms 13104 KB Output is correct
15 Correct 129 ms 14672 KB Output is correct
16 Correct 4 ms 3160 KB Output is correct
17 Correct 3 ms 3160 KB Output is correct
18 Correct 3 ms 2908 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 4537 ms 324132 KB Output is correct
21 Correct 4274 ms 309604 KB Output is correct
22 Correct 2933 ms 257744 KB Output is correct
23 Correct 129 ms 14560 KB Output is correct
24 Correct 74 ms 5972 KB Output is correct
25 Correct 85 ms 6748 KB Output is correct
26 Correct 81 ms 6712 KB Output is correct
27 Correct 151 ms 13136 KB Output is correct
28 Correct 127 ms 14676 KB Output is correct
29 Correct 1 ms 2392 KB Output is correct
30 Correct 2 ms 2648 KB Output is correct
31 Correct 3 ms 2908 KB Output is correct
32 Correct 4 ms 2904 KB Output is correct
33 Correct 255 ms 27644 KB Output is correct
34 Correct 1500 ms 171556 KB Output is correct
35 Correct 2935 ms 241548 KB Output is correct
36 Correct 4964 ms 284568 KB Output is correct
37 Correct 480 ms 6480 KB Output is correct
38 Correct 298 ms 5560 KB Output is correct
39 Correct 423 ms 6552 KB Output is correct
40 Correct 296 ms 5472 KB Output is correct
41 Correct 424 ms 6484 KB Output is correct
42 Correct 303 ms 5460 KB Output is correct
43 Correct 148 ms 13136 KB Output is correct
44 Correct 129 ms 14528 KB Output is correct
45 Correct 90 ms 4692 KB Output is correct
46 Correct 151 ms 5320 KB Output is correct
47 Correct 1331 ms 103652 KB Output is correct
48 Correct 2567 ms 226352 KB Output is correct
49 Correct 88 ms 6484 KB Output is correct
50 Correct 85 ms 6484 KB Output is correct
51 Correct 88 ms 6584 KB Output is correct
52 Correct 80 ms 6996 KB Output is correct
53 Correct 79 ms 6812 KB Output is correct
54 Correct 78 ms 6996 KB Output is correct