트 리 배열 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; }

좋은 웹페이지 즐겨찾기