http://www.lydsy.com/JudgeOnline/problem.php?id=1503

抄别人模板过的...打了点注释算是大概懂了...

/********************* Template ************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define BUG         cout<<" BUG! "<<endl;
#define LINE        cout<<" ------------------ "<<endl;
#define FIN         freopen("in.txt","r",stdin);
#pragma comment     (linker,"/STACK:102400000,102400000")
template<class T> inline T L(T a)       {return (a << 1);}
template<class T> inline T R(T a)       {return (a << 1 | 1);}
template<class T> inline T lowbit(T a)  {return (a & -a);}
template<class T> inline T Mid(T a,T b) {return ((a + b) / 2);}
template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;}
template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;}
template<class T> inline T Min(T a,T b) {return a < b ? a : b;}
template<class T> inline T Max(T a,T b) {return a > b ? a : b;}
template<class T> inline T Min(T a,T b,T c)     {return min(min(a,b),c);}
template<class T> inline T Max(T a,T b,T c)     {return max(max(a,b),c);}
template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
template<class T> inline void mem(T &a,int b)   {memset(a,b,sizeof(a));}
template<class T> inline T exGCD(T a, T b, T &x, T &y){ //
    if(!b) return x = 1,y = 0,a;T res = exGCD(b,a%b,x,y),tmp = x;x = y,y = tmp - (a / b) * y;return res;
}
typedef long long LL;    typedef unsigned long long ULL;
//typedef __int64 LL;      typedef unsigned __int64 ULL;
const LL LINF   = 1LL << 60;
const int MOD   = 1000000007;
const int INF   = 0x7fffffff;
const int MAXN  = (int)1e5+5;
const double EPS    = 1e-8;
const double DINF   = 1e15;
const double PI     = acos(-1.0);/*********************   By  F   *********************/
int n,lim,num,ans;
char op;
struct splay{int ch[MAXN][2];    // 0:左节点 1:右节点int pre[MAXN];      // 父节点int cnt[MAXN];int sz[MAXN];       // size[] 保存节点int val[MAXN];int root,top;int sum;inline void up(int x){sz[x] = cnt[x] + sz[ch[x][0]] + sz[ch[x][1]];}inline void Rotate(int x,int c){    //  0 左旋, 1 右旋int y = pre[x],z = pre[y];      //  y为x的父亲,z为y的父亲ch[y][!c] = ch[x][c];           //  举例-> 如果右旋:x的右节点 -> y的左节点pre[ch[x][c]] = y;              //  标记: x右节点的父亲 -> ypre[x] = z;                     //  y旋转到x下面 -> 标记: x的父节点 -> y的父节点if(z != 0)                      //  若y不是根节点,y的父节点转为x的父节点ch[z][ch[z][1] == y] = x;ch[x][c] = y;                   //  x的右节点已经给了y的左节点,所以x的右节点变为ypre[y] = x;                     //  y的父节点变为xup(y);                          //  ?
    }inline void Splay(int x,int goal){while(pre[x] != goal){if(pre[pre[x]] == goal) Rotate(x,ch[pre[x]][0] == x);   //  如果x的祖父是目标节点,只需要转一次else{int y = pre[x],z = pre[y],c = (ch[z][0] == y);if(ch[y][c] == x) Rotate(x,!c),Rotate(x,c);         //  z字旋转 x左右各转一次else Rotate(y,c),Rotate(x,c);                       //  1字旋转 先转y 后转x
            }}up(x);                          //  ?if(goal == 0) root = x;         //  0节点是根节点的父节点,如果要转到0下 则该点为根
    }inline void Init(){                 //  初始化ch[0][0] = ch[0][1] = pre[0] = sz[0] = cnt[0] = sum = root = top = 0;}
//    inline void RTO(int k,int goal){    //  将第k位数旋转到goal的下面
//        int x = root;
//        while(sz[ch[x][0]] != k-1){
//            if(k < sz[ch[x][0]]+1) x = ch[x][0];
//            else{
//                k -= (sz[ch[x][0]]+1);
//                x = ch[x][1];
//            }
//        }
//        Splay(x,goal);
//    }inline void Insert(int &x,int key,int f){if(!x){                         //  如果x是叶子节点x = ++top;                  //  节点标号,从1开始ch[x][0] = ch[x][1] = 0;    //  子节点设置0sz[x] = cnt[x] = 1;val[x] = key;pre[x] = f;Splay(x,0);                 //  将该点转到根return ;}if(key == val[x]){              //  如果权值重复cnt[x]++;                   //  cnt[x]++sz[x]++;                    //  size[x]++Splay(x,0);                 //  将该点转到根return ;}else if(key < val[x]) Insert(ch[x][0],key,x);   //  保证平衡树的性质else if(key > val[x]) Insert(ch[x][1],key,x);up(x);}inline void del(int &x,int f){if(!x) return ;                 //  如果x是叶子节点if(val[x] >= lim){              //  从x左边开始删del(ch[x][0],x);}else{sum += sz[ch[x][0]] + cnt[x];x = ch[x][1];pre[x] = f;if(f == 0) root = x;del(x,f);}if(x) up(x);}inline int find_kth(int x,int k){if(k < sz[ch[x][0]]+1) return find_kth(ch[x][0],k);else if(k > sz[ch[x][0] ] + cnt[x])return find_kth(ch[x][1],k-sz[ch[x][0]]-cnt[x]);else{Splay(x,0);return val[x];}}inline void show(int x){if(x){printf("%d: Left: %d Right: %d Val: %d Size: %d\n",x,ch[x][0],ch[x][1],val[x],sz[x]);show(ch[x][0]);show(ch[x][1]);}}
}s;
int main(){while(~scanf("%d%d",&n,&lim)){s.Init();int limit = lim;for(int i = 0 ; i < n ; i++){getchar();scanf("%c %d",&op,&num);if(op == 'I'){if(num < limit) continue;s.Insert(s.root,num+lim-limit,0);   //  从根节点开始插入
            }if(op == 'A') lim -= num;if(op == 'S'){lim += num;if(s.sz[s.root] > 0) s.del(s.root,0);}if(op == 'F'){int sz = s.sz[s.root];if(num > sz) printf("-1\n");else printf("%d\n",s.find_kth(s.root,sz-num+1)-lim+limit);}//s.show(s.root);
        }printf("%d\n",s.sum);}return 0;
}

转载于:https://www.cnblogs.com/Felix-F/p/3325518.html

NOI2004 郁闷的出纳员 splay相关推荐

  1. [BZOJ1503][NOI2004]郁闷的出纳员 无旋Treap

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MB Description OIER公司是一家大型专业化软件公司,有着数以万计的员 ...

  2. 洛谷P1486 [NOI2004] 郁闷的出纳员 题解

    P1486 [NOI2004] 郁闷的出纳员 题目链接:P1486 [NOI2004] 郁闷的出纳员 题意:维护一个数据结构,支持 插入一个大小为 kkk​ 的值,小于下界时不插入 所有元素加上 kk ...

  3. bzoj1503: [NOI2004]郁闷的出纳员

    地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题目: 1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  ...

  4. 【题解】 bzoj1503: [NOI2004]郁闷的出纳员 (Splay)

    bzoj1503,懒得复制,戳我戳我 Solution: 我知不知道我是那根筋抽了突然来做splay,调了起码\(3h+\),到第二天才改出来(我好菜啊),当做训练调错吧 一个裸的splay,没啥好说 ...

  5. BZOJ1503:[NOI2004]郁闷的出纳员——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1503 (题面复制的洛谷的,因为洛谷好看) 题目描述 OIER公司是一家大型专业化软件公司,有着数以万 ...

  6. P1486 [NOI2004]郁闷的出纳员

    题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资 ...

  7. 洛谷 P1486 [NOI2004]郁闷的出纳员

    题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资 ...

  8. 950. 郁闷的出纳员(Splay树)

    OIER 公司是一家大型专业化软件公司,有着数以万计的员工. 作为一名出纳员,我的任务之一便是统计每位员工的工资. 这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资. ...

  9. NOI2004郁闷的出纳员题解

    描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资.如 ...

  10. 郁闷的出纳员(splay, 树状数组可做)

    题面 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资.如 ...

最新文章

  1. 快速构建深度学习图像数据集,微软Bing和Google哪个更好用?
  2. 第三方医药物流的现状及发展
  3. 互联网公益平台米多乐获近千万天使轮融资,熊猫资本投资...
  4. LeetCode:3. Longest Substring Without Repeating Characters
  5. 牛客 - 车辆调度(dfs)
  6. 线程池应该设置多少核心线程数——Java多线程系列学习笔记
  7. jz2440-uboot-201204版本移植【学习笔记】【原创】
  8. ASP.NET Core+Quartz.Net实现web定时任务
  9. amos调节变量怎么画_AMOS 中验证性因素分析(CFA)
  10. vue中if和for指令不能同时使用
  11. SMS2003 推送软件包中的日志查看及安装错误代号1633分析
  12. 音乐网站搭建源码Sourcecode
  13. Linux移植笔记--arm64上的Linux系统移植
  14. webservice视频教程-基于SOA 思想下的WebService实战
  15. 电机不动 米兔机器人_深度揭秘米兔积木机器人八大黑科技
  16. 接口测试工具apipost关于post请求
  17. oracle文本类型字段,Oracle字符的5种类型的介绍
  18. outlook服务器邮件满了怎么办,Outlook邮箱不能接收邮件提示邮件箱已满的解决方法...
  19. 4. Zigbee应用程序框架开发指南 - 生成应用程序配置文件
  20. CAT1模块EC200S 4G物联网模块串口透传MQTT协议 快速入门指导资料

热门文章

  1. Kubernetes架构图 Architecture Workload Networking Storage RBAC
  2. kubernetes RC(Replication Controller)和RS(Replicaset)的作用和区别
  3. HTTPSConnectionPool(host=‘api.github.com‘, port=443): Max retries exceeded with url
  4. thinkphp 同时更新多条数据
  5. Process 获取子进程输入流、杀死子进程
  6. c# 如何抓微信把柄_抓住把柄表情包 - 抓住把柄微信表情包 - 抓住把柄QQ表情包 - 发表情 fabiaoqing.com...
  7. c语言 屏幕亮度调节_4096级屏幕亮度调节:改善安卓机自动亮度调节顽疾
  8. Git 问题:SSL certificate problem: self signed certificate
  9. CF125E MST Company
  10. 用简单直白的方式讲解A星寻路算法原理