http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610

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 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).

输入

     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)

输出

    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.

示例输入

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

示例输出

Case #1:
13
7
3
6
9

提示

 

来源

 2013年山东省第四届ACM大学生程序设计竞赛

示例程序

分析:

题意是说给你一串数字,求在动态区间[L , R]内的在[A , B]范围内的数的个数。

数据很大,一般的方法会超。

官方标程:

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; #define lson l, m, rt->left
#define rson m + 1, r, rt->right const int maxn = ; struct Node {
int sum;
Node *left, *right;
}*root[maxn], tree[], *idx; int num[maxn], p[maxn], mp[maxn];
int n, m;
int tol; inline Node* nextNode() {
idx->sum = ;
idx->left = idx->right = NULL;
return idx++;
} inline Node* copyNode(Node* temp) {
idx->sum = temp->sum;
idx->left = temp->left;
idx->right = temp->right;
return idx++;
} inline bool cmp(int a, int b) {
return num[a] < num[b];
} void input() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &num[i]);
p[i] = i;
}
sort(p + , p + n + , cmp);
int pre = -0x7f7f7f7f;
tol = ;
for(int i = ; i <= n; i++) {
if(num[ p[i] ] == pre) {
num[ p[i] ] = tol;
}
else {
pre = num[ p[i] ];
mp[ ++tol ] = num[ p[i] ];
num[ p[i] ] = tol;
}
}
} void build(int l, int r, Node* rt) {
if(l == r) return;
int m = (l + r) >> ;
rt->left = nextNode();
rt->right = nextNode();
build(lson);
build(rson);
} int query(int ll, int rr, int l, int r, Node* rtl, Node* rtr) {
if(ll <= l && r <= rr) {
return rtr->sum - rtl->sum;
}
int m = (l + r) >> ;
int ret = ;
if(ll <= m) ret += query(ll, rr, l, m, rtl->left, rtr->left);
if(rr > m) ret += query(ll, rr, m + , r, rtl->right, rtr->right);
return ret;
} Node* add(int x, int l, int r, Node* rt) {
Node* temp = copyNode(rt);
if(l == r) {
temp->sum++;
return temp;
}
int m = (l + r) >> ;
if(x <= m) temp->left = add(x, lson);
else temp->right = add(x, rson);
temp->sum = temp->left->sum + temp->right->sum;
return temp;
} void build() {
idx = tree;
root[] = nextNode();
build(, tol, root[]);
for(int i = ; i <= n; i++) {
root[i] = add(num[i], , tol, root[i - ]);
}
} inline int bin1(int x) { //find the left-most number that is >= x
int left = , right = tol;
int ans = -;
while(left <= right) {
int mid = (left + right) >> ;
if(mp[mid] >= x) {
ans = mid;
right = mid - ;
}
else left = mid + ;
}
return ans;
} inline int bin2(int x) { // find the right-most number that is <= x
int left = , right = tol;
int ans = -;
while(left <= right) {
int mid = (left + right) >> ;
if(mp[mid] <= x) {
ans = mid;
left = mid + ;
}
else right = mid - ;
}
return ans;
} void solve() {
static int cas = ;
printf("Case #%d:\n", cas++);
while(m--) {
int l, r, a, b;
scanf("%d%d%d%d", &l, &r, &a, &b);
a = bin1(a);
b = bin2(b);
if(a == - || b == -) {
puts("");
continue;
}
printf("%d\n", query(a, b, , tol, root[l - ], root[r]));
}
} int main() {
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout); int __t;
scanf("%d", &__t);
while(__t--) {
input();
build();
solve();
}
return ;
}

sdutoj 2610 Boring Counting的更多相关文章

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

    Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you a ...

  2. SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)

    Boring Counting Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descriptio ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  5. 后缀数组 --- HDU 3518 Boring counting

    Boring counting Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...

  6. UPC 2224 / “浪潮杯”山东省第四届ACM大学生程序设计竞赛 1008 Boring Counting 主席树

    Problem H:Boring Counting Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/ ...

  7. SDIBT 3237 Boring Counting( 划分树+二分枚举 )

    http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec  ...

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

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  9. HDOJ 3518 Boring counting

    SAM基本操作 拓扑寻求每个节点  最左边的出现left,最右边的出现right,已经有几个num ...... 对于每个出现两次以上的节点.对其所相应的一串子串的长度范围 [fa->len+1 ...

随机推荐

  1. PHP 下的SSL加密设置

    这个是报的错[Composer\Downloader\TransportException] The . OpenSSL Error messages: error::SSL routines:SSL ...

  2. MyBatis的几种批量操作

    MyBatis中批量插入 方法一: <insert id="insertbatch" parameterType="java.util.List"> ...

  3. PHP 操作MySQL———来自copy

    学习要点:1.PHP 连接到MySQL2.增删改查3.其他常用函数 如果你已经具有了使用PHP.SQL 和MySQL 的丰富经验,现在就可以把所有这些技术组合在一起.PHP 与MySQL 之间稳固的集 ...

  4. Error #include nested too deeply

    转载:http://blog.csdn.net/ysdaniel/article/details/7043395 出现 Error #include nested too deeply 原因是: 头文 ...

  5. android-Activity(四大组件之一)

    一.Activity理解 1.定义: 直译为活动,是Android定义四大应用组件之一,也是最重要的用的最多的: 用来提供一个能让用户操作并与之交互的界面 一个应用有多个界面也就是包含多个Activi ...

  6. [NoSQL]验证redis的主从复制

    安装配置redis  http://www.cnblogs.com/myrunning/p/4222385.html 1.1查看当前redis文件 1.2修改配置文件 拷贝配置文件分别为redis_m ...

  7. CSS,bootstrap表格控制当td内容过长时用省略号表示,以及在不使用bootstrap时过长也用省略号表示

    首先需要在table中设置table-layout:fixed; <table style="table-layout:fixed"></table> 然后 ...

  8. javascript阻止事件冒泡的兼容写法及其相关示例

    //阻止事件冒泡的兼容写法 function stopBubble(e){ //如果提供了事件对象,则是一个非IE浏览器 if(e && e.stopPropagation) //因此 ...

  9. Excel报表开发

    读取Excel数据 /// <summary> /// 封装方法 /// </summary> /// <param name="path">& ...

  10. 让Session失效的三种方法

    我们设置SESSION失效的时间,是为了确保在用户长时间不与服务器交互的情况下,可以自动退出登录.本文介绍了三种设置SESSION失效的方法,希望对你有帮助. Session对象是HttpSessio ...