【山东省第四节ACM省赛】Boring Counting(二分+划分树)

算是一道模板题了,可惜弱的一B的我并不会划分树,花了点时间学了下,回头A了这道题

3s的限时跑了2.8s也是醉了。。。

Description

In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each query, please tell us among [L, R], how many Pi is not less than A and
not greater than B( L<= i <= R). In other words, your task is to count the number of Pi (L <= i <= R,  A <= Pi <= B).

Input

In the first line there is an integer T (1 < T <= 50), indicates the number of test cases.

For each case, the first line contains two numbers N and M (1 <= N, M <= 50000), the size of sequence P, the number of queries. The second line contains N numbers Pi(1 <= Pi <= 10^9), the number sequence P. Then there are M lines, each line contains
four number L, R, A, B(1 <= L, R <= n, 1 <= A, B <= 10^9)

Output

For each case, at first output a line ‘Case #c:’, c is the case number start from 1. Then for each query output a line contains the answer.

Sample Input

1 13 
5 6 
9 5 2 3 6 8 7 3 2 5 1 4 
1 13 1 10
1 13 3 6
3 6 3 6
2 8 2 8
1 9 1 9

Sample Output

Case #1:
1
3
7
3
6
9

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 50005;
int n,m;
int tree[20][maxn];
int lnum[20][maxn];
int sorted[maxn];
void debug(int v){
    for(int i = 1; i <= n; i++) printf("%d ",sorted[i]);
    puts("");
    for(int i = 0; i < v; i++){
        for(int j = 1; j <= n; j++)
            printf("%d ",tree[i][j]);
        puts("");
    }
}
void build(int l,int r,int d){
    if(l == r) return;
    int mid = (l + r) >> 1;
    int same = mid - l + 1;
    for(int i = l; i <= r; i++)
        if(tree[d][i] < sorted[mid])
            same --;
    int lpos = l,rpos = mid + 1;
    lnum[d][0] = 0;
    for(int i = l; i <= r; i++){
        if(tree[d][i] < sorted[mid]){
            lnum[d][i] = lnum[d][i - 1] + 1;
            tree[d + 1][lpos++] = tree[d][i];
        }
        else if(tree[d][i] == sorted[mid] && same > 0){
            same --;
            lnum[d][i] = lnum[d][i - 1] + 1;
            tree[d + 1][lpos++] = tree[d][i];
        }
        else{
            lnum[d][i] = lnum[d][i - 1];
            tree[d + 1][rpos++] = tree[d][i];
        }
    }
    build(l,mid,d + 1);
    build(mid + 1,r,d + 1);
}
int query(int L,int R,int l,int r,int d,int k){
    //printf("%d %d %d %d\n",l,r,d,k);
    if(l == r) return tree[d][l];
    int mid = (L + R) >> 1;
    int cnt = lnum[d][r] - lnum[d][l - 1];
    if(cnt >= k){
        int newl = L + lnum[d][l - 1] - lnum[d][L - 1];
        int newr = newl + cnt - 1;
        return query(L,mid,newl,newr,d + 1,k);
    }
    else{
        int newr = r + lnum[d][R] - lnum[d][r];
        int newl = newr - (r - l - cnt);
        return query(mid + 1,R,newl,newr,d + 1,k - cnt);
    }
}
void solve(){
    int L,R,a,b;
    scanf("%d%d%d%d",&L,&R,&a,&b);
    int l = 1,r = (R - L + 2);
    int v1 = 0,v2 = 0;
    //找大于等于
    while(l < r){
        int mid = (l + r) >> 1;
        int value = query(1,n,L,R,0,mid);
        if(value < a)
            l = mid + 1;
        else if(value >= a)
            r = mid;
    }
    v1 = l;
    l = 1;
    r = (R - L + 2);
    //找大于
    while(l < r){
        int mid = (l + r) >> 1;
        int value = query(1,n,L,R,0,mid);
        if(value <= b){
            l = mid + 1;
        }
        else if(value > b)
            r = mid;
    }
    v2 = l;
    printf("%d\n",v2 - v1);
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++){
            scanf("%d",&tree[0][i]);
            sorted[i] = tree[0][i];
        }
        sort(sorted + 1,sorted + n + 1);
        build(1,n,0);
        printf("Case #%d:\n",Case++);
        for(int i = 0; i < m; i++)
            solve();
    }
    return 0;
}
/*
5 5
1 1 1 1 1
1 5 2 3
2
*/
时间: 2024-10-06 18:02:37

【山东省第四节ACM省赛】Boring Counting(二分+划分树)的相关文章

13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec  Memory Limit: 128 MB Description In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence.

2014山东省“浪潮杯”第五届ACM省赛总结

一次比赛做一次总结,弱菜又来总结了-- 我这种大四的又死皮赖来混省赛了,貌似就我和山大威海的某哥们(不详其大名)了吧.颁奖前和他聊天,得知他去百度了,真是不错,ORZ之. 比赛流水账: 题目目前不知道哪有,过几天填坑. 没发题目前,我们赌A题可能是水题,由于我是主coder,我去读A,剩下的一个从前往后,一个从后往前. 结果--,看到A有一个貌似是几何的图--,我还是硬头皮读了.读到一半,3分钟刷榜,发现E有出,让ZK读E,ZK先告诉我了B题题意,转而读E.B题是一个有环形关系的期望,我扫了一下

sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each quer

【SDUT OJ 2610】 Boring Counting(主席树)

[SDUT OJ 2610] Boring Counting(主席树) Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to ans

山东省第六届ACM省赛 H---Square Number 【思考】

题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct intege

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

本文出处:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411 题意:给你一个串,让你依据那个串来输出ppi.坑特别多.ppi的计算方法是dp / inches; dp = sqrt(wp*wp + hp * hp); 现在我来说说这个题目有多坑: 给你的串的格式是这样: name + inches+ "inches"

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

Time Limit: 5000MS Memory limit: 65536K 题目描写叙述 Haveyou ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for NvidiaTegra 2 based Android devices) is a video game developed by Hal

sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P. 1 <= n <= 10^6 0 <= A, K, a, b <= 10^9 1 <= m, P <= 10^9 本题目的关键在于大幂的分解和..你要这样想,因