답안 #142656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
142656 2019-08-10T09:54:21 Z Bodo171 가로등 (APIO19_street_lamps) C++14
0 / 100
1833 ms 4420 KB
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
const int nmax=1005;
struct pct
{
    int x,y,val;
}nl;
int n,i,q,j,poz,a,b;
string ini;
bool operator <(pct unu,pct doi)
{
    return unu.x<doi.x;
}
bool operator !=(pct unu,pct doi)
{
    return (unu.x!=doi.x||unu.y!=doi.y||unu.val!=doi.val);
}
set<pct> s;
set<pct>::iterator it;
string tip;
struct aint
{
    pct a[4*nmax];
    int nr;
    void update(int nod,int l,int r,int x,int y,int val)
    {
        ++nr;
        a[nr].x=x,a[nr].y=y,a[nr].val=val;
    }
    int ret;
    void query(int nod,int l,int r,int x,int y)
    {
        for(int idx=1;idx<=nr;idx++)
            if(a[idx].x<=x&&a[idx].y>=y)
                 ret+=a[idx].val;
    }
    int Q(int x,int y)
    {
        ret=0;
        query(1,1,n,x,y);
        return ret;
    }
}A;
void ins(int poz)
{
    pct st=nl,dr=nl,act;
    it=s.lower_bound({poz,0,0});
    if((*it).x==poz+1)
        dr=(*it);
    if(it!=s.begin())
    {
        it--;
        if((*it).y==poz-1)
            st=(*it);
    }
    act.val=i;act.x=act.y=poz;
    if(st!=nl)
    {
        A.update(1,1,n,st.x,st.y,i-st.val);
        act.x=st.x;
        s.erase(st);
    }
    if(dr!=nl)
    {
        A.update(1,1,n,dr.x,dr.y,i-dr.val);
        act.y=dr.y;
        s.erase(dr);
    }
    s.insert(act);
    return;
}
void del(int poz)
{
    pct st=nl,dr=nl,act;
    it=s.lower_bound({poz+1,0,0});
    it--;//mereu se poate
    act=(*it);
    A.update(1,1,n,act.x,act.y,i-act.val);
    s.erase(act);
    if(poz>act.x)
    {
        st={act.x,poz-1,i};
        s.insert(st);
    }
    if(poz<act.y)
    {
        dr={poz+1,act.y,i};
        s.insert(dr);
    }

}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n>>q;
    cin>>ini;
    for(i=0;i<ini.size();i++)
    {
        if(ini[i]=='1')
        {
            j=i;
            while(j<ini.size()&&ini[j]=='1')
                j++;
            j--;
            s.insert({i+1,j+1,0});
            i=j;
        }
    }
    for(i=1;i<=q;i++)
    {
        cin>>tip;
        if(tip=="toggle")
        {
            cin>>poz;
            if(ini[poz]=='1')
            {
                ini[poz]='0';
                del(poz);
            }
            else
            {
                ini[poz]='1';
                ins(poz);
            }
        }
        else
        {
            int ans;
            cin>>a>>b;
            it=s.lower_bound({b,0,0});
            it--;
            if((*it).x<=a&&(*it).y>=b-1) ans=i-(*it).val;
            else ans=0;
            ans+=A.Q(a,b-1);
            cout<<ans<<'\n';
        }
    }
    return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:100:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<ini.size();i++)
             ~^~~~~~~~~~~
street_lamps.cpp:105:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(j<ini.size()&&ini[j]=='1')
                   ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1833 ms 4420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Incorrect 4 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Incorrect 5 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -