c++17
/c++20
以上代码
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve(i64 n,i64 x, i64 y, vector<int>a){
i64 sum = reduce(a.begin(),a.end());
i64 l = sum -y,r =sum-x;
ranges::sort(a);
int x1 = n-1, x2=n-1;
i64 res = 0;
for (int i = 0;i<n;i++){
i64 t1 = l-a[i],t2=r-a[i];
while (x2>i && a[x2]>t2)x2--;
if (x2<=i) continue;
x1=min(x1,x2);
x1=max(x1,i+1);
while(x1>i && a[x1]>=t1)x1--;
if (x1+1 < n && a[x1+1]>=t1 && x2-x1 >0 && x1>=i &&x2>i){
res +=x2 - x1;
}
}
cout << res << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
i64 n, x, y;
cin >> n >> x>> y;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
solve(n,x,y,a);
}
return 0;
}
rust代码
use crate::algo_lib::collections::iter_ext::iters::Iters;
use crate::algo_lib::collections::min_max::MinimMaxim;
use crate::algo_lib::collections::vec_ext::default::default_vec;
use crate::algo_lib::collections::vec_ext::gen::VecGen;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::misc::extensions::do_with::DoWith;
use crate::algo_lib::misc::test_type::TaskType;
use crate::algo_lib::misc::test_type::TestType;
// https://codeforces.com/submissions/Egor 感谢这位大佬的代码
use std::fs::File;
use std::io::BufReader;
//use std::collections::HashSet;
use std::cmp;
type PreCalc = ();
//这个模板里会把N读成第一个.然后每个样例读。
fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &mut PreCalc) {
let n = input.read_size();
let x: usize = input.read_size();
let y = input.read_size() as i64;
let mut a = input.read_long_vec(n);
//let mut ans = vec![0;n];
let mut res = 0;
//let mut books = HashSet::new();
//let mut unbooks = HashSet::new();
let sum: i64 = a.iter().sum();
let l = sum - y;
let r = sum - x as i64;
a.sort();
let mut x1 = n - 1;
let mut x2 = n - 1;
let mut res = 0;
for i in 0..n {
let t1 = l - a[i as usize];
let t2 = r - a[i as usize];
while x2 > i && a[x2 as usize] > t2 {
x2 -= 1;
}
if x2 <= i { continue; }
x1 = cmp::min(x1, x2);
x1 = cmp::max(x1, i + 1);
while x1 > i && a[x1 as usize] >= t1 {
x1 -= 1;
}
if x1 + 1 < n && a[(x1 + 1) as usize] >= t1 && x2 - x1 > 0 && x1 >= i && x2 > i {
res += x2 - x1;
}
}
//println!("{}", res);
out.print_line(res);
}
fn main() {
//Local-testing 本地测试可以读src文件夹下input.txt作为输入数据
//let file = File::open("src/input.txt").expect("Failed to open input file");
//let mut sin = BufReader::new(file);
let mut sin = std::io::stdin(); //线上编译器会传入在stdin里
let input = crate::algo_lib::io::input::Input::new(&mut sin);
let mut stdout = std::io::stdout();
let output = crate::algo_lib::io::output::Output::new(&mut stdout);
run(input, output);
}
还有其他模板:
https://www.acwing.com/solution/content/262489/ 把模板拼到rust算法后面!
https://www.acwing.com/solution/content/262489/