Submission #956852

# Submission time Handle Problem Language Result Execution time Memory
956852 2024-04-02T14:50:34 Z HoriaHaivas Street Lamps (APIO19_street_lamps) C++14
0 / 100
480 ms 52428 KB
/*
    "care a facut teste cu Lattice reduction attack e ciudat"
    "linistiti va putin"
    - 2023 -
*/
#include<bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")

using namespace std;

struct event
{
    int x;
    int y;
    int moment;
};

int mom,n;
bool state[300001];
bool initialstate[300001];
bool type[300001];
int l[300001];
int r[300001];
int pos[300001];
int altaib[300001];
vector<event> updates;
vector<int> vals[300001];
vector<int> aib[300001];

void altupdate(int index, int delta)
{
    while (index<=n)
    {
        altaib[index]+=delta;
        index+=index&(-index);
    }
}

int altquery(int index)
{
    int ans;
    ans=0;
    while (index>0)
    {
        ans+=altaib[index];
        index-=index&(-index);
    }
    return ans;
}

void setupdate(int x, int y)
{
     for (;x<=n;x+=x&(-x))
     {
          vals[x].push_back(y);
     }
}

void updatinho(int apos)
{
     int st,dr;
     st=apos-1;
     while (st>0 && state[st]==1)
     {
            st--;
     }
     st++;
     dr=apos+1;
     while (dr<n+1 && state[dr]==1)
     {
            dr++;
     }
     dr--;
     setupdate(st,apos);
     updates.push_back({st,apos,mom});
     setupdate(apos+1,apos);
     updates.push_back({apos+1,apos,mom});
     setupdate(st,dr+1);
     updates.push_back({st,dr+1,mom});
     setupdate(apos+1,dr+1);
     updates.push_back({apos+1,dr+1,mom});
}

int indice(int x, int y)
{
    return upper_bound(vals[x].begin(),vals[x].end(),y)-vals[x].begin()-1;
}

void update(int x, int y, int delta)
{
    int newy;
    for (;x<=n;x+=x&(-x))
    {
         newy=indice(x,y);
         for (;newy<=aib[x].size();newy+=newy&(-newy))
         {
              aib[x][newy]+=delta;
         }
    }
}

int query(int x, int y)
{
    int ans,newy;
    ans=0;
    for (;x>0;x-=x&(-x))
    {
        newy=indice(x,y);
        for (;newy>0;newy-=newy&(-newy))
        {
             ans+=aib[x][newy];
        }
    }
    return ans;
}

signed main()
{
    ifstream fin("secvp.in");
    ofstream fout("secvp.out");
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long q,i,j,cnt;
    char ch;
    string s;
    cin >> n >> q;
    for (i=1; i<=n; i++)
    {
        cin >> ch;
        if (ch=='0')
            state[i]=0;
        else
            state[i]=1;
        if (state[i]==1)
        altupdate(i,1);
        initialstate[i]=state[i];
    }
    for (i=1; i<=q; i++)
    {
        cin >> s;
        if (s=="query")
            type[i]=1;
        else
            type[i]=0;
        if (type[i]==1)
        {
            cin >> l[i] >> r[i];
        }
        else
        {
            cin >> pos[i];
            mom=i;
            updatinho(pos[i]);
            state[pos[i]]=1-state[pos[i]];
        }
    }
    for (i=1;i<=n;i++)
    {
         vals[i].push_back(0);
         sort(vals[i].begin(),vals[i].end());
         aib[i].resize(vals[i].size());
         state[i]=initialstate[i];
    }
    cnt=0;
    for (i=1;i<=q;i++)
    {
         if (type[i]==1)
         {
             if (query(l[i],r[i]-1)!=0)
             cout << i+query(l[i],r[i]-1) << "\n";
             else
             {
                 if (altquery(r[i]-1)-altquery(l[i]-1)==(r[i]-l[i]))
                    cout << i << "\n";
                 else
                    cout << 0 << "\n";
             }
         }
         else
         {
             if (state[pos[i]]==1)
             {
                 update(updates[cnt].x,updates[cnt].y,mom);
                 cnt++;
                 update(updates[cnt].x,updates[cnt].y,-mom);
                 cnt++;
                 update(updates[cnt].x,updates[cnt].y,-mom);
                 cnt++;
                 update(updates[cnt].x,updates[cnt].y,mom);
                 cnt++;
                 altupdate(pos[i],-1);
             }
             else
             {
                 update(updates[cnt].x,updates[cnt].y,-mom);
                 cnt++;
                 update(updates[cnt].x,updates[cnt].y,mom);
                 cnt++;
                 update(updates[cnt].x,updates[cnt].y,mom);
                 cnt++;
                 update(updates[cnt].x,updates[cnt].y,-mom);
                 cnt++;
                 altupdate(pos[i],1);
             }
         }
    }
}

Compilation message

street_lamps.cpp: In function 'void update(int, int, int)':
street_lamps.cpp:97:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |          for (;newy<=aib[x].size();newy+=newy&(-newy))
      |                ~~~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:126:19: warning: unused variable 'j' [-Wunused-variable]
  126 |     long long q,i,j,cnt;
      |                   ^
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 19036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 480 ms 52428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19288 KB Output is correct
2 Incorrect 7 ms 19292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Incorrect 5 ms 19036 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 19036 KB Output isn't correct
2 Halted 0 ms 0 KB -