202912026-01-05 20:17:58ubormaciHáromszögekcpp17Elfogadva 40/4093ms1508 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

void solve() {

	ll n, q;
	cin >> n >> q;

	vector<ll> v(n, 0);
	for(ll i = 0; i < n; i++) {
		cin >> v[i];
	}

	sort(v.begin(), v.end());

	for(ll qi = 0; qi < q; qi++) {

		// harom eset van:
		// a <= b <= c
		// a <= c <= b
		// c <= a <= b

		ll a, b;
		cin >> a >> b;

		if(a > b) {
			swap(a, b);
		}

		//cerr << "\n\na=" << a << "; b=" << b;
		
		ll p1 = 0;

		// a <= b < c	
		{
			ll l = upper_bound(v.begin(), v.end(), b) - v.begin();
			ll r = lower_bound(v.begin(), v.end(), a + b) - v.begin() - 1;
			
			if(r >= l) {
				p1 = r - l + 1;
			}
		}

		// c a kozepso oldal
		// vagyis a <= c <= b, (a + c > b == c > b - a)

		ll p2 = 0;
		{

			//cerr << "\nkozepso";

			ll l1 = upper_bound(v.begin(), v.end(), b - a) - v.begin();
			ll l2 = lower_bound(v.begin(), v.end(), a) - v.begin();

			ll r = upper_bound(v.begin(), v.end(), b) - v.begin() - 1;

			//cerr << "\nl1=" << l1 << "; l2=" << l2 << "; r=" << r;

			if(r >= max(l1, l2)) {
				p2 = r - max(l1, l2) + 1;
			}	
		
		}
		// c a legkisebb oldal
		// c < a <= b, de (c + a > b <-> c > b - a)
		
		//cerr << "\n";

		ll p3 = 0;
		{
			ll r = lower_bound(v.begin(), v.end(), a) - v.begin() - 1;
			ll l = upper_bound(v.begin(), v.end(), b - a) - v.begin();

			//cerr << "\nlegkisebb, l=" << l << "; r=" << r;

			if(l <= r) {
				p3 = r - l + 1;
			}
		}

		//cerr << "\np1=" << p1 << "; p2=" << p2 << "; p3=" << p3;

		cout << p1 + p2 + p3 << "\n";
	}	

}

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	solve();

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/01ms508 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva0/08ms316 KiB
4Elfogadva4/42ms316 KiB
5Elfogadva2/22ms316 KiB
6Elfogadva1/179ms1480 KiB
7Elfogadva1/150ms1476 KiB
8Elfogadva2/250ms1476 KiB
9Elfogadva2/250ms1404 KiB
10Elfogadva1/185ms1304 KiB
11Elfogadva1/175ms1476 KiB
12Elfogadva2/276ms1472 KiB
13Elfogadva2/282ms1496 KiB
14Elfogadva1/193ms1508 KiB
15Elfogadva1/174ms1332 KiB
16Elfogadva1/171ms1332 KiB
17Elfogadva1/157ms1220 KiB
18Elfogadva1/150ms1268 KiB
19Elfogadva2/250ms1260 KiB
20Elfogadva2/250ms1136 KiB
21Elfogadva3/352ms1224 KiB
22Elfogadva5/586ms1352 KiB
23Elfogadva5/593ms1480 KiB