답안 #931979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931979 2024-02-22T17:31:48 Z sleepntsheep 가로등 (APIO19_street_lamps) C++17
100 / 100
1850 ms 36944 KB
#include<stdio.h>
#define N 300005
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,tune=native")

int abs(int x){return x<0?-x:x;}
unsigned X=12345;int rand_(){return(X*=3)>>1;}
int (*compar)(int,int);
void sort(int*aa,int l,int r){
    while(l<r){int i=l,j=l,k=r,tmp,p=aa[l+rand_()%(r-l)];
        while(j<k)switch(compar(aa[j],p)){case 0:++j;break;case -1:tmp=aa[j],aa[j]=aa[i],aa[i]=tmp,++i,++j;break;case 1:--k,tmp=aa[j],aa[j]=aa[k],aa[k]=tmp;break;}
        sort(aa,l,i);l=k;
    }
}
void f_u(int*t,int p,int k){ for(;p<N;p+=p&-p)t[p]+=k; }
int f_q(int*t,int p){int z=0;for(;p;p-=p&-p)z+=t[p];return z;}
int f_lb(int*t,int k){if(!k)return 0;int p=0,z=0;for(int j=1<<19;j>>=1;)if(p+j<N&&z+t[p+j]<k)z+=t[p+=j];return p+1;}

#define Z (1<<20)
int f[2][N],p[1<<20][3],pn,q[1<<20][4],qn,n,Q;char s[N],op[7];
int pq[Z][4],m,ans[N],ansn[N];
int o1[Z],o2[Z];

void A(int a,int b,int t){if(a<1)a=1;if(b>n)b=n;p[pn][0]=a,p[pn][1]=b,p[pn++][2]=t;}

int cmprb(int i,int j){return pq[i][1]<pq[j][1]?1:(pq[i][1]>pq[j][1]?-1:(~pq[i][3]&&~pq[j][3]?0:(~pq[i][3]?1:~pq[j][3]?-1:0)));
}
int cmprx(int i,int j){
    i=o1[i],j=o1[j];
    int ai=pq[i][0],aj=pq[j][0];
    if(ai-aj)return ai<aj?-1:1;
    if(~pq[i][3]&&~pq[j][3])return 0;
    if(~pq[i][3])return 1;if(~pq[j][3])return -1;
    return 0;
}

void cdq(int l,int r){
    if(l+1>=r)return;
    int m=(l+r)>>1;
    cdq(l,m);cdq(m,r);
    for(int j=l;j<r;++j)o2[j]=j;
    sort(o2,l,r);
    for(int j=l;j<r;++j)
    {
        int I=o1[o2[j]],J=pq[I][3];
        if(~J&&o2[j]>=m){
            ans[J]+=f_q(f[0],pq[I][2]+1);
            ansn[J]+=f_q(f[1],pq[I][2]+1);
        }
        else if(!~J&&o2[j]<m)
        {
            f_u(f[0],abs(pq[I][2])+1,pq[I][2]);
            if(pq[I][2]>=0)f_u(f[1],pq[I][2]+1,1);
            else f_u(f[1],-pq[I][2]+1,-1);
        }
    }

    for(int j=l;j<r;++j)
    {
        int I=o1[o2[j]],J=pq[I][3];
        if(!~J&&o2[j]<m)
        {
            f_u(f[0],abs(pq[I][2])+1,-pq[I][2]);
            if(pq[I][2]>=0)f_u(f[1],pq[I][2]+1,-1);
            else f_u(f[1],-pq[I][2]+1,-(-1));
        }
    }
}

