【poj2761】 Feed the dogs

http://poj.org/problem?id=2761 (题目链接)

题意:求区间第K大。

Solution 
  和poj2104一模一样。

主席树代码:

// poj2761
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int M=10000010;
int a[M],b[M],lson[M],rson[M],T[M],c[M];
int n,tot,q;

int build(int l,int r) {
    int k=++tot;
    c[k]=0;
    if (l==r) return k;
    int mid=(l+r)>>1;
    lson[k]=build(l,mid);
    rson[k]=build(mid+1,r);
    return k;
}
int update(int l,int r,int K,int x) {
    int k=++tot;
    c[k]=c[K]+1;
    if (l==r) return k;
    int mid=(l+r)>>1;
    if (x<=mid) {
        rson[k]=rson[K];
        lson[k]=update(l,mid,lson[K],x);
    }
    else {
        lson[k]=lson[K];
        rson[k]=update(mid+1,r,rson[K],x);
    }
    return k;
}
int query(int l,int r,int k,int K,int x) {
    if (l==r) return l;
    int mid=(l+r)>>1;
    if (c[lson[K]]-c[lson[k]]>=x)
        return query(l,mid,lson[k],lson[K],x);
    else return query(mid+1,r,rson[k],rson[K],x-c[lson[K]]+c[lson[k]]);
}
int main() {
    while (scanf("%d%d",&n,&q)!=EOF) {
        tot=0;
        for (int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
        sort(b+1,b+1+n);
        int m=unique(b+1,b+1+n)-b-1;
        T[0]=build(1,m);
        for (int i=1;i<=n;i++) {
            int x=lower_bound(b+1,b+1+m,a[i])-b;
            T[i]=update(1,m,T[i-1],x);
        }
        while (q--) {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",b[query(1,m,T[l-1],T[r],k)]);
        }
    }
    return 0;
}

分块+莫队代码:

// poj2761
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=100010;
struct ask {int l,r,k,id;}t[maxn];
struct data {int w,id;}a[maxn];
int pos[maxn],cnts[maxn],b[maxn],p[maxn],ma[maxn],ans[maxn],n,m,s,q,block;

bool wcmp(data a,data b) {
    return a.w<b.w;
}
bool poscmp(ask a,ask b) {
    return pos[a.l]==pos[b.l] ? a.r<b.r : pos[a.l]<pos[b.l];
}
void build() {
    block=(int)sqrt((float)m);
    for (int i=1;i<=m;i++) pos[i]=(i-1)/block+1;
    s=m%block ? m/block+1 : m/block;
    for (int i=1;i<=s;i++) cnts[i]=0;
}
void update(int x,int val) {
    cnts[pos[x]]+=val;
    b[x]+=val;
}
int query(int k) {
    int tot=0;
    for (int i=1;i<=s;i++) {
        tot+=cnts[i];
        if (tot>=k) {
            tot-=cnts[i];
            for (int j=(i-1)*block+1;j<=min(i*block,m);j++) {
                if (tot+b[j]>=k) return ma[j];
                else tot+=b[j];
            }
        }
    }
}
int main() {
    scanf("%d%d",&n,&q);
    for (int i=1;i<=n;i++) scanf("%d",&a[i].w),a[i].id=i;
    for (int i=1;i<=q;i++) scanf("%d%d%d",&t[i].l,&t[i].r,&t[i].k),t[i].id=i;
    sort(a+1,a+1+n,wcmp);
    m=0;
    ma[p[a[1].id]=++m]=a[1].w;
    for (int i=2;i<=n;i++) {
        if (a[i].w!=a[i-1].w) m++;
        ma[p[a[i].id]=m]=a[i].w;
    }
    block=(int)sqrt((float)n);
    for (int i=1;i<=n;i++) pos[i]=(i-1)/block+1;
    sort(t+1,t+1+q,poscmp);
    build();
    for (int l=1,r=0,i=1;i<=q;i++) {
        for (;r<t[i].r;r++) update(p[r+1],1);
        for (;r>t[i].r;r--) update(p[r],-1);
        for (;l<t[i].l;l++) update(p[l],-1);
        for (;l>t[i].l;l--) update(p[l-1],1);
        ans[t[i].id]=query(t[i].k);
    }
    for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
    return 0;
}

  

时间: 2024-09-28 02:47:13

【poj2761】 Feed the dogs的相关文章

【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 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,

【POJ2761】【fhq treap】A Simple Problem with Integers

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

POJ2761 Feed the dogs

Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description 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 sp

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

                                                            Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 20634   Accepted: 6494 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed

【POJ3169 】Layout (认真的做差分约束)

Layout Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they ar

【git】git与github的英文记录

Pull requests  Issues Gist 请求 问题 要点 ------------------------------------------------------------------------------------------- Learn Git and GitHub without any code! 没有任何代码学习Git和GitHub! ---------------------------------------------------------------

机器学习资源大全【转】

本文汇编了一些机器学习领域的框架.库以及软件(按编程语言排序). C++ 计算机视觉 CCV —基于C语言/提供缓存/核心的机器视觉库,新颖的机器视觉库 OpenCV—它提供C++, C, Python, Java 以及 MATLAB接口,并支持Windows, Linux, Android and Mac OS操作系统. 通用机器学习 MLPack DLib ecogg shark Closure 通用机器学习 Closure Toolbox—Clojure语言库与工具的分类目录 Go 自然语

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

【转】Xamarin Forms 介绍

特此声明,本篇博文转自:http://blog.csdn.net/kinfey/article/details/29621381 什么是 Xamarin Forms ? Xamarin Forms 是一个高效创建跨平台用户界面的库 .通过Xamarin Forms 可以一次编码生成基于主流移动平台(iOS, Android, Windows Phone)的应用界面.和HTML 5 不同, 它是一套原生的界面解决方案,这意味着通过Xamarin Forms 渲染的界面是与底层API 紧密相连, 那