hdu4417 樹狀陣列(求指定區間比指定數小的數的個數)
http://acm.hdu.edu.cn/showproblem.php?pid=4417
Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every
integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
Output
For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
Sample Input
1
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
/**
hdu4417 樹狀陣列(求指定區間比指定數小的數的個數)
http://blog.csdn.net/wmn_wmn/article/details/8034181
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=300005;
int C[maxn],ans[maxn];
int n,m;
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=C[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=n)
{
C[x]+=d;
x+=lowbit(x);
}
}
struct node
{
int id,num;
bool operator < (const node &other) const
{
return num < other.num;
}
}a[maxn];
struct note
{
int l,r,id,value;
bool operator < (const note &other)const
{
return value <other.value;
}
}b[maxn];
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&a[i].num);
a[i].id=i+1;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].value);
b[i].l++;
b[i].r++;
b[i].id=i+1;
}
sort(a,a+n);
sort(b,b+m);
memset(C,0,sizeof(C));
int k=0;
for(int i=0;i<m;i++)
{
while(k<n&&a[k].num<=b[i].value)
{
add(a[k].id,1);
k++;
}
ans[b[i].id]=sum(b[i].r)-sum(b[i].l-1);
}
printf("Case %d:\n",++tt);
for(int i=1;i<=m;i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
}
相關文章
- 求區間不同數的個數【樹狀陣列求解】陣列
- 【樹狀陣列 求比其小的個數】poj 2353 Stars陣列
- 生成指定區間的隨機小數隨機
- D 區間求和 [數學 樹狀陣列]陣列
- JavaScript 獲取指定區間的數字JavaScript
- 【Leetcode每日一題】327. 區間和的個數(線段樹/樹狀陣列)LeetCode每日一題陣列
- HDU2689 Sort it (樹狀陣列求逆序數)陣列
- 在陣列中尋找和為指定值的兩個數陣列
- js如何獲取指定元素在陣列中個數JS陣列
- javascript實現獲取指定數字區間的隨機數JavaScript隨機
- Leetcode 327. 區間和的個數 (字首和 + 離散化 + 樹狀陣列)LeetCode陣列
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- 驗證數字具有指定的位數區間程式碼例項
- javascript如何獲取指定元素在陣列中的數目JavaScript陣列
- POJ 2299-Ultra-QuickSort(樹狀陣列求逆序數)UI陣列
- javascript保留指定位數小數程式碼JavaScript
- JS使用迴圈按指定倍數分割陣列組成新的陣列的方法JS陣列
- 樹狀陣列的區間查詢與區間修改陣列
- 實現保留指定數目小數的示例程式碼
- javascript擷取指定位數的小數程式碼例項JavaScript
- Java擷取指定區間內的陣列元素並存入新陣列Java陣列
- 輸入一個整數陣列,輸出奇偶數相間排列的陣列陣列
- [隨機數詳解]生成一個隨機數,生成指定範圍的隨機數及隨機陣列去重隨機陣列
- 【遞迴打卡2】求兩個有序陣列的第K小數遞迴陣列
- JavaScript如何求陣列的質數JavaScript陣列
- 求陣列中k個數的所有組合陣列
- js獲取指定區間的隨機數程式碼例項JS隨機
- python實現給定一個數和陣列,求陣列中兩數之和為給定的數Python陣列
- SPOJ DQUERY (離線數狀陣列||線上主席樹)陣列
- 1102:與指定數字相同的數的個數(C C++)C++
- 【樹狀陣列 區間更新區間查詢】code陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 求陣列內所有偶數的和陣列
- POJ 3067-Japan(樹狀陣列-逆序數)陣列
- 線段樹維護區間等差數列
- 2.6.6 指定程式的最大數量
- AcWing 242. 一個簡單的整數問題(樹狀陣列解法)陣列
- js實現從陣列中獲取相加和為指定數字的元素JS陣列