Just h-index 2018湘潭邀请赛


题目描述

The h-index of an author is the largest h where he has at least h papers with citations not less than h .

Bobo has published n papers with citations \(a_1 , a_2 ,..., a_n\) respectively. One day, he raises q questions. The $i_{th} $question is described by two integers li and ri , asking the h-index of Bobo if has only published papers with citations \(a_{li}, a_{li+1},..., a_{ri}\) .

输入

The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains two integers n and q . The second line contains n integers \(a_1 , a_2 ,..., a_n\) .

The i-th of last q lines contains two integers \(l_i\) and \(r_i\) .

输出

For each question, print an integer which denotes the answer.

? \(1 ≤ n, q ≤ 10^5\)

? \(1 ≤ a_i ≤ n\)

? \(1 \leq l_i \leq r_i \leq n\)

? The sum of n does not exceed 250,000.

? The sum of q does not exceed 250,000.

样例输入

5 3

1 5 3 2 1

1 3

2 4

1 5

5 1

1 2 3 4 5

1 5

样例输出

2

2

2

3

分析

??对权值通过维护一个可持久化线段树.

??将数组的区间\([l,r]\)抽象为第l到第r次修改产生的影响.

??每次查询的时候,我们应当找到满足\(x \in [l,r]\)且\(\sum_{i=x}^{r}a_i>=x\)的最大的\(x\)

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100050;
int n,q,a[maxn];
int sz,sum[maxn*20],lson[maxn*20],rson[maxn*20];
int update(int x,int l,int r,int id){
    int root=++sz;
    if(l==x&&r==x){
        sum[root]=sum[id]+1;
        lson[root]=rson[root]=0;
        return root;
    }
    int mid=(l+r)>>1;
    if(x<=mid){
        lson[root]=update(x,l,mid,lson[id]);
        rson[root]=rson[id];
    }
    else{
        lson[root]=lson[id];
        rson[root]=update(x,mid+1,r,rson[id]);
    }
    sum[root]=sum[lson[root]]+sum[rson[root]];
    return root;
}
int query(int suf,int l,int r,int r1,int r2){
//    printf("%d %d %d\n", k,l,r);
    if(l==r){
        return l;
    }
    int s=sum[rson[r2]]-sum[rson[r1]];
    int mid=(l+r)>>1;
    if(s+suf>=mid+1) return query(suf,mid+1,r,rson[r1],rson[r2]);
    else return query(s+suf,l,mid,lson[r1],lson[r2]);
}
int T[maxn];
int main(int argc, char const *argv[])
{
    while(~scanf("%d%d", &n,&q)){
        T[0]=0;
        sz=0;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &a[i]);
            T[i]=update(a[i],1,n,T[i-1]);
        }
        while(q--){
            int l,r;
            scanf("%d%d", &l,&r);
            printf("%d\n", query(0,1,n,T[l-1],T[r]));
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/sciorz/p/9058559.html

时间: 2024-10-15 04:27:16

Just h-index 2018湘潭邀请赛的相关文章

[题解]2018湘潭邀请赛

A.Easy h-index 题目描述 The h-index of an author is the largest h where he has at least h papers with citations not less than h.Bobo has published many papers. Given a0, a1, a2 ,..., an which means Bobo has published a i papers with citations exactly i ,

2018年湘潭邀请赛

题目链接: http://acm.hdu.edu.cn/listproblem.php?vol=53 A题: 题意: 总共有sum(a[i])篇文章,文章含有i条引用的文章数是ai,求最大的h使得最少有h篇文章含有至少h条引用. 思路: 二分,不过一开始把i和ai的含义读反wa了几发. 代码实现如下: 1 #include <set> 2 #include <map> 3 #include <deque> 4 #include <queue> 5 #incl

2018 CCPC 湘潭邀请赛 &amp; 2018 JSCPC

Problem A Problem B Problem C Problem D Problem E Problem F Problem G Problem H Problem I Problem J Problem K 原文地址:https://www.cnblogs.com/cxhscst2/p/9062107.html

[CCPC2017]湘潭邀请赛

题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/index/p/14/ D.模拟,按照原图每一个字符变成一个a*b的矩阵构造新矩阵. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 111; 5 int n, m, a, b; 6 char G[maxn][maxn]; 7 char GG[maxn][maxn]; 8 9 10 in

XTU 1261 - Roads - [最小割][2017年湘潭邀请赛(江苏省赛)B题]

之前在网上搜了一个下午没搜到这道题的题解,然后同时又对着叉姐写的两行字题解看了一个下午: 虽然基本上已经知道了这题的思路,但愣是因为自己代码实现起来太繁复,外加不确定正确性,没敢码-- 但是一道题肝了一下午没肝出来,就要放弃的话,怕是太扎心了,忍不住就跑去ICPCCamp.Post问叉姐了(https://post.icpc-camp.org/d/715-2017-b-roads) 看了叉姐的对于我的几个问题的回复,我总算肯定了我的思路,而且叉姐还在下面给了标程,当时可以说心情非常愉悦: 听起来

湘潭邀请赛——Alice and Bob

Alice and Bob Accepted : 133   Submit : 268 Time Limit : 1000 MS   Memory Limit : 65536 KB  Problem Description The famous "Alice and Bob" are playing a game again. So now comes the new problem which need a person smart as you to decide the winn

2017 CCPC 湘潭邀请赛

Problem A Problem B Problem C Problem D 直接输出即可. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 1e2 + 10; int c[N][N], f[N][N]; i

2019东北四省B,G,H,J 2018宁夏 A,F

The 13th Chinese Northeast Collegiate Programming Contest B. Balanced Diet #include<bits/stdc++.h> #define il inline #define pb push_back #define fi first #define se second #define ms(_data,v) memset(_data,v,sizeof(_data)) #define sc(n) scanf("

201⑨湘潭邀请赛 Chika and Friendly Pairs(HDU6534)

题意:给你一个数组,对于第i个数来说,如果存在一个位置j,使得j>i并且a[j]-k<=a[i]<=a[j]+k,那么这对数就称为好的,有q个询问,问你l到r区间有多少对好的数. 离线询问,想到可以用莫队维护区间,新加入元素(或删除元素)x时要统计区间[x-k,x+k]内的元素个数,想到 可以利用树状数组存元素个数(cnt)(权值数组),区间和就是元素个数,数据<=1e9,因此需要离散化a[i],a[i]+k,a[i]-k,记离散化后对应的数组为p1,p2,p3,每次区间增加下标为