Submission #979039

# Submission time Handle Problem Language Result Execution time Memory
979039 2024-05-10T06:55:32 Z Kenjikrab Street Lamps (APIO19_street_lamps) C++14
80 / 100
5000 ms 47960 KB
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
typedef array<int,4> arr4;
int const n_max=3e5+10;

set<int> zero;
char s[n_max];
arr4 arr[3*n_max];
int ans[n_max];
int cnt=0;
int fen[n_max];
void upd(int idx,int val)
{
    for(;idx<n_max;idx+=idx&-idx)fen[idx]+=val;
}
int qr(int idx)
{
    int ret=0;
    for(idx;idx;idx-=idx&-idx)ret+=fen[idx];
    return ret;
}
void sol(int l,int r)
{
    if(l>=r)return;
    int mid=(l+r)>>1;
    sol(l,mid);sol(mid+1,r);
    int p=l;
    for(int i=mid+1;i<=r;i++)
    {
        if(arr[i][2])continue;
        while(p<=mid&&arr[p][1]>=arr[i][1]){upd(arr[p][3],arr[p][2]),p++;}
        ans[arr[i][3]]+=qr(arr[i][3]);
    }
    for(int i=l;i<p;i++)
    {
        upd(arr[i][3],-arr[i][2]);
    }
    inplace_merge(arr+l,arr+mid+1,arr+r+1,[&](const arr4 &a,const arr4 &b)
    {return a[1]>b[1];});
}
signed main()
{
    //cin.tie(nullptr)->sync_with_stdio(false);
    memset(ans,-1,sizeof(ans));
    int n,q;
    scanf("%d%d%s",&n,&q,s+1);
    zero.insert(0);
    for(int i=1;i<=n;i++)
    { 
        if(!(s[i]-='0'))zero.insert(i);
    }
    zero.insert(n+1);
    char com[10];
    for(int i=1;i<=q;i++)
    {
        scanf("%s",com);
        if(com[0]=='q')
        {
            int a,b;
            scanf("%d%d",&a,&b);
            b--;
            arr[cnt++]={a,b,0,i};
            ans[i]=*zero.lower_bound(a)<=b? 0:i;
        }
        else
        {
            int a;
            scanf("%d",&a);
            if(s[a])
            {
                auto it=zero.upper_bound(a);
                int l=*prev(it),m=a,r=*it;
                arr[cnt++]={l+1,r-1,i,i};
                arr[cnt++]={l+1,m-1,-i,i};
                arr[cnt++]={m+1,r-1,-i,i};
                zero.insert(a);
            }
            else
            {
                auto it=zero.find(a);
                int l=*prev(it),m=a,r=*next(it);
                arr[cnt++]={l+1,r-1,-i,i};
                arr[cnt++]={l+1,m-1,i,i};
                arr[cnt++]={m+1,r-1,i,i};
                zero.erase(it);
            }
            s[a]^=1;
        }
    }
    sort(arr, arr+cnt, [&](const arr4 &a, const arr4 &b)
    {return a[0]^b[0] ? a[0]<b[0] : ((a[1]!=b[1])? a[1]>b[1]:(b[2]==0)); });
    sol(0,cnt-1);
    for(int i=1;i<=q;i++)
    {
        if(ans[i]==-1)continue;
        else printf("%d\n",ans[i]);
    }
    return 0;
}

Compilation message

street_lamps.cpp: In function 'int qr(int)':
street_lamps.cpp:22:9: warning: statement has no effect [-Wunused-value]
   22 |     for(idx;idx;idx-=idx&-idx)ret+=fen[idx];
      |         ^~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |     scanf("%d%d%s",&n,&q,s+1);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         scanf("%s",com);
      |         ~~~~~^~~~~~~~~~
street_lamps.cpp:63:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |             scanf("%d%d",&a,&b);
      |             ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:71:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |             scanf("%d",&a);
      |             ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 22268 KB Output is correct
2 Correct 345 ms 24060 KB Output is correct
3 Correct 269 ms 23968 KB Output is correct
4 Correct 333 ms 35644 KB Output is correct
5 Correct 359 ms 32516 KB Output is correct
6 Correct 334 ms 38228 KB Output is correct
7 Correct 325 ms 34772 KB Output is correct
8 Correct 163 ms 21512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6628 KB Output is correct
5 Correct 397 ms 47960 KB Output is correct
6 Correct 380 ms 41172 KB Output is correct
7 Correct 354 ms 33176 KB Output is correct
8 Correct 317 ms 22056 KB Output is correct
9 Correct 230 ms 16168 KB Output is correct
10 Correct 267 ms 17932 KB Output is correct
11 Correct 252 ms 18284 KB Output is correct
12 Correct 468 ms 36092 KB Output is correct
13 Correct 303 ms 21544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 401 ms 28960 KB Output is correct
6 Correct 436 ms 31212 KB Output is correct
7 Correct 350 ms 38136 KB Output is correct
8 Correct 315 ms 38812 KB Output is correct
9 Correct 276 ms 30896 KB Output is correct
10 Correct 235 ms 32280 KB Output is correct
11 Correct 257 ms 29220 KB Output is correct
12 Correct 244 ms 32176 KB Output is correct
13 Correct 257 ms 29776 KB Output is correct
14 Correct 236 ms 31312 KB Output is correct
15 Correct 479 ms 35536 KB Output is correct
16 Correct 311 ms 21804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 355 ms 22268 KB Output is correct
9 Correct 345 ms 24060 KB Output is correct
10 Correct 269 ms 23968 KB Output is correct
11 Correct 333 ms 35644 KB Output is correct
12 Correct 359 ms 32516 KB Output is correct
13 Correct 334 ms 38228 KB Output is correct
14 Correct 325 ms 34772 KB Output is correct
15 Correct 163 ms 21512 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 2 ms 6628 KB Output is correct
20 Correct 397 ms 47960 KB Output is correct
21 Correct 380 ms 41172 KB Output is correct
22 Correct 354 ms 33176 KB Output is correct
23 Correct 317 ms 22056 KB Output is correct
24 Correct 230 ms 16168 KB Output is correct
25 Correct 267 ms 17932 KB Output is correct
26 Correct 252 ms 18284 KB Output is correct
27 Correct 468 ms 36092 KB Output is correct
28 Correct 303 ms 21544 KB Output is correct
29 Correct 2 ms 6488 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Correct 3 ms 6492 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 401 ms 28960 KB Output is correct
34 Correct 436 ms 31212 KB Output is correct
35 Correct 350 ms 38136 KB Output is correct
36 Correct 315 ms 38812 KB Output is correct
37 Correct 276 ms 30896 KB Output is correct
38 Correct 235 ms 32280 KB Output is correct
39 Correct 257 ms 29220 KB Output is correct
40 Correct 244 ms 32176 KB Output is correct
41 Correct 257 ms 29776 KB Output is correct
42 Correct 236 ms 31312 KB Output is correct
43 Correct 479 ms 35536 KB Output is correct
44 Correct 311 ms 21804 KB Output is correct
45 Correct 252 ms 19592 KB Output is correct
46 Correct 183 ms 19532 KB Output is correct
47 Correct 219 ms 23252 KB Output is correct
48 Correct 364 ms 36268 KB Output is correct
49 Execution timed out 5028 ms 20448 KB Time limit exceeded
50 Halted 0 ms 0 KB -