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 ...
随机推荐
- unix network programming volume1 sorce code build and get(UNIX網絡編程卷1第三版)
source code下载地址:unpv13e.tar.gz下载 (也有放一份在google cloud storage) compile 1. ./configure 2. cd lib make ...
- CentOS VirtualBox启动虚拟及报错:VirtualBox error: Kernel driver not installed (rc=1908)
VirtualBox error: Kernel driver not installed (rc=1908) Hi all, Let me first say that this is my fin ...
- 百度ueditor 拖文件或world 里面复制粘贴图片到编辑中 上传到第三方问题
我这边从world 里面复制粘贴图片到编辑器中,它自动给我上传了,但是我是用的第三方的要设置一个token值,我找了很久,也没有找到应该在哪里设置这个上传的参数,如果是点击图片上传,我知道在dialo ...
- HTML 5 服务器发送事件
接收 Server-Sent 事件通知 EventSource 对象用于接收服务器发送事件通知: 实例 var source=new EventSource("demo_sse.php&qu ...
- 安装时遇到:正在尝试其它镜像。 http://mirrors.btte.net/centos/7.2.1511/extras/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.btte.net; 未知的错误"
我出现这种错误是因为网络链接问题,因为我设置虚拟机网络链接为VmNET8,设置了nat模式,使得我本地机可以访问虚拟机的linux服务器.但是打开虚拟机的浏览器却不能上网了.所以现在我用xshell装 ...
- const 指针的三种使用方式
///////////////////////const 指针的三种状态///////////////////// 注意:const 的前后顺序 const 在类型之前 ---可以修改指针包含的地址, ...
- Linux LVM过程问题
问题: 使用fdisk 修改完成磁盘后,在/etc/下没有出现新建的分区文件 解决: 重启系统 (好吧,这他妈也算解决方案~~)
- Java碎片知识(笔记)
1.在java中有goto,但这只是保留字,并不能使用(const也是).在eclipse中的报错信息为”Syntax error on token "goto", throw e ...
- Python强化训练笔记(二)——元组元素的命名
对于一个元组如: >>> s1 = ('Jim', 21, 'boy', '5788236@qq.com') 我们要得到该对象的名字,年龄,性别及邮箱的方法为s1[0],s1[1], ...
- OpenERP/Odoo命令行参数
http://blog.sina.com.cn/s/blog_7cb52fa80102v8h1.html