其实就是找一个最小的level k使得level 1到k的ant数量和不小于rank。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 100001; 7 int a[N]; 8 int c[N]; 9 int n, m; 10 11 int lb( int i ) 12 { 13 return i & -i; 14 } 15 16 void update( int i, int v ) 17 { 18 while ( i <= n ) 19 { 20 c[i] += v; 21 i += lb(i); 22 } 23 } 24 25 int kth( int k ) 26 { 27 int ans = 0, cnt = 0; 28 for ( int i = 16; i >= 0; i-- ) 29 { 30 ans += ( 1 << i ); 31 if ( ans >= n || cnt + c[ans] >= k ) 32 { 33 ans -= ( 1 << i ); 34 } 35 else 36 { 37 cnt += c[ans]; 38 } 39 } 40 return ans + 1; 41 } 42 43 int main () 44 { 45 while ( scanf("%d", &n) != EOF ) 46 { 47 memset( c, 0, sizeof(c) ); 48 for ( int i = 1; i <= n; i++ ) 49 { 50 scanf("%d", a + i); 51 update( i, a[i] ); 52 } 53 scanf("%d", &m); 54 while ( m-- ) 55 { 56 char op[2]; 57 int x, y; 58 scanf("%s", op); 59 if ( op[0] == ‘q‘ ) 60 { 61 scanf("%d", &x); 62 printf("%d\n", kth(x)); 63 } 64 else 65 { 66 scanf("%d%d", &x, &y); 67 update( x, y - a[x] ); 68 a[x] = y; 69 } 70 } 71 } 72 return 0; 73 }
时间: 2024-10-15 09:09:57