【POJ - 2761】Feed the dogs 【主席树 求静态区间第k大】

本文介绍了一种使用主席树解决区间内第K小元素查询问题的方法。通过构建线段树并利用其版本控制特性,实现了在一系列离散数值中快速查找特定位置的值。适用于需要频繁进行区间更新及查询的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.

Your task is to help Jiajia calculate which dog ate the food after each feeding.
Input
The first line contains n and m, indicates the number of dogs and the number of feedings.

The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.

Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.

You can assume that n<100001 and m<50001.
Output
Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
分析 : 还好前几天硬着头皮把 可持久的01Trie弄懂了,主席树的主体思想和那个一样,每个数字都是一颗线段树,不过没有修改的部分直接连接上一个版本的。
代码 模板

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream> 
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 100001+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int root[MAXN<<5],lson[MAXN<<5],rson[MAXN<<5],sum[MAXN<<5],sz;
void Build(int& rt,int le,int ri){
    rt=++sz;   sum[rt]=0;
    if(le==ri) return ;
    int mid=(le+ri)>>1;
    Build(lson[rt],le,mid);
    Build(rson[rt],mid+1,ri);
}
void Update(int pre,int &rt,int le,int ri,int val){
    rt=++sz; 
    lson[rt]=lson[pre],rson[rt]=rson[pre],sum[rt]=sum[pre]+1; 
    if(le==ri) return ;
    int mid=(le+ri)>>1;
    if(val<=mid) Update(lson[pre],lson[rt],le,mid,val);
    else Update(rson[pre],rson[rt],mid+1,ri,val);
}
int Query(int st,int ed,int le,int ri,int k){
    if(le==ri) return le;
    int mid=(le+ri)>>1;
    int t=sum[lson[ed]]-sum[lson[st]] ;
    if(k<=t) Query(lson[st],lson[ed],le,mid,k);
    else Query(rson[st],rson[ed],mid+1,ri,k-t);
}

int a[MAXN],X[MAXN],cnt;
int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
            X[i]=a[i];
        }
        sort(X+1,X+1+n); cnt=unique(X+1,X+1+n)-X-1;
        sz=0; Build(root[0],1,cnt);
        for(int i=1;i<=n;i++){
            int t=lower_bound(X+1,X+1+cnt,a[i])-X;
            Update(root[i-1],root[i],1,cnt,t);
        }
        while(m--){
            int x,y,k;scanf("%d%d%d",&x,&y,&k);
            int ans=Query(root[x-1],root[y],1,cnt,k);
            printf("%d\n",X[ans]);
        }
    }
    return 0;
}
一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值