zoukankan      html  css  js  c++  java
  • [HDU3436]Queue-jumpers

    Problem

    有一个数列,从1排列到n,然后有Q个操作

    1. Top x:将第x个数放到序列的最前面
    2. Query x:询问x这个数在第几位
    3. Rank x:询问第x位数是什么

    Solution

    n非常的大,需要离散化:读入的Query操作和Top操作需要离散化
    然后每当处理一个数时,用二分计算出离散化后的结果
    对于Top操作,先把那个数删掉,然后加在splay的最左边。

    Notice

    离散化非常复杂

    Code

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define sqz main
    #define ll long long
    #define reg register int
    #define rep(i, a, b) for (reg i = a; i <= b; i++)
    #define per(i, a, b) for (reg i = a; i >= b; i--)
    #define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
    const int INF = 1e9, N = 500000;
    const double eps = 1e-6, phi = acos(-1);
    ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
    ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
    void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
    int s[N + 5], e[N + 5], point = 0, root, now, id[N + 5];
    struct node
    {
    	int val[N + 5], Size[N + 5], num[N + 5], son[2][N + 5], parent[N + 5];
    	inline void up(int u)
    	{
    		Size[u] = Size[son[0][u]] + Size[son[1][u]] + num[u];
    	}
    	void Newnode(int &u, int from, int v)
    	{
    		u = ++point;
    		parent[u] = from, son[0][u] = son[1][u] = 0;
    		num[u] = Size[u] = e[v] - s[v] + 1;
    		val[u] = v, id[v] = u;
    	}
    	void Build(int &u, int l, int r, int from)
    	{
    		int mid = (l + r) >> 1;
    		Newnode(u, from, mid);
    		if (l < mid) Build(son[0][u], l, mid - 1, u);
    		if (mid < r) Build(son[1][u], mid + 1, r, u);
    		up(u);
    	}
    	int Find(int x)
    	{
    		int l = 1, r = now;
    		while (l <= r)
    		{
    			int mid = (l + r) >> 1;
    			if (x >= s[mid] && x <= e[mid]) return mid;
    			else if (x < s[mid]) r = mid - 1;
    			else l = mid + 1;
    		}
    	}
    	
    	void Rotate(int x, int &rt)
    	{
    		int y = parent[x], z = parent[y];
    		int l = (son[1][y] == x), r = 1 - l;
    		if (y == rt) rt = x;
    		else if (son[0][z] == y) son[0][z] = x;
    		else son[1][z] = x;
    		parent[x] = z;
    		parent[son[r][x]] = y, son[l][y] = son[r][x];
    		parent[y] = x, son[r][x] = y;
    		up(y);
    		up(x);
    	}
    	void Splay(int x, int &rt)
    	{
    		while (x != rt)
    		{
    			int y = parent[x], z = parent[y];
    			if (y != rt)
    			{
    				if ((son[0][z] == y) ^ (son[0][y] == x))
    					Rotate(x, rt);
    				else Rotate(y, rt);
    			}
    			Rotate(x, rt);
    		}
    	}
    
    	void Insert(int &u, int x, int last)
    	{
    		if (u == 0) 
    		{
    			Newnode(u, last, x);
    			return;
    		}	
    		else Insert(son[0][u], x, u);
    		up(u);
    	}
    	void Delete(int x)
    	{
    		Splay(x, root);
    		if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x];
    		else
    		{
    			int t = son[1][x];
    			while (son[0][t] != 0) t = son[0][t];
    			Splay(t, root);
    			son[0][t] = son[0][x], parent[son[0][x]] = t;
    			up(t);
    		}
    		parent[root] = 0;
    	}
    
    	int Find_rank(int x)
    	{
    		int t = id[Find(x)];
    		Splay(t, root);
    		return Size[son[0][root]] + 1;
    	}
    	int Find_num(int u, int k)
    	{
    		if (k <= Size[son[0][u]]) return Find_num(son[0][u], k);
    		else if (k <= Size[son[0][u]] + num[u]) return s[val[u]] + k - Size[son[0][u]] - 1;
    		else return Find_num(son[1][u], k - Size[son[0][u]] - num[u]);
    	}
    	void Top(int x)
    	{
    		int t = Find(x);
    		int y = id[t];
    		Delete(y);
    		Insert(root, t, 0);
    		Splay(point, root);
    	}
    }Splay_tree;
    int Q[N + 5], T[N + 5];
    char st[N + 5][10];
    int sqz()
    {
    	int H_H = read();
    	rep(cas, 1, H_H)
    	{
    		int n = read(), q = read(), num = 0;
    		Q[0] = 0;
    		rep(i, 1, q)
    		{
    			scanf("%s%d", st[i], &T[i]);
    			if (st[i][0] == 'T' || st[i][0] == 'Q') Q[++num] = T[i];
    		}
    		Q[++num] = n;
    		sort(Q + 1, Q + num + 1);
    		now = 0;
    		rep(i, 1, num)
    		{
    			if (Q[i] == Q[i - 1]) continue;
    			if (Q[i] - Q[i - 1] > 1)
    			{
    				s[++now] = Q[i - 1] + 1;
    				e[now] = Q[i] - 1;
    			}
    			s[++now] = e[now] = Q[i];
    		}
    		point = 0;
    		Splay_tree.son[0][0] = Splay_tree.son[1][0] = Splay_tree.parent[0] = Splay_tree.Size[0] = Splay_tree.val[0] = Splay_tree.num[0] = 0;
    		Splay_tree.Build(root, 1, now, 0);
    		printf("Case %d:
    ", cas);
    		rep(i, 1, q)
    			if (st[i][0] == 'T') Splay_tree.Top(T[i]);
    			else if (st[i][0] == 'Q') printf("%d
    ", Splay_tree.Find_rank(T[i]));
    			else printf("%d
    ", Splay_tree.Find_num(root, T[i]));
    	}
    }
    
  • 相关阅读:
    轻院:2211: 小明的有趣回文数
    轻院:2209: 小明找整数
    轻院:2206: 小明发福利
    轻院:2207: 小明被提问
    2135: 这里是天堂!
    牛客小白月赛2:文
    轻院2261: flower
    问题 I: 堆
    SharePoint Server 2010安装图解
    Microsoft Windows Sharepoint Services V3.0 安装图示
  • 原文地址:https://www.cnblogs.com/WizardCowboy/p/7631398.html
Copyright © 2011-2022 走看看