BNU - Choosing a camera - 數據結構 (線段樹、隊列)

線段樹存的是更新到當前所有的相機,隊列則不管3721都把出現的camera塞進去。 

最後,在詢問那裏判斷。 一個一個出隊,詢問這個相機是否有用。

隊列是用了優先隊列。


題意是:相機有兩個參數和價格。

一個參數大於,一個參數大於等於剩下的所有相機,這個相機纔不過時。

我們需要在不過時的相機裏面挑選價格最便宜的。 價格相等的時候挑選最早出現的。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 10000
#define inf 999999999
vector <int> x, y;
bool type[maxn];
#define lson l, m, rt << 1
#define rson m + 1, r , rt << 1 | 1
class Tree{
    public :
	int val[maxn << 2];
	void build(int l, int r, int rt){
		val[rt] = 0;
		if(l == r) return ;
		int m = (l + r) >> 1;
		build(lson); build(rson);
	}
	void update(int pos, int cc, int l, int r, int rt){
		if(l == r){
			val[rt] = cc;
			return ;
		}
		int m = (l + r) >> 1;
		if(pos <= m) update(pos, cc, lson);
		else update(pos, cc, rson);
		val[rt] = max(val[rt << 1] , val[rt << 1 | 1]);
	}
	int find_max(int L, int R, int l, int r, int rt){
		if(L <= l && r <= R){
			return val[rt];
		}int m = (l + r) >> 1;
		int ret = 0;
		if(L <= m) ret = max(ret, find_max(L,R, lson));
		if(r > m) ret = max(ret, find_max(L,R, rson));
		return ret;
	}
};
struct Node{
	int p, cost, id;
	int ra;
	Node(){}
	Node(int xx, int yy, int cc, int idd):p(xx), ra(yy), cost(cc), id(idd) {
	}
}q[maxn];
struct cmp{
    bool operator ()(const Node & aa, const Node & bb){
        if(bb.cost == aa.cost) return aa.id > bb.id;
        return aa.cost > bb.cost;
    }
};
priority_queue < Node, vector< Node >, cmp > qu;

//promise the node in the tree is always the efficient ones
//find the smallest --- use Min
char op[10];
int ans[maxn], anssub;
int main(){
	int T,n;
	scanf("%d",&T);
	while(T --){
	    x.clear(), y.clear();
		scanf("%d",&n); double ka;
		for(int i = 1; i <= n; i ++){
			scanf("%s",op);
			if(op[0] == 'P'){
				scanf("%d%lf%d", &q[i].p, &ka, &q[i].cost);
				q[i].ra = ka * 1000000 + 1e-8;
				x.push_back( q[i].p );
				y.push_back( q[i].ra );
				type[i] = 0;
			}else{
				type[i] = 1;
			}
			q[i]. id = i;
		}
		sort(x.begin(), x.end());
		sort(y.begin(), y.end());
		int nx = unique(x.begin(), x.end()) - x.begin();
		int ny = unique(y.begin(), y.end()) - y.begin();

		x.erase( unique(x.begin(), x.end()) , x.end());
		y.erase( unique(y.begin(), y.end()) , y.end());

		Tree a, b;
		a.build(0, nx, 1);
		b.build(0, ny, 1);
        while(!qu.empty()) qu.pop();
        anssub = 0;
        //printf("%d  %d size \n", x.size(), y.size());
		for(int i = 1; i <= n; i ++){
			if(type[i] == 1){//query
                while(!qu.empty()){
                    int vx = qu.top().p;
                    int vy = qu.top(). ra;

                    //printf("%d  %d  %d now to deal\n", vx, vy, qu.top().id);
                    if(vx < b.find_max(vy, ny, 0, ny, 1) || vy < a.find_max(vx, nx, 0, nx, 1)){
                        qu.pop();
                        //printf(" kakka %d\n", qu.top().id);
                        continue;
                    }
                    break;
                }
                if(qu.empty()) ans[anssub ++] = -1;
                else {
                    ans[anssub ++] = qu.top().id;
                }
			}else{
                int vx = lower_bound(x.begin(), x.end(),q[i].p) - x.begin();
                int vy = lower_bound(y.begin(), y.end(),q[i].ra) - y.begin();
                a.update(vx, vy, 0, nx, 1);
                b.update(vy, vx, 0, ny, 1);
                qu.push(Node(vx , vy , q[i].cost, i));
			}
		}
		if(anssub > 0){
            for(int i = 0; i < anssub - 1; i ++){
                printf("%d ",ans[i]);
            }
            printf("%d\n",ans[anssub - 1]);
		}
	}
	return 0;
}


發佈了177 篇原創文章 · 獲贊 2 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章