提交记录 6972


用户 题目 状态 得分 用时 内存 语言 代码长度
zhylj noip17f. 【NOIP2017】列队 Accepted 100 684.752 ms 88740 KB C++ 3.76 KB
提交时间 评测时间
2018-11-25 10:40:26 2020-08-01 00:55:34
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 3e5 + 5;

ll n, m;

struct node {
    ll left, right, size;
    node *child[2], *father;
    
    void updata() {size = child[0]->size + child[1]->size + right - left + 1;}
} *nul = new node;

void init() {
    nul->child[0] = nul->child[1] = nul->father = nul;
    nul->left = 1; nul->right = nul->size = 0;
}

node *newNode(ll l, ll r, node *fa) {
    node *ptr = new node;
    ptr->father = fa; ptr->child[0] = nul; ptr->child[1] = nul;
    ptr->left = l; ptr->right = r; ptr->size = r - l + 1;
    return ptr;
}

void cect(node *x, node *y, ll p) {x->father = y; y->child[p] = x;}
bool getPos(node *x) {return x->father->child[1] == x;}

void rotate(node *x) {
    ll p = getPos(x), fp = getPos(x->father);
    node *gfa = x->father->father;
    cect(x->child[p ^ 1], x->father, p);
    cect(x->father, x, p ^ 1); cect(x, gfa, fp);
    x->child[p ^ 1]->updata(); x->updata();
}

void split(node *x, ll k) {
    node *tmpChild[2] = {x->child[0], x->child[1]};
    if(x->left < k) {
    	x->child[0] = newNode(x->left, k - 1, x);
    	if(tmpChild[0] != nul) cect(tmpChild[0], x->child[0], 0);
    	x->child[0]->updata();
    }
    if(x->right > k) {
    	x->child[1] = newNode(k + 1, x->right, x);
    	if(tmpChild[1] != nul) cect(tmpChild[1], x->child[1], 1);
    	x->child[1]->updata();
    }
    x->left = x->right = k;
}

struct splayTree {
    node *rt;

    splayTree() {rt = newNode(1, 0, nul);}

    void build(node *&cur, node *fa, ll l, ll r) {
        if(l <= r) {
            ll mid = (l + r) >> 1;
            cur = newNode(mid * m, mid * m, fa);
            build(cur->child[0], cur, l, mid - 1);
            build(cur->child[1], cur, mid + 1, r);
            cur->updata();
        }
    }

    void splay(node *cur, node *goal) {
        while(cur->father != goal) {
            node *fa = cur->father, *gfa = cur->father->father;
            if(gfa != goal) getPos(cur) == getPos(fa) ? rotate(fa) : rotate(cur);
            rotate(cur);
        }
    }

    node *findKth(ll k) {
        node *cur = rt->child[1];
    	while(true) {
            if(k <= cur->child[0]->size) cur = cur->child[0];
            else if(k > cur->child[0]->size + cur->right - cur->left + 1) {
            	k = k - cur->child[0]->size - (cur->right - cur->left + 1);
            	cur = cur->child[1];
            } else {
            	split(cur, cur->left + k - cur->child[0]->size - 1);
                splay(cur, rt);
                return cur;
            }
        }
    }

    ll earse(node *goal) {
        node *rplNode = goal->child[0];
        while(rplNode->child[1] != nul) rplNode = rplNode->child[1];
        if(rplNode != nul) {
            splay(rplNode, goal);
            cect(rplNode, rt, 1);
            cect(goal->child[1], rplNode, 1);
            rplNode->updata();
        } else cect(goal->child[1], rt, 1);
        ll id = goal->left;
        delete goal;
        return id;
    }

    void insLast(ll k) {
        node *cur = rt;
        while(cur->child[1] != nul) cur = cur->child[1];
        if(cur != rt) splay(cur, rt);
    	cur->child[1] = newNode(k, k, cur);
    	cur->size++;
    }
} row[MAXN], lastLine;

ll leave(ll x, ll y) {
    ll id;
    if(y != m) {
        lastLine.insLast(id = row[x].earse(row[x].findKth(y)));
        row[x].insLast(lastLine.earse(lastLine.findKth(x)));
    } else lastLine.insLast(id = lastLine.earse(lastLine.findKth(x)));
    return id;
}

int main() {
    init();
    ll q, x, y;
    scanf("%lld%lld%lld", &n, &m, &q);
    lastLine.build(lastLine.rt->child[1], lastLine.rt, 1, n);
    for(ll i = 1; i <= n; i++)
        (row[i].rt)->child[1] = newNode((i - 1) * m + 1, i * m - 1, row[i].rt);
    while(q--) {
        scanf("%lld%lld", &x, &y);
        printf("%lld\n", leave(x, y));
    }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #14.066 ms20 MB + 820 KBAcceptedScore: 5

Testcase #24.057 ms20 MB + 824 KBAcceptedScore: 5

Testcase #34.07 ms20 MB + 820 KBAcceptedScore: 5

Testcase #44.074 ms20 MB + 828 KBAcceptedScore: 5

Testcase #54.07 ms20 MB + 824 KBAcceptedScore: 5

Testcase #64.067 ms20 MB + 824 KBAcceptedScore: 5

Testcase #75.606 ms26 MB + 328 KBAcceptedScore: 5

Testcase #85.608 ms26 MB + 304 KBAcceptedScore: 5

Testcase #95.755 ms26 MB + 816 KBAcceptedScore: 5

Testcase #105.592 ms26 MB + 196 KBAcceptedScore: 5

Testcase #1199.305 ms26 MB + 200 KBAcceptedScore: 5

Testcase #1298.633 ms26 MB + 172 KBAcceptedScore: 5

Testcase #13401.231 ms37 MB + 376 KBAcceptedScore: 5

Testcase #14374.348 ms37 MB + 116 KBAcceptedScore: 5

Testcase #15396.001 ms71 MB + 544 KBAcceptedScore: 5

Testcase #16416.807 ms71 MB + 988 KBAcceptedScore: 5

Testcase #17172.79 ms42 MB + 552 KBAcceptedScore: 5

Testcase #18166.479 ms42 MB + 8 KBAcceptedScore: 5

Testcase #19684.752 ms85 MB + 88 KBAcceptedScore: 5

Testcase #20676.725 ms86 MB + 676 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-05-06 02:26:33 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用