Submission #250939

# Submission time Handle Problem Language Result Execution time Memory
250939 2020-07-19T14:07:01 Z huuducpro Street Lamps (APIO19_street_lamps) C++14
100 / 100
3218 ms 156944 KB
#include <bits/stdc++.h>
using namespace std;
int n,q,A[300002];
int bb[300002],ll[300002],rr[300002],len[300002];
int ss[300002];
struct query
{
    int type,a,b;
};
query Q[300002];
void update(int i,int val)
{
    for (i;i<=n;i+=i&(-i))
        bb[i]+=val;
}
int get(int i)
{
    int ans=0;
    for (i;i>=1;i-=i&(-i))
        ans+=bb[i];
    return ans;
}
int getl(int a)
{
    int ans=a;
    int d=1;int c=a-1;int t=get(a-1);
    while (d<=c)
    {
        int mid=(d+c)/2;
        if (t-get(mid-1)==a-mid)
        {
            ans=mid;
            c=mid-1;
        }
        else d=mid+1;
    }
    return ans;
}
int getr(int a)
{
    int ans=a;
    int d=a+1;int c=n;int t=get(a);
    while (d<=c)
    {
        int mid=(d+c)/2;
        if (get(mid)-t==mid-a)
        {
            ans=mid;
            d=mid+1;
        }
        else c=mid-1;
    }
    return ans;
}

