首页 > 试题广场 >

数列

[问答题]

数列

题目描述给定两个长度为 n 的整数数列 AB。再给定 q 组查询,每次查询给出两个整数 xy,求满足 Ai >= xBi >= y 这样的 i 的数量。

输入格式

第一行给定两个整数 nq

第二行给定数列 A,包含 n 个整数。

第三行给定数列 B,包含 n 个整数。

接下来 q 行,每行两个整数 xy,意义如上所述。

输出格式

对于每组查询,输出所求的下标数量。

输入样例

3 2

3 2 4

6 5 8

1 1

4 8

输出样例

3

1

数据规模

对于 30% 的数据,1 <= n, q <= 100

对于 100% 的数据,1 <= n, q, Ai, Bi <= 10^5

推荐
#include <bits/stdc++.h>
using namespace std;//先以A对pair (A, B)排序, 树状数组维护i以后的比y大的个数
const int maxn = 100000 + 10;
struct node {
    int x, y, pos;
    bool operator < (const node &a) const {
        if (x == a.x)
            return y<a.y;
        return x<a.x;
    }
}p[maxn], q[maxn];
int A[maxn];
int B[maxn];
int lowbit(int i) {
    return i & (-i);
}
void add(int a, int i) {
    while (i <= maxn-1) {
        A[i] += a;
        i += lowbit(i);
    }
}
int sum(int i) {
    int res = 0;
    while (i >= 1) {
        res += A[i];
        i -= lowbit(i);
    }
    return res;
}
int main() {
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++) scanf("%d", &p[i].x);
    for(int i = 0; i < n; i++) scanf("%d", &p[i].y);
    sort(p, p+n);
    for(int i = 0; i < m; i++) {
        scanf("%d%d", &q[i].x, &q[i].y);
        q[i].pos = i;
    }
    for(int i = 0; i < n; i++)
        add(1, p[i].y);
    sort(q, q+m);
    int k = 0;
    for (int i = 0; i < n; i++) {
        if (p[i].x < q[k].x)
            add(-1, p[i].y);
        else {
            while (1) {
                B[q[k].pos] = sum(maxn - 1) - sum(q[k].y - 1);
                k++;
                if (k >= m)
                    break;                 if (p[i].x < q[k].x) {
                    add(-1, p[i].y);
                    break;
                }
            }
        }
        if (k >= m)
            break;
    }
    for (int i = 0; i < m; i++)
        printf("%d\n", B[i]);
    return 0;
}
编辑于 2017-07-28 16:52:35 回复(1)
private static int count(int[] A,int[] B,int x,int y){
        int length= A.length;
        int count=0;
        for (int i = 0; i < length; i++) {
            if (A[i]>=x&&B[i]>=y){
                count++;
            }
        }
        return count;
    }

