【洛谷】AT_abc371_d [ABC371D] 1D Country 的题解

【洛谷】AT_abc371_d [ABC371D] 1D Country 的题解

洛谷传送门

AT传送门

题解

锐评:比C题还简单

题目大意:给定你 n n n 个地址,在第 x [ i ] x[i] x[i] 位置有 p [ i ] p[i] p[i] 个人,给定 q q q 个询问,每次查询位置 l l l r r r之间有多少个人。

首先,惊人的数据范围让我直接放弃了暴力。考虑线性算法,刚开始和大多数人一样考虑到了前缀和。但是后面看了题解想到了二分。唯一的问题就是负数的问题,这可以将 l l l, r r r,进行一个映射,让它成为一个在 1 1 1 n n n 之间的数。但是我很懒,直接用 upper_boundlower_bound 水过去了qaq。

Caution:不开 long long 见祖宗!!!

代码

#include <bits/stdc++.h>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int n, q, x[200005], p[200005], ans[200005];
int main() {
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n = read();
	for(int i = 1; i <= n; i ++) {
		x[i] = read();
	}
	for(int i = 1; i <= n; i ++) {
		p[i] = read();
		ans[i] = ans[i - 1] + p[i];
	}
	q = read();
	while(q --) {
		int l, r;
		l = read(), r = read();
		l = lower_bound(x + 1, x + n + 1, l) - x;
		r = upper_bound(x + 1, x + n + 1, r) - x;
		write(ans[r - 1] - ans[l - 1]), putchar('\n');
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值