int main(){scanf("%d%d%s",&n,&Q,s+1);
for(int r=1;r<=n;++r)if(s[r]=='1'){int l=r;while(s[r]=='1')++r;A(l,r-1,0);}
for(int i=1;i<=n;++i)f_u(f[s[i]&1],i,1);
for(int i=1,a,b;i<=Q;++i){scanf("%s%d",op,&a);
    if(*op=='q'){scanf("%d",&b);q[qn][0]=a,q[qn][1]=--b,q[qn][2]=i;q[qn][3]=qn;++qn;}
    else{
        if(s[a]=='1')
        {
            int x=f_q(*f,a),l=f_lb(*f,x),r=f_lb(*f,x+1);
            A(l+1,r-1,-i);if(l+1<=a-1)A(l+1,a-1,i);if(a+1<=r-1)A(a+1,r-1,i);
        }
        else
        {
            int x=f_q(f[1],a),l=f_lb(f[1],x),r=f_lb(f[1],x+1);
            if(l==a-1&&a+1==r){ int X=f_q(*f,a),ll=f_lb(*f,X-1),rr=f_lb(*f,X+1);A(ll+1,a-1,-i);A(a+1,rr-1,-i);A(ll+1,rr-1,i); }
            else if(l==a-1) { int X=f_q(*f,a),ll=f_lb(*f,X-1); A(ll+1,a-1,-i);A(ll+1,a,i); }
            else if(a+1==r) { int X=f_q(*f,a),rr=f_lb(*f,X+1); A(a+1,rr-1,-i),A(a,rr-1,i); }
            else A(a,a,i);
        }
        f_u(f[s[a]&1],a,-1);s[a]^=1;f_u(f[s[a]&1],a,1);
    }
}for(int i=0;i<2;++i)for(int j=0;j<N;++j)f[i][j]=0;

for(int i=0;i<pn;++i)pq[i][0]=p[i][0],pq[i][1]=p[i][1],pq[i][2]=p[i][2],pq[i][3]=-1,o1[i]=i;
for(int i=pn,j=0;j<qn;++j,++i)pq[i][0]=q[j][0],pq[i][1]=q[j][1],pq[i][2]=q[j][2],pq[i][3]=q[j][3],o1[i]=i;
m=qn+pn;
    compar=cmprb;sort(o1,0,m);compar=cmprx;
    cdq(0,m);
    for(int i=0;i<qn;++i)printf("%d\n",-ans[i]+(!!ansn[i])*q[i][2]);
}

Compilation message

street_lamps.cpp: In function 'int cmprx(int, int)':
street_lamps.cpp:33:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   33 |     if(~pq[i][3])return 1;if(~pq[j][3])return -1;
      |     ^~