发表于 2019-09-08 22:12:58 回复(0)
用控制台输入
public void answer(List<String> listAB, List<String> list) {
        //获取两个数列
        String strA = listAB.get(0);
        String strB = listAB.get(1);
        String[] arrA = strA.split(" ");
        String[] arrB = strB.split(" ");
        for (String string : list) {
            String[] splitArr = string.split(" ");
            int count = 0;
            for (int j = 0; j < arrA.length; j++) {
                if (Integer.parseInt(arrA[j]) >= Integer.parseInt(splitArr[0])
                        && Integer.parseInt(arrB[j]) >= Integer.parseInt(splitArr[1])) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }

发表于 2022-12-01 12:51:09 回复(0)
你们是不是想复杂了?这个只要A.B同时一次遍历判断就可以得出结果
发表于 2019-07-18 09:59:00 回复(0)
public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int q=sc.nextInt();
		int[] A=new int[n];
		int[] B=new int[n];
		int[] xy = new int[2*q];
		for (int i = 0; i < n; i++) {
			A[i] = sc.nextInt();
		}
		for (int i = 0; i < n; i++) {
			B[i] = sc.nextInt();
		}
		for(int i=0;i<(2*q);i++){
			xy[i]=sc.nextInt();
		}
		
		int count=0;
		for(int j=0;j<2*q;j+=2){
			for(int i=0;i<n;i++){
				if(A[i]>=xy[j]&&B[i]>=xy[j+1]){
					count+=1;
				}
			}
			System.out.println(count);
			count=0;
		}	
	} 

发表于 2017-09-01 15:46:43 回复(0)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;


int main()
{
int n,q;
cin >> n >> q;
int* a = new int[n];
int* b = new int[n];
int i=0;
for (i = 0;i < n;i++)
cin >> a[i];
for (i = 0;i < n;i++)
cin >> b[i];
while (q--)
{
int c[2];
cin >> c[0];
cin >> c[1];
int res = 0;
for (i = 0;i < n;i++)
{
if (a[i] >= c[0] && b[i] >= c[1])
res++;
}
cout << res << endl;
}
system("pause");
return 0;
}

发表于 2017-08-23 18:12:55 回复(0)
 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author Administrator
 *
 */
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n =sc.nextInt();
int q = sc.nextInt();
int n1[] = getArr(sc, n);
int n2[] = getArr(sc, n);
List<Integer> list = queryNum(sc, n, q, n1, n2);
printArr(list);
}
}
//打印结果
private static void printArr(List<Integer> list) {
for (Integer integer : list) {
System.out.println(integer);
}
}
//查询
private static List<Integer> queryNum(Scanner sc, int n, int q, int[] n1,
int[] n2) {
List<Integer> list  = new ArrayList<Integer>();
for (int i = 0; i < q; i++) {
int result = 0;
int queryNum1 = sc.nextInt();
int queryNum2 = sc.nextInt();
for (int j = 0; j < n; j++) {
if(n1[j]>=queryNum1 && n2[j]>=queryNum2){
result+=1;
}
}
list.add(result);
}
return list;
}
/**
* 获取数组
* @param n
*/
public static int[] getArr(Scanner sc, int n) {
int n1[] = new int[n];
for (int i = 0; i < n; i++) {
n1[i] = sc.nextInt();
}
return n1;
}

}
发表于 2017-08-22 14:05:40 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int main(){
int n, q;
while (cin >> n >> q){
vector<int> A(n + 1);
vector<int> B(n + 1);
vector<int> qx(q + 1);
vector<int> qy(q + 1);
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < n; i++)
cin >> B[i];
for (int i = 0; i < q; i++)
cin >> qx[i]>>qy[i];
for (int i = 0; i < q; i++){
int count = 0;
for (int j = 0; j < n; j++){
if (qx[i] <= A[j] && qy[i] <= B[j]){
count++;
}
}
cout << count;
if (i != q - 1)
cout << endl;
}
}
return 0;
}

发表于 2017-08-22 11:40:28 回复(0)
 package tests;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Shulie {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        int q=scanner.nextInt();
        int[] a=new int[n];
        int[] b=new int[n];
        for (int i = 0; i < n; i++) {
            a[i]=scanner.nextInt();
        }
        for (int i = 0; i < n; i++) {
            b[i]=scanner.nextInt();
        }
        List<int[]> qList=new ArrayList<int[]>();
        for (int i = 0; i < q; i++) {
                int[] ks={scanner.nextInt(),scanner.nextInt()};
                qList.add(ks);
        }
        //===========
       
        for (int j = 0; j < q; j++) {
            int flag=0;
            for (int i = 0; i < n; i++) {
                if ((a[i]>=qList.get(j)[0]) && (b[i]>=qList.get(j)[1])) {
                    flag++;
                }
            }
            System.out.println(flag);
        }
    }

}

发表于 2017-08-01 16:01:17 回复(0)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// write your code here
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int q = in.nextInt();
    int[] A = new int[n];
    int[] B = new int[n];

    for (int i=0; in.hasNext()&& i<n; i++){
        A[i] = in.nextInt();
    }
    for (int i=0; in.hasNext()&& i<n; i++){
        B[i] = in.nextInt();
    }

    ArrayList<match> list = new ArrayList<match>();
    for (int i=0; i<q; i++){
        match ma = new match(in.nextInt(),in.nextInt());
        list.add(ma);
    }
    int[] result = new int[q];
    for (int i=0; i<q; i++){
        result[i] = 0;
        for (int j=0; j<n; j++){
            if (A[j]>=list.get(i).a && B[j]>=list.get(i).b){
                result[i]++;
            }
        }

    }

    for (int sum : result){
        System.out.println(sum);
    }
}

}
class match{
match(int a, int b){
this.a = a;
this.b = b;
}
int a;
int b;
}

发表于 2017-07-31 22:47:14 回复(0)
#include<stdio.h>
int a[10000],b[100000];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
for(int i=0;i<n;i++)
{
scanf("%d",b+i);
}
int x,y;
int c=0;
for(int i=0;i<q;i++)
{
scanf("%d%d",&x,&y);
for(int i=0;i<n;i++)
{
if(a[i] >= x && b[i] >= y)
c++;
}
printf("%d\n",c);
c=0;
}
return 0;
}
请问一下我这个代码可以吗
编辑于 2017-07-28 20:01:24 回复(1)