loj10145. 「一本通 4.6 练习 2」郁闷的出纳员

思路:

  简单的treap+模拟。

  但这道题的题面有误导群众的嫌疑,因为题中并没有明确刚进入公司就离开的算不算在离开公司的人中。

  当然,对于这道题的数据,题目意思是不算在内。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
const int maxn = 80010;
inline void qread(int &x) {
    x = 0;
    register int ch = getchar(), flag = 0;
    while(ch < ‘0‘ || ch > ‘9‘)        {
        if(ch == ‘-‘)    flag = 1;
        ch = getchar();
    }
    while(ch >= ‘0‘ && ch <= ‘9‘)    x = 10 * x + ch - 48, ch = getchar();
    if(flag)    x = -x;
}
int rt = 0, tpn, v[maxn], l[maxn], r[maxn], R[maxn], sz[maxn], vn[maxn];
inline void update(int x) {
    sz[x] = sz[l[x]] + sz[r[x]] + vn[x];
}
inline void zig(int &x) {
    int t = l[x];
    l[x] = r[t];
    r[t] = x;
    update(x);
    update(t);
    x = t;
}
inline void zag(int &x) {
    int t = r[x];
    r[x] = l[t];
    l[t] = x;
    update(x);
    update(t);
    x = t;
}
void insert(int &x, int w) {
    if(!x)    {
        x = ++tpn;
        v[x] = w;
        R[x] = rand();
        sz[x] = vn[x] = 1;
        return ;
    }
    if(w < v[x])    insert(l[x], w);
    else if(w > v[x])    insert(r[x], w);
    else            vn[x]++;
    update(x);
    if(l[x] && R[l[x]] < R[x])    zig(x);
    if(r[x] && R[r[x]] < R[x])    zag(x);
}
void pop(int &x, int w) {
    if(w < v[x])    pop(l[x], w);
    else if(w > v[x])    pop(r[x], w);
    else {
        if(vn[x] > 1)    vn[x]--;
        else if(!l[x] || !r[x])    x = l[x] | r[x];
        else if(R[l[x]] < R[r[x]])    zig(x), pop(x, w);
        else                        zag(x), pop(x, w);
    }
    update(x);
}
int ask(int x, int w){
    if(!x)        return -1;
    if(w < v[x])    return ask(l[x], w);
    if(w > v[x])    return ask(r[x], w);
    return     x;
}
int findr(int x, int w){
    int res = 0;
    while(x){
        if(v[x] == w)    return res + sz[l[x]] + 1;
        if(v[x] > w)    x = l[x];
        else            res += sz[l[x]] + vn[x], x = r[x];
    }
    return res;
}
int findk(int x, int k){
    if(k >= sz[l[x]] + 1 && k <= sz[l[x]] + vn[x])    return v[x];
    if(k <= sz[l[x]])    return findk(l[x], k);
    return     findk(r[x], k - sz[l[x]] - vn[x]);
}
int findpre(int x, int w){
    int res = -0x3f3f3f3f;
    while(x){
        if(w <= v[x])    x = l[x];
        else            res = v[x], x = r[x];
    }
    return res;
}
int findnxt(int x, int w){
    int res = 0x3f3f3f3f;
    while(x){
        if(w < v[x])    res = v[x], x = l[x];
        else            x = r[x];
    }
    return res;
}
int main(void) {
    srand(11535);
    int n, minn, nminn, ans = 0;
    qread(n);
    qread(minn);
    nminn = minn;
    while(n--){
        string op;
        int k;
        cin >> op;
        qread(k);
        if(op == "I"){
            if(k >= minn)
                insert(rt, k - minn + nminn);
        }
        else if(op == "A")
            nminn -= k;
        else if(op == "S"){
            nminn += k;
            int t;
            while((t = findpre(rt, nminn)) != -0x3f3f3f3f)
                pop(rt, t), ans++;
        }
        else
            printf("%d\n", k > sz[rt] ? -1 : findk(rt, sz[rt] - k + 1) + minn - nminn);
    }
    printf("%d\n", ans);
}

原文地址:https://www.cnblogs.com/junk-yao-blog/p/9483899.html

