zoukankan      html  css  js  c++  java
  • HDU 2871 Memory Control(线段树)

    HDU 2871 Memory Control

    题目链接

    题意:内存操作,和hotel那题几乎相同,多一个get操作

    思路:线段树区间合并,其它都几乎相同,多一个get操作,这个用set去乱搞就过了- -。预计数据鶸吧,多这个操作感觉要用splay去搞了

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <set>
    using namespace std;
    
    const int N = 50005;
    #define MP(a,b) make_pair(a,b)
    typedef pair<int, int> pii;
    
    int n, m;
    
    set<pii> g;
    set<pii>::iterator it;
    
    #define lson(x) ((x<<1)+1)
    #define rson(x) ((x<<1)+2)
    
    struct Node {
        int l, r, lsum, rsum, sum, lazy;
        int size() {return r - l + 1;}
        void gao(int v) {
            lazy = v;
            lsum = rsum = sum = v * size();
        }
    } node[N * 4];
    
    void pushdown(int x) {
        if (node[x].lazy != -1) {
            node[lson(x)].gao(node[x].lazy);
            node[rson(x)].gao(node[x].lazy);
            node[x].lazy = -1;
        }
    }
    
    void pushup(int x) {
        node[x].lsum = node[lson(x)].lsum;
        node[x].rsum = node[rson(x)].rsum;
        node[x].sum = max(node[lson(x)].sum, node[rson(x)].sum);
        if (node[lson(x)].lsum == node[lson(x)].size())
            node[x].lsum += node[rson(x)].lsum;
        if (node[rson(x)].rsum == node[rson(x)].size())
            node[x].rsum += node[lson(x)].rsum;
        node[x].sum = max(node[x].sum, node[lson(x)].rsum + node[rson(x)].lsum);
    }
    
    void build(int l, int r, int x = 0) {
        node[x].l = l; node[x].r = r; node[x].lazy = -1;
        if (l == r) {
            node[x].lsum = node[x].rsum = node[x].sum = 1;
            return;
        }
        int mid = (l + r) / 2;
        build(l, mid, lson(x));
        build(mid + 1, r, rson(x));
        pushup(x);
    }
    
    void add(int l, int r, int v, int x = 0) {
        if (node[x].l >= l && node[x].r <= r) {
            node[x].gao(v);
            return;
        }
        int mid = (node[x].l + node[x].r) / 2;
        pushdown(x);
        if (l <= mid) add(l, r, v, lson(x));
        if (r > mid) add(l, r, v, rson(x));
        pushup(x);
    }
    
    #define INF 0x3f3f3f3f
    
    int query(int v, int x = 0) {
        if (node[x].l == node[x].r)
            return node[x].l;
    	int mid = (node[x].l + node[x].r) / 2;
    	pushdown(x);
    	int ans = INF;
    	if (node[lson(x)].sum >= v) ans = query(v, lson(x));
    	else if (node[lson(x)].rsum + node[rson(x)].lsum >= v) ans = node[lson(x)].r - node[lson(x)].rsum + 1;
    	else if (node[rson(x)].sum >= v) ans = query(v, rson(x));
    	pushup(x);
    	return ans;
    }
    
    int main() {
    	while (~scanf("%d%d", &n, &m)) {
    		build(1, n);
    		g.clear();
    		char op[15];
    		int x;
    		while (m--) {
    			scanf("%s", op);
    			if (op[0] == 'R') {
    				printf("Reset Now
    ");
    				add(1, n, 1);
    				g.clear();
    				continue;
    			}
    			scanf("%d", &x);
    			if (op[0] == 'N') {
    				int v = query(x);
    				if (v != INF) {
    					printf("New at %d
    ", v);
    					add(v, v + x - 1, 0);
    					g.insert(MP(v, v + x - 1));
    				}
    				else printf("Reject New
    ");
    			}
    			if (op[0] == 'G') {
    				if (g.size() < x) printf("Reject Get
    ");
    				else {
    					it = g.begin();
    					for (int i = 0; i < x - 1; i++)
    						it++;
    					printf("Get at %d
    ", it->first);
    				}
    			}
    			if (op[0] == 'F') {
    				it = g.upper_bound(MP(x, INF));
    				if (it == g.begin()) printf("Reject Free
    ");
    				else {
    					it--;
    					if (it->second < x) printf("Reject Free
    ");
    					else {
    						printf("Free from %d to %d
    ", it->first, it->second);
    						add(it->first, it->second, 1);
    						g.erase(it);
    					}
    				}
    			}
    		}
    		printf("
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    GIT基本概念和用法总结
    SELECT联动
    PHP无级分类续及搜索功能,分组分页
    PHP管理员登陆、验证与添加(前端验证)
    PHP手写cms 缓存Cache
    将本地文件上传到Ftp上的一些操作【转】
    SQL对时间的处理
    SQL Server游标的使用【转】
    修改数据表字段长度
    Web.Config加密【转】
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7301156.html
Copyright © 2011-2022 走看看