Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## 用线段树老超时～～实在无奈～～～请各位大牛帮忙看看～～～

Posted by owenlee at 2008-05-27 09:50:00 on Problem 2828
```#include<iostream>
using namespace std;

int  A[ 200000 + 1 ][ 2 ], C[ 600000 + 3 ], result[ 200000 + 1 ];

inline void  BuildTree( int i, int p ,int q )

{	if( p == q )
{
C[ i ] = 1;
return;
}
C[ i ] = q - p + 1;
int L, R, Mid;
Mid = ( p + q ) >> 1;
L = ( i << 1 ) + 1;
R = L + 1;
BuildTree( L, p, Mid );
BuildTree( R, Mid + 1, q );
}

inline  void OutOfQueue( int i, int p, int q, int position )
{
C[ i ]--;
if( p == q )
{
result[ p ] = A[ position ][ 1 ];
return;
}
int L, R, Mid;
Mid = ( p + q ) >> 1;
L = ( i << 1 ) + 1;
R = L + 1;
if( C[ L ] > A[ position ][ 0 ] )
OutOfQueue( L, p, Mid, position );
else
{
A[ position ][ 0 ] -= C[ L ];
OutOfQueue( R, Mid + 1, q, position );
}
}

int main()
{
int n;
while( cin >> n )
{
for( int i = 0; i < n; i++ ) cin >> A[ i ][ 0 ] >> A[ i ][ 1 ];

BuildTree( 0, 1, n );

for( int i = n - 1; i >= 0; i-- )
OutOfQueue( 0, 1, n, i );

for( int i = 1; i <= n; i++)
cout << result[ i ] << ' ';
cout << endl;
}
return 0;
}```

Followed by: