好久没有做题了,开始做这道题时也不知道是咋想的,竟然有链表去实现,弄了好大时候也没弄出来,最后网上看了一下思路,
豁然开朗,用数组就可以实现,总体来说这题用的知识有map<int,int>映射:把输入的x映射为1,2,3...;还有就是树状数组
求"check x y"的值,再加上一个flag数组标记x是否在队列里边就OK了!具体看代码实现吧…………
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <map> 5 #include <cstring> 6 #include <algorithm> 7 #include <cstdio> 8 using namespace std; 9 #define N 100005 10 int n, a[N], num, top; 11 struct node{ 12 int x, y; 13 node(int x, int y):x(x), y(y){} 14 node(){} 15 }; 16 17 bool operator < (node a, node b){ 18 return a.y > b.y; 19 } 20 21 int lowbit(int x){ 22 return x & (-x); 23 } 24 25 void update(int x, int v){ 26 while( x <= n ){ 27 a[x] += v; 28 x += lowbit(x); 29 } 30 } 31 32 int sum(int x){ 33 int ans = 0; 34 while( x > 0 ){ 35 ans += a[x]; 36 x -= lowbit(x); 37 } 38 return ans; 39 } 40 41 int main() 42 { 43 int t, x, y; 44 char ss[8]; 45 while(~scanf("%d", &t)){ 46 n = t; 47 top = 0, num = 1; 48 map<int, int> m; 49 bool flag[N]; 50 priority_queue<node> q; 51 memset(a, 0, sizeof(a)); 52 memset(flag, false, sizeof(flag)); 53 while(t--){ 54 scanf("%s", ss); 55 if(ss[0] == 'l') { 56 while( !q.empty() && !flag[q.top().x] ) q.pop(); 57 if( !q.empty() ) { 58 int k = q.top().x; q.pop(); 59 flag[ k ] = false; 60 update(k, -1); 61 } 62 } 63 else if(ss[0] == 'p') { 64 while( top < num && !flag[top] ) top++; 65 if( top + 1 > num ) continue; 66 update( top, -1 ); 67 flag[top] = false; 68 top++; 69 } 70 else if( ss[0] == 'a' ){ 71 scanf("%d%d", &x, &y); 72 q.push(node(num, y)); 73 flag[ m[x] = num ] = true; 74 num++; 75 } 76 else { 77 scanf("%d%d", &x, &y); 78 x = m[x]; 79 if( !flag[x] ) continue; 80 int yy = x + sum(x-1) - 1; 81 printf("%d ", yy); 82 if( yy > y ){ 83 update( x, -1 ); 84 flag[x] = false; 85 } 86 } 87 } 88 } 89 return 0; 90 }