sdutoj 2610 Boring Counting
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610
Boring Counting
Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
输入
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)
输出
示例输入
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
提示
来源
示例程序
分析:
题意是说给你一串数字,求在动态区间[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的更多相关文章
- sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)
Boring Counting Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In this problem you a ...
- SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)
Boring Counting Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descriptio ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- 后缀数组 --- HDU 3518 Boring counting
Boring counting Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...
- UPC 2224 / “浪潮杯”山东省第四届ACM大学生程序设计竞赛 1008 Boring Counting 主席树
Problem H:Boring Counting Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/ ...
- SDIBT 3237 Boring Counting( 划分树+二分枚举 )
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- HDOJ 3518 Boring counting
SAM基本操作 拓扑寻求每个节点 最左边的出现left,最右边的出现right,已经有几个num ...... 对于每个出现两次以上的节点.对其所相应的一串子串的长度范围 [fa->len+1 ...
随机推荐
- 【JAVA基础】 MAP 遍历
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- /var/log目录下的20个Linux日志文件功能详解
如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. 以下介绍的 ...
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree
2016-05-30 11:04:51 学习了link-cut-tree 二中神犇封禹的讲义感觉讲的超级清晰易懂啊(没有的可以q窝 算是模板吧 #include<bits/stdc++.h&g ...
- Java_Java SE6调用动态编译
转自:http://www.cnblogs.com/flyoung2008/archive/2011/11/14/2249017.html 一.使用JavaCompiler接口编译java源程序 我们 ...
- 再谈Jquery Ajax方法传递到action
原始出处 :http://cnn237111.blog.51cto.com/2359144/984466 本人只是转载 原文如下: 假设 controller中的方法是如下: public Act ...
- 转:DataGridView列的宽度、行的高度自动调整
注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. 介绍一下DataGridView列的宽度和行的高度,根据单元格或Header的内容(一般是内容全部被表示)自 ...
- [LintCode] Happy Number 快乐数
Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...
- 【LeetCode】1. Two Sum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 教你彻底解决css中设置z-index的值无效的问题
在使用z-index这个属性之前,我们必须先了解使用z-index的必要条件: 1.要想给元素设置z-index样式,必须先让它变成定位元素,说的明白一点,就是要给元素设置一个postion:rela ...
- wamp虚拟主机的配置 .
开发环境:WAMP 实例一,Apaceh配置localhost虚拟主机步骤 1,用记事本打开apache目录下httpd文件(如:D:\wamp\bin\apache\apache2.2.8\conf ...