street_lamps.cpp:33:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   33 |     if(~pq[i][3])return 1;if(~pq[j][3])return -1;
      |                           ^~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:70:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 | int main(){scanf("%d%d%s",&n,&Q,s+1);
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:73:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 | for(int i=1,a,b;i<=Q;++i){scanf("%s%d",op,&a);
      |                           ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:74:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     if(*op=='q'){scanf("%d",&b);q[qn][0]=a,q[qn][1]=--b,q[qn][2]=i;q[qn][3]=qn;++qn;}
      |                  ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12380 KB Output is correct
2 Correct 2 ms 12380 KB Output is correct
3 Correct 2 ms 12380 KB Output is correct
4 Correct 2 ms 12380 KB Output is correct
5 Correct 3 ms 12452 KB Output is correct
6 Correct 2 ms 12380 KB Output is correct
7 Correct 2 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 747 ms 33508 KB Output is correct
2 Correct 774 ms 33872 KB Output is correct
3 Correct 887 ms 33380 KB Output is correct
4 Correct 1416 ms 33120 KB Output is correct
5 Correct 1580 ms 35692 KB Output is correct
6 Correct 1598 ms 35392 KB Output is correct
7 Correct 699 ms 28544 KB Output is correct
8 Correct 739 ms 30168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12380 KB Output is correct
2 Correct 5 ms 12380 KB Output is correct
3 Correct 4 ms 12380 KB Output is correct
4 Correct 3 ms 12380 KB Output is correct
5 Correct 1663 ms 36748 KB Output is correct
6 Correct 1850 ms 36848 KB Output is correct
7 Correct 1741 ms 35140 KB Output is correct
8 Correct 948 ms 29884 KB Output is correct
9 Correct 393 ms 22356 KB Output is correct
10 Correct 441 ms 22484 KB Output is correct
11 Correct 459 ms 22636 KB Output is correct
12 Correct 984 ms 28572 KB Output is correct
13 Correct 994 ms 29964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 12376 KB Output is correct
2 Correct 4 ms 12380 KB Output is correct
3 Correct 4 ms 12380 KB Output is correct
4 Correct 6 ms 12496 KB Output is correct
5 Correct 1252 ms 31180 KB Output is correct
6 Correct 1519 ms 34916 KB Output is correct
7 Correct 1682 ms 34660 KB Output is correct
8 Correct 1742 ms 36812 KB Output is correct
9 Correct 1108 ms 34640 KB Output is correct
10 Correct 1129 ms 36752 KB Output is correct
11 Correct 1098 ms 34900 KB Output is correct
12 Correct 1168 ms 36716 KB Output is correct
13 Correct 1111 ms 34764 KB Output is correct
14 Correct 1140 ms 36944 KB Output is correct
15 Correct 961 ms 28480 KB Output is correct
16 Correct 961 ms 29864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12380 KB Output is correct
2 Correct 2 ms 12380 KB Output is correct
3 Correct 2 ms 12380 KB Output is correct
4 Correct 2 ms 12380 KB Output is correct
5 Correct 3 ms 12452 KB Output is correct
6 Correct 2 ms 12380 KB Output is correct
7 Correct 2 ms 12380 KB Output is correct
8 Correct 747 ms 33508 KB Output is correct
9 Correct 774 ms 33872 KB Output is correct
10 Correct 887 ms 33380 KB Output is correct
11 Correct 1416 ms 33120 KB Output is correct
12 Correct 1580 ms 35692 KB Output is correct
13 Correct 1598 ms 35392 KB Output is correct
14 Correct 699 ms 28544 KB Output is correct
15 Correct 739 ms 30168 KB Output is correct
16 Correct 5 ms 12380 KB Output is correct
17 Correct 5 ms 12380 KB Output is correct
18 Correct 4 ms 12380 KB Output is correct
19 Correct 3 ms 12380 KB Output is correct
20 Correct 1663 ms 36748 KB Output is correct
21 Correct 1850 ms 36848 KB Output is correct
22 Correct 1741 ms 35140 KB Output is correct
23 Correct 948 ms 29884 KB Output is correct
24 Correct 393 ms 22356 KB Output is correct
25 Correct 441 ms 22484 KB Output is correct
26 Correct 459 ms 22636 KB Output is correct
27 Correct 984 ms 28572 KB Output is correct
28 Correct 994 ms 29964 KB Output is correct
29 Correct 4 ms 12376 KB Output is correct
30 Correct 4 ms 12380 KB Output is correct
31 Correct 4 ms 12380 KB Output is correct
32 Correct 6 ms 12496 KB Output is correct
33 Correct 1252 ms 31180 KB Output is correct
34 Correct 1519 ms 34916 KB Output is correct
35 Correct 1682 ms 34660 KB Output is correct
36 Correct 1742 ms 36812 KB Output is correct
37 Correct 1108 ms 34640 KB Output is correct
38 Correct 1129 ms 36752 KB Output is correct
39 Correct 1098 ms 34900 KB Output is correct
40 Correct 1168 ms 36716 KB Output is correct
41 Correct 1111 ms 34764 KB Output is correct
42 Correct 1140 ms 36944 KB Output is correct
43 Correct 961 ms 28480 KB Output is correct
44 Correct 961 ms 29864 KB Output is correct
45 Correct 432 ms 26924 KB Output is correct
46 Correct 548 ms 26704 KB Output is correct
47 Correct 891 ms 30576 KB Output is correct
48 Correct 1579 ms 32868 KB Output is correct
49 Correct 540 ms 28480 KB Output is correct
50 Correct 498 ms 28480 KB Output is correct
51 Correct 511 ms 28496 KB Output is correct
52 Correct 591 ms 28540 KB Output is correct
53 Correct 601 ms 28776 KB Output is correct
54 Correct 589 ms 28720 KB Output is correct