社区讨论

关于结构体关键字(二分)

灌水区参与者 5已保存回复 17

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
17 条
当前快照
1 份
快照标识符
@m2cr84lm
此快照首次捕获于
2024/10/17 11:41
去年
此快照最后确认于
2025/11/04 23:53
4 个月前
查看原帖
想问问若用结构体中某一关键字进行二分,具体的重载函数该应该怎么写,或者说有更简单的方法,求解答
CPP
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
template<typename T> void re(T&x){x = 0; int sign = 1; char c;do{c = getchar(); if (c == '-') sign = -1;}while(!isdigit(c)); do{x = x * 10 + c - '0'; c = getchar();}while(isdigit(c)); x *= sign;}
void write(int x){if (x < 0) x = -x, putchar('-'); if (x < 10) putchar(x + '0'); else write(x / 10), putchar(x % 10 + '0');}

const int N = 4e5 + 1000;

struct node{
	int x, val;
	bool operator < (const node &p) const {
		return p.x < x;
	}
};

vector<node> alls;

int n, sum[N], a[N], b[N];

bool cmp(node a, node b){
	return a.x < b.x;
}

int main(){
	re(n);
	for (int i = 1; i <= n; i ++) re(a[i]);
	for (int i = 1; i <= n; i ++) re(b[i]), sum[i] = sum[i - 1] + b[i];
	for (int i = 1; i <= n; i ++) alls.push_back((node){a[i], sum[i]});
	sort(alls.begin(), alls.end(), cmp);
	int q, x, y;
	re(q);
	while (q --){
		re(x), re(y);
		node *it1 = lower_bound(alls.begin(), alls.end(), x);
		node *it2 = upper_bound(alls.begin(), alls.end(), y);
	}
	return 0;
}
以上是错误代码,还是求问结构体重载写法

回复

17 条回复,欢迎继续交流。

正在加载回复...