Problem

查询区间第k大,但保证区间不互相包含(可以相交)

Solution

只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况。

所以我们暴力对下一个区间加上这个区间没有的点,删去下个区间没有的点。

因为每个点最多被加入,删除1次,所以时间复杂度为O(nlogn)

Notice

当相邻两段区间不相交时,那么我们要先加入点,在删去点。

Code

非旋转Treap

#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 = 100000, M = 50000;
const double eps = 1e-6, phi = acos(-1.0);
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 point = 0, T[N + 5], root, ans[M + 5];
struct Node
{
int left, right, ask, id;
}Q[M + 5];
struct node
{
int Size[N + 5], Val[N + 5], Level[N + 5], Son[2][N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
}
int Newnode(int v)
{
int u = ++point;
Val[u] = v, Level[u] = rand();
Size[u] = 1, Son[0][u] = Son[1][u] = 0;
return u;
}
int Merge(int X, int Y)
{
if (X * Y == 0) return X + Y;
if (Level[X] < Level[Y])
{
Son[1][X] = Merge(Son[1][X], Y);
up(X); return X;
}
else
{
Son[0][Y] = Merge(X, Son[0][Y]);
up(Y); return Y;
}
}
void Split(int u, int t, int &x, int &y)
{
if (!u)
{
x = y = 0;
return;
}
if (Val[u] <= t) x = u, Split(Son[1][u], t, Son[1][u], y);
else y = u, Split(Son[0][u], t, x, Son[0][u]);
up(u);
}
void Build(int l, int r)
{
int last, s[N + 5], top = 0;
rep(i, l, r)
{
int u = Newnode(T[i]);
last = 0;
while (top && Level[s[top]] > Level[u])
{
up(s[top]);
last = s[top--];
}
if (top) Son[1][s[top]] = u;
Son[0][u] = last;
s[++top] = u;
}
while (top) up(s[top--]);
root = s[1];
} int Find_num(int u, int t)
{
if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
else if (t <= Size[Son[0][u]] + 1) return u;
else return Find_num(Son[1][u], t - Size[Son[0][u]] - 1);
}
void Insert(int v)
{
int t = Newnode(v), x, y;
Split(root, v, x, y);
root = Merge(Merge(x, t), y);
}
void Delete(int v)
{
int x, y, z;
Split(root, v, x, z), Split(x, v - 1, x, y);
root = Merge(Merge(x, Merge(Son[0][y], Son[1][y])), z);
}
}Treap;
int cmp(Node X, Node Y)
{
return X.left < Y.left || (X.left == Y.left && X.right < Y.right);
}
int sqz()
{
int n = read(), m = read();
rep(i, 1, n) T[i] = read();
rep(i, 1, m) Q[i].left = read(), Q[i].right = read(), Q[i].ask = read(), Q[i].id = i;
sort(Q + 1, Q + m + 1, cmp);
rep(i, Q[1].left, Q[1].right) Treap.Insert(T[i]);
ans[Q[1].id] = Treap.Val[Treap.Find_num(root, Q[1].ask)];
rep(i, 2, m)
{
rep(j, Q[i - 1].right + 1, Q[i].right) Treap.Insert(T[j]);
rep(j, Q[i - 1].left, Q[i].left - 1) Treap.Delete(T[j]);
ans[Q[i].id] = Treap.Val[Treap.Find_num(root, Q[i].ask)];
}
rep(i, 1, m) printf("%d\n", ans[i]);
return 0;
}

旋转Treap

#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 = 100000, M = 50000;
const double eps = 1e-6, phi = acos(-1.0);
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 point = 0, T[N + 5], root, ans[M + 5];
struct node
{
int left, right, ask, id;
}Q[M + 5];
int cmp(node X, node Y)
{
return X.left < Y.left || (X.left == Y.left && X.right < Y.right);
}
struct Node
{
int Val[N + 5], Son[2][N + 5], Level[N + 5], Size[N + 5], Num[N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + Num[u];
}
inline void Lturn(int &x)
{
int y = Son[1][x]; Son[1][x] = Son[0][y]; Son[0][y] = x;
up(x), up(y); x = y;
}
inline void Rturn(int &x)
{
int y = Son[0][x]; Son[0][x] = Son[1][y]; Son[1][y] = x;
up(x), up(y); x = y;
}
inline void Newnode(int &u, int v)
{
u = ++point;
Level[u] = rand(), Val[u] = v;
Num[u] = Size[u] = 1, Son[0][u] = Son[1][u] = 0;
} void Insert(int &u, int t)
{
if (!u)
{
Newnode(u, t);
return;
}
Size[u]++;
if (t == Val[u]) Num[u]++;
else if (t < Val[u])
{
Insert(Son[0][u], t);
if (Level[Son[0][u]] < Level[u]) Rturn(u);
}
else
{
Insert(Son[1][u], t);
if (Level[Son[1][u]] < Level[u]) Lturn(u);
}
}
void Delete(int &u, int t)
{
if (!u) return;
if (Val[u] == t)
{
if (Num[u] > 1)
{
Size[u]--, Num[u]--;
return;
}
if (Son[0][u] * Son[1][u] == 0) u = Son[0][u] + Son[1][u];
else if (Level[Son[0][u]] < Level[Son[1][u]]) Rturn(u), Delete(u, t);
else Lturn(u), Delete(u, t);
}
else if (t < Val[u]) Size[u]--, Delete(Son[0][u], t);
else Size[u]--, Delete(Son[1][u], t);
} int Find_num(int u, int t)
{
if (!u) return 0;
if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
else if (t <= Size[Son[0][u]] + Num[u]) return u;
else return Find_num(Son[1][u], t - Size[Son[0][u]] - Num[u]);
}
}Treap;
int sqz()
{
int n = read(), m = read();
rep(i, 1, n) T[i] = read();
rep(i, 1, m) Q[i].left = read(), Q[i].right = read(), Q[i].ask = read(), Q[i].id = i;
sort(Q + 1, Q + m + 1, cmp);
rep(i, Q[1].left, Q[1].right) Treap.Insert(root, T[i]);
ans[Q[1].id] = Treap.Val[Treap.Find_num(root, Q[1].ask)];
rep(i, 2, m)
{
if (Q[i].left <= Q[i - 1].right)
{
rep(j, Q[i - 1].left, Q[i].left - 1) Treap.Delete(root, T[j]);
rep(j, Q[i - 1].right + 1, Q[i].right) Treap.Insert(root, T[j]);
}
else
{
rep(j, Q[i - 1].left, Q[i - 1].right) Treap.Delete(root, T[j]);
rep(j, Q[i].left, Q[i].right) Treap.Insert(root, T[j]);
}
ans[Q[i].id] = Treap.Val[Treap.Find_num(root, Q[i].ask)];
}
rep(i, 1, m) printf("%d\n", ans[i]);
return 0;
}

[POJ2761]Feed the dogs的更多相关文章

  1. 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs

    先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...

  2. [POJ2761] Feed the dogs (Treap)

    题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...

  3. [Poj2761]Feed the dogs(主席树)

    Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...

  4. 【POJ2761】【区间第k大】Feed the dogs(吐槽)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

  5. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  6. 划分树---Feed the dogs

    POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...

  7. poj2761 feed the dog

    题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...

  8. POJ 2761 Feed the dogs(平衡树or划分树or主席树)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

  9. POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs Time Limit: 6000MS   Memor ...

随机推荐

  1. QT文件(夹)操作---QFile、QDir、QFileInfo、QTextStream和QDataStream异同

    1.1    文件和目录 QFile.QBuffer和QTcpSocket可支持读写设备,用open函数打开,用write或putChar函数写入.用read和readLine或readAll进行读取 ...

  2. JAVA基础知识总结:十七

    一.转换流 作用:实现将字节流转换为字符流 a.InputStreamReader:字节字符转换输入流:字节输入流----->字符输入流 b.OutputStreamWriter:字节字符转换输 ...

  3. 全栈性能测试修炼宝典--Jmeter实战(一)

    性能测试方向职业发展 1.软件测试发展路线 我们可以暂且把软件测试职业路线分为3个方向,分别是业务路线.技术路线.管理路线:4个象限,分别为执行层.中层.中高层过渡.高层. (1)业务路线 常见业务路 ...

  4. Tomcat基本组件、其功能和处理请求的过程

      一.Tomcat是一个基于组件的服务器,它的构成组件都是可配置的,其中最外层的组件是Catalina Servlet容器,其他的组件按照一定的格式要求配置在这个顶层容器中 Tomcat的各个组件是 ...

  5. Codeforces 1151F Sonya and Informatics (概率dp)

    大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...

  6. XML注释与取消注释快捷键

    IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...

  7. python获取文件路径、文件名、后缀名的实例

    def jwkj_get_filePath_fileName_fileExt(filename): (filepath,tempfilename) = os.path.split(filename); ...

  8. 5月21 练习AJAX的查看详细及批量删除

    老师讲过之后的复习: 显示数据的代码部分: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  9. win10下使用python访问vmbox中的redis

    了解到redis没有windows的官方支持,所以在vmbox中的ubuntu装了redis#在ubuntu中 #搜索redis相关软件信息 apt-cache search ^redis #不清楚为 ...

  10. SpringBoot利用注解@Value获取properties属性为null

    参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...