트 리 배열 test
1503 단어 test
간단 하여 알 기 쉽다
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn = 10005;
int a[ maxn ],c[ maxn ];
int lowbit( int i ){
return i&(-i);
}
int sum( int i ){
int s=0;
while( i>0 ){
s+=c[ i ];
i-=lowbit( i );
}
return s;
}
void update( int i,int new_val,int pre_val ){
while( i<maxn ){
c[ i ]-=pre_val;
c[ i ]+=new_val;
i+=lowbit( i );
}
}
void test( int n ){
printf("
");
for( int i=1;i<=n;i++ )
printf("%d ",a[ i ]);
printf("
");
}
int main(){
int n;
while( scanf("%d",&n)==1 ){
memset( a,0,sizeof( a ) );
memset( c,0,sizeof( c ) );
for( int i=1;i<=n;i++ ){
scanf("%d",&a[ i ]);
update( i,a[ i ],0 );
}
int m;
scanf("%d",&m);
while( m-- ){
int op;
scanf("%d",&op);
if( op==1 ){
int pos,new_val;
scanf("%d%d",&pos,&new_val);
test( n );
update( pos,new_val,a[ pos ] );
a[ pos ]=new_val;
test( n );// 1 n
}//change a[ pos ] to new_val
else if( op==2 ){
int from,to;
scanf("%d%d",&from,&to);
if( from>1 )
printf("from %d to %d:%d
",from,to,sum( to )-sum( from-1 ) );
else
printf("from %d to %d:%d
",from,to,sum( to ) );
}// from to
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Sorting Layer와 3D Object의 관계이것은 원래는 에 게재하고 있던 기사이지만, 뭐 자신의 블로그는 기술적인 기사를 도카도카 게재하는 장소도 아니기 때문에, 이쪽에도 병행해 게재해 둔다. 어느 날 질문을 받았다. "그러고 보니 unity에서 sorti...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.