hdu 6375 Science Degree, bear queue (list analog)

 

Bears are learning degree of double-ended queue, he had a great interest in its flip and merge.

When the initial  N  empty deque (numbered  1  to  N  ), you have to bear the support of  Q  operations.

. 1  u  W  V A L  numbered  u  queue and a weight added value of  V A L  elements. ( W = 0  indicates applied on top, W = . 1  represents added at the end face).

2  u  w  inquiry number  u  is an element of the queue, and delete it. W = 0  indicating an inquiry and the operation of the front element, W = . 1  represents rearmost)

. 3  U v  W  the number  v  queue "connected" number  u  queue last. W = 0  indicates the order of connection (queue  v  at the beginning of the queue and  u  end together, queue v  trailing end of a new queue),  W = . 1  represents a reverse connection (first queue  v  inverted, and then connected to the queue sequence  u  Behind). And after the operation is complete queue  v  is emptied.

 

 

 

Doubly linked list maintenance simulation, the time to pay attention to the deleted node determines on which side after deletion.

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e7+10;
int n, q, clk;
int head[N], tail[N];
struct _ {int l,r,v;} a[N];
int tim [N] you;
void upd(int x) {
    if (tim[x]!=ti) tim[x]=ti,head[x]=tail[x]=0;
}
void addL(int id, int v) {
    upd(id);
    a[++clk].v = v;
    if (!head[id]) {
        head[id] = tail[id] = clk;
        a[clk].l = a[clk].r = -1;
    }
    else {
        if (a[head[id]].l==-1) { 
            a[head[id]].l = clk;
            a[clk].l = -1;
            a[clk].r = head[id];
        }
        else { 
            a[head[id]].r = clk;
            a[clk].r = -1;
            a[clk].l = head[id];
        }
        head[id] = clk;
    }
}
void addR(int id, int v) {
    upd(id);
    a[++clk].v = v;
    if (!tail[id]) {
        head[id] = tail[id] = clk;
        a[clk].l = a[clk].r = -1;
    }
    else {
        if (a[tail[id]].l==-1) {
            a[tail[id]].l = clk;
            a[clk].l = -1;
            a[clk].r = tail[id];
        }
        else {
            a[tail[id]].r = clk;
            a[clk].r = -1;
            a[clk].l = tail[id];
        }
        tail[id] = clk;
    }
}
void delL(int id) {
    upd(id);
    if (!head[id]) return puts("-1"),void();
    printf("%d\n", a[head[id]].v);
    if (a[head[id]].l==-1&&a[head[id]].r==-1) {
        head[id] = tail[id] = 0;
		return;
    }
	int t = head[id];
	if (a[t].l==-1) head[id] = a[t].r;
	else head[id] = a[t].l;
	if (a[head[id]].r==t) a[head[id]].r = -1;
	else a[head[id]].l = -1;
}
void delR(int id) {
    upd(id);
    if (!tail[id]) return puts("-1"),void();
    printf("%d\n", a[tail[id]].v);
    if (a[tail[id]].l==-1&&a[tail[id]].r==-1) {
        head[id] = tail[id] = 0;
		return;
    }
	int t = tail[id];
    if (a[t].l==-1) tail[id] = a[t].r;
	else tail[id] = a[t].l;
	if (a[tail[id]].l==t) a[tail[id]].l = -1;
	else a[tail[id]].r = -1;
}
void reverse(int id) {
    upd(id);
    swap(head[id],tail[id]);
}
void get (int x, int y) {
    if (x==y) return;
    upd (x), upd (y);
    if (!head[y]) return;
    if (!head[x]) {
        head[x] = head[y];
        tail[x] = tail[y];
    }
    else {
        if (a[tail[x]].l==-1) a[tail[x]].l = head[y];
        else a[tail[x]].r = head[y];
        if (a[head[y]].l==-1) a[head[y]].l = tail[x];
        else a[head[y]].r = tail[x];
        tail[x] = tail[y];
    }
    head[y] = tail[y] = 0;
}

void read(int &x){
    scanf("%d",&x);
}

int main () {
    while (~scanf("%d%d", &n, &q)) { 
        ++ ti;
        while (q--) {
            int on, u, v, w;
            read(op),read(u),read(v);
            if (at == 1) {
                read(w);
                if (v==0) addL(u,w);
                else addR(u,w);
            }
            else if (up == 2) {
                if (v==0) delL(u);
                else delR(u);
            }
            else {
                read(w);
                if (w) reverse(v);
                go (u, v);
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/uid001/p/11139806.html