vector <int> bit[300002],cs[300002],pre[300002],nex[300002];
vector <int> st[300002];
void addr(int a,int b)
{
    for (int i=a;i<=n;i+=i&(-i))
        st[i].push_back(b);
}
void addl(int a,int b)
{
    for (int i=a;i>=1;i-=i&(-i))
        st[i].push_back(b);
}
void build_bit2d()
{
    for (int i=1;i<=n;i++)
    {
        cs[i].push_back(0);
        sort(st[i].begin(),st[i].end());
        for (int j=0;j<st[i].size();j++)
        {
            if (j==0||st[i][j]>st[i][j-1])
                cs[i].push_back(st[i][j]);
        }
        len[i]=cs[i].size()-1;
        cs[i].push_back(1e9);
        bit[i].resize(cs[i].size(),0);
       // for (int j=1;j<=len[i];j++)
           // cout<<cs[i][j]<<' ';cout<<endl;
    }
}
void update_bit2d(int a,int b,int val)
{
    for (int i=a;i<=n;i+=i&(-i))
    {
        int xp=lower_bound(cs[i].begin(),cs[i].end(),b)-cs[i].begin();
        assert(xp>0);
        for (int j=xp;j<=len[i];j+=j&(-j))
            bit[i][j]+=val;
    }
}
int get_bit2d(int a,int b)
{
    int ans=0;
    for (int i=a;i>=1;i-=i&(-i))
    {
        int xp=lower_bound(cs[i].begin(),cs[i].end(),b)-cs[i].begin();
        for (int j=xp;j>=1;j-=j&(-j))
            ans+=bit[i][j];
    }
    return ans;
}
void solve()
{
    cin>>n>>q;
    string s;cin>>s;
    for (int i=1;i<=n;i++)
    {
        if (s[i-1]=='1') update(i,1);
        A[i]=s[i-1]-'0';
    }
    for (int i=1;i<=q;i++)
    {
        string s;cin>>s;
        if (s=="toggle")
        {
            int p;cin>>p;A[p]=1-A[p];
            int l=getl(p);int r=getr(p);
            addr(l,p);addr(p+1,p);
            addr(l,r+1);addr(p+1,r+1);
            Q[i]={0,p,A[p]};
            ll[i]=l;rr[i]=r;
            if (A[p]==0) update(p,-1);
            else update(p,1);
        }
        else
        {
            int a,b;cin>>a>>b;b--;
            addl(a,b);
            Q[i]={1,a,b};
            if (get(b)-get(a-1)==b-a+1) ss[i]=1;
            else ss[i]=0;
        }
    }
    build_bit2d();
    for (int i=1;i<=q;i++)
    {
        if (Q[i].type==0)
        {
            int p=Q[i].a;int l=ll[i];int r=rr[i];
            if (Q[i].b==1)
            {
                update_bit2d(l,p,-i);
                update_bit2d(p+1,p,i);
                update_bit2d(l,r+1,i);
                update_bit2d(p+1,r+1,-i);
            }
            else
            {
                update_bit2d(l,p,i);
                update_bit2d(p+1,p,-i);
                update_bit2d(l,r+1,-i);
                update_bit2d(p+1,r+1,i);
            }
        }
        else
        {
            int a=Q[i].a;int b=Q[i].b;
            long long res=get_bit2d(a,b);
            if (ss[i]==1) res+=i;
            cout<<res<<'\n';
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("lamps2.inp","r",stdin);
   // freopen("lamps2.out","w",stdout);
    solve();
}

Compilation message

street_lamps.cpp: In function 'void update(int, int)':
street_lamps.cpp:13:11: warning: statement has no effect [-Wunused-value]
     for (i;i<=n;i+=i&(-i))
           ^
street_lamps.cpp: In function 'int get(int)':
street_lamps.cpp:19:11: warning: statement has no effect [-Wunused-value]
     for (i;i>=1;i-=i&(-i))
           ^
street_lamps.cpp: In function 'void build_bit2d()':
street_lamps.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<st[i].size();j++)
                      ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35584 KB Output is correct
2 Correct 25 ms 35584 KB Output is correct
3 Correct 21 ms 35584 KB Output is correct
4 Correct 21 ms 35576 KB Output is correct
5 Correct 21 ms 35584 KB Output is correct
6 Correct 21 ms 35584 KB Output is correct
7 Correct 21 ms 35584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 335 ms 58936 KB Output is correct
2 Correct 433 ms 65612 KB Output is correct
3 Correct 772 ms 75184 KB Output is correct
4 Correct 2161 ms 135100 KB Output is correct
5 Correct 1955 ms 130860 KB Output is correct
6 Correct 2366 ms 136240 KB Output is correct
7 Correct 1103 ms 99604 KB Output is correct
8 Correct 1197 ms 102348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35964 KB Output is correct
2 Correct 27 ms 35840 KB Output is correct
3 Correct 29 ms 35840 KB Output is correct
4 Correct 24 ms 35712 KB Output is correct
5 Correct 3218 ms 156944 KB Output is correct
6 Correct 2856 ms 144368 KB Output is correct
7 Correct 1910 ms 131628 KB Output is correct
8 Correct 1258 ms 107648 KB Output is correct
9 Correct 156 ms 47084 KB Output is correct
10 Correct 183 ms 48104 KB Output is correct
11 Correct 177 ms 47972 KB Output is correct
12 Correct 1162 ms 104828 KB Output is correct
13 Correct 1309 ms 107524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35840 KB Output is correct
2 Correct 27 ms 35840 KB Output is correct
3 Correct 24 ms 35840 KB Output is correct
4 Correct 30 ms 35840 KB Output is correct
5 Correct 1312 ms 107268 KB Output is correct
6 Correct 1689 ms 125432 KB Output is correct
7 Correct 2225 ms 138508 KB Output is correct
8 Correct 2945 ms 149264 KB Output is correct
9 Correct 549 ms 68548 KB Output is correct
10 Correct 493 ms 65100 KB Output is correct
11 Correct 564 ms 68816 KB Output is correct
12 Correct 445 ms 65888 KB Output is correct
13 Correct 562 ms 68932 KB Output is correct
14 Correct 436 ms 65496 KB Output is correct
15 Correct 1056 ms 105092 KB Output is correct
16 Correct 1282 ms 107648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35584 KB Output is correct
2 Correct 25 ms 35584 KB Output is correct
3 Correct 21 ms 35584 KB Output is correct
4 Correct 21 ms 35576 KB Output is correct
5 Correct 21 ms 35584 KB Output is correct
6 Correct 21 ms 35584 KB Output is correct
7 Correct 21 ms 35584 KB Output is correct
8 Correct 335 ms 58936 KB Output is correct
9 Correct 433 ms 65612 KB Output is correct
10 Correct 772 ms 75184 KB Output is correct
11 Correct 2161 ms 135100 KB Output is correct
12 Correct 1955 ms 130860 KB Output is correct
13 Correct 2366 ms 136240 KB Output is correct
14 Correct 1103 ms 99604 KB Output is correct
15 Correct 1197 ms 102348 KB Output is correct
16 Correct 25 ms 35964 KB Output is correct
17 Correct 27 ms 35840 KB Output is correct
18 Correct 29 ms 35840 KB Output is correct
19 Correct 24 ms 35712 KB Output is correct
20 Correct 3218 ms 156944 KB Output is correct
21 Correct 2856 ms 144368 KB Output is correct
22 Correct 1910 ms 131628 KB Output is correct
23 Correct 1258 ms 107648 KB Output is correct
24 Correct 156 ms 47084 KB Output is correct
25 Correct 183 ms 48104 KB Output is correct
26 Correct 177 ms 47972 KB Output is correct
27 Correct 1162 ms 104828 KB Output is correct
28 Correct 1309 ms 107524 KB Output is correct
29 Correct 25 ms 35840 KB Output is correct
30 Correct 27 ms 35840 KB Output is correct
31 Correct 24 ms 35840 KB Output is correct
32 Correct 30 ms 35840 KB Output is correct
33 Correct 1312 ms 107268 KB Output is correct
34 Correct 1689 ms 125432 KB Output is correct
35 Correct 2225 ms 138508 KB Output is correct
36 Correct 2945 ms 149264 KB Output is correct
37 Correct 549 ms 68548 KB Output is correct
38 Correct 493 ms 65100 KB Output is correct
39 Correct 564 ms 68816 KB Output is correct
40 Correct 445 ms 65888 KB Output is correct
41 Correct 562 ms 68932 KB Output is correct
42 Correct 436 ms 65496 KB Output is correct
43 Correct 1056 ms 105092 KB Output is correct
44 Correct 1282 ms 107648 KB Output is correct
45 Correct 136 ms 48088 KB Output is correct
46 Correct 243 ms 52320 KB Output is correct
47 Correct 1064 ms 87896 KB Output is correct
48 Correct 2058 ms 136628 KB Output is correct
49 Correct 193 ms 49260 KB Output is correct
50 Correct 192 ms 49128 KB Output is correct
51 Correct 201 ms 49380 KB Output is correct
52 Correct 242 ms 51688 KB Output is correct
53 Correct 232 ms 51692 KB Output is correct
54 Correct 232 ms 51816 KB Output is correct