时间: 2024-07-30 02:20:28

loj10145. 「一本通 4.6 练习 2」郁闷的出纳员的相关文章

10249「一本通 1.3 例 5」weight

#10249「一本通 1.3 例 5」weight 题目描述 原题来自:USACO 已知原数列a1,a2,...,an中的前1项,前2项,前3项,... ,前 n 项的和,以及后 1 项,后 2 项,后 3 项,...,后 n 项的和,但是==所有的数都被打乱了顺序==.此外,我们还知道数列中的数存在于集合 S 中.试求原数列.当存在多组可能的数列时,求字典序最小的数列. 输入格式 第 1 行,一个整数 n . 第 2 行, 2 × n 个整数,注意:数据已被打乱. 第 3 行,一个整数 m ,

「一本通 4.1 练习 2」简单题

题目描述 题目来源:CQOI 2006 有一个 n 个元素的数组,每个元素初始均为 0.有 m 条指令,要么让其中一段连续序列数字反转--0 变 1,1变 0(操作 1),要么询问某个元素的值(操作 2). 例如当 n=20 时,10 条指令如下: 操作 回答 操作后的数组 1 1 10 N/A 11111111110000000000 2 6 1 11111111110000000000 2 12 0 11111111110000000000 1 5 12 N/A 11110000001100

「一本通 4.2 例 2」最敏捷的机器人(loj10120)

题目描述 Wind 设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了-- 机器人们都想知道谁是最敏捷的,于是它们进行了如下一个比赛.首先,他们面前会有一排共 n 个数,它们比赛看谁能最先把每连续 k 个数中最大和最小值写下来,当然,这些机器人运算速度都很快,它们比赛的是谁写得快. 但是 Wind 也想知道答案,你能帮助他吗? 输入格式 第一行为 n,k,意义如题目描述. 第二行共 n 个数,为数字序列,所有数字均在 Pascal 的 longint 范围内,即所有数均为整数,且

loj10139. 「一本通 4.5 练习 1」树上操作(loj2125. 「HAOI2015」树上操作 )

思路: 第二遍dfs时记录end[x]为在结点序列中以x为根的子树最后访问的节点,写个线段树标记下传即可.与值有关的数据注意long long #include<cstdio> #include<iostream> #include<vector> #include<cmath> #include<string> using namespace std; const int maxn = 100010; inline void qread(int

loj10143. 「一本通 4.6 例 1」营业额统计

思路: 使用treap在线存数据,每次取出新读入数据的前驱与后继,与该数据差值较小的就是.(注意若已有同一个数据,特判ans+=0) #include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> using namespace std; const int maxn = 100010; inline void qread(int &x) { x = 0; register

loj10144. 「一本通 4.6 练习 1」宠物收养所

思路: treap+简单模拟. 我只能说:注意取模!!!! #include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> using namespace std; const int maxn = 80010; inline void qread(int &x) { x = 0; register int ch = getchar(), flag = 0; while(ch

loj10141. 「一本通 4.5 练习 3」染色

思路: 利用树链剖分转化为线段树问题,考虑线段上经这两种操作后的sum求值方法,并着重考虑树链合并的情况即可.线段树为了打cov,要注意将所有的颜色统一加一. #include<cstdio> #include<iostream> using namespace std; const int maxn = 100010; inline void qread(int &x){ x = 0; register int ch = getchar(); while(ch <

loj10147. 「一本通 5.1 例 1」石子合并

思路: 经典的区间dp,题解到处都是... #include<cstdio> #include<iostream> #include<cstdio> using namespace std; const int maxn = 1010; inline void qread(int &x){ x = 0; register int ch = getchar(); while(ch < '0' || ch > '9') ch = getchar(); w

loj10159. 「一本通 5.2 练习 2」旅游规划

思路: 树形dp,最后用dfs标记所有的路径上的点,要注意可能会有多个点在以它为根的子树中的最长路径等于整棵树的最长路径,要分别作为初始点遍历整棵树. #include<cstdio> #include<iostream> #include<cstdlib> #include<vector> using namespace std; const int maxn = 200010; void qread(int &x) { x = 0; regist