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 ...
随机推荐
- 读Thinking in java 4
读tij4 ,生活中还是杂事太多,有时候就忘了最初买书来读的初衷,也没了刚开始激情了,为了督促下自己,好好看完一本书,不妨来写写读书笔记吧.
- [CareerCup] 17.9 Word Frequency in a Book 书中单词频率
17.9 Design a method to find the frequency of occurrences of any given word in a book. 这道题让我们找书中单词出现 ...
- jsp...九九乘法表,三角形,菱形
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Linux_MySql安装
1.卸载原始mysql-lib sudo rpm -e --nodeps mysql-libs-xx 2.yum安装mysql-server sudo yum -y install mysql -se ...
- JavaScript:词法结构
1.字符集JavaScript程序是用Unicode字符集编写的. 1.1 区分大小写 JavaScript是区分大小写的语言.也就是说,关键字.变量.函数名和所有标识符(identifier)都必须 ...
- Struts的文件下载功能实现代码
Action: package com.tengfeiyang.action; import java.io.File; import java.io.FileInputStream; import ...
- css3 半个字符美化方法
<html lang="zh-CN"> <head> <title></title> <meta charset=" ...
- 一次有趣的XSS漏洞挖掘分析(2)
第一次和一套程序做了这么多次的斗争.今天基友又给我来信说,没得玩了.了解了下情况,是他拿着0day到处插,被人家发现了.还出了个公告,说所有***必须安装补丁.呵呵,性福总是走的这么突然.这乐子一下就 ...
- SpringMvc简单实例
Spring MVC应用一般包括4个步骤: (1)配置web.xml,指定业务层对应的spring配置文件,定义DispatcherServlet; (2)编写处理请求的控制器 (3)编写视图对象,例 ...
- Mysql忽略文件名的安全编码
author:skatetime:2014/09/28 mysql如何删除以"#sql-"开头的临时表 现象:在重建索引后,发现Mysql服务器的磁盘空间快满了 在用如下命令重建索 ...