Submission #713922

# Submission time Handle Problem Language Result Execution time Memory
713922 2023-03-23T08:57:21 Z ssense Pairs (IOI07_pairs) C++14
30 / 100
4000 ms 4360 KB
#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long  ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//mesanu

void solve1()
{
    int n, d, m;
    cin >> n >> d >> m;
    vint a = read(n);
    sort(all(a));
    int last = 0;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        while(a[i]-a[last] > d)
        {
            last++;
        }
        ans+=(i-last);
    }
    cout << ans << endl;
    return;
}

int bit2d[150001];

void add2d(int pos, int add){
    for(int i = pos; i <= 150000; i = i|(i+1))
    {
        bit2d[i]+=add;
    }
}

int get2d(int l){
    int ans = 0;for(int i = l; i >= 0; i = (i&(i+1))-1){ans+=bit2d[i];}return ans;
}

int get2d(int l, int r){
    if(l != 0){return get2d(r)-get2d(l-1);}
    else{return get2d(r);}
}

bool cmp2d(pair<int, int> a, pair<int, int> b){
    return (a.fr-a.sc)<(b.fr-b.sc);
}

void solve2()
{
    int n, d, m;
    cin >> n >> d >> m;
    vector<pair<int, int>> a(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i].fr >> a[i].sc;
    }
    sort(all(a), cmp2d);
    int last = 0;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        last = 0;
        add2d(a[i].fr+a[i].sc, 1);
        while(a[i].fr-a[i].sc-a[last].fr+a[last].sc > d)
        {
            add2d(a[i].fr+a[i].sc, -1);
            last++;
        }
        ans+=get2d(max(0LL, a[i].fr+a[i].sc-d), a[i].fr+a[i].sc+d)-1;
    }
    cout << ans << endl;
}

void solve()
{
    int b;
    cin >> b;
    if(b == 1)
    {
        solve1();
    }
    else if(b == 2)
    {
        solve2();
    }
}

int32_t main(){
    startt
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}

/*
1 6 5 100
25
50
50
10
20
23
 
2 5 4 10
5 2
7 2
8 4
6 5
4 4
*/

Compilation message

pairs.cpp: In member function 'void prefix_sum::build(std::vector<long long int>)':
pairs.cpp:20:667: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//mesanu
      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 1876 KB Output is correct
2 Correct 12 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2192 KB Output is correct
2 Correct 17 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2132 KB Output is correct
2 Correct 19 ms 2196 KB Output is correct
3 Correct 17 ms 2200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 1364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4025 ms 2408 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4019 ms 2644 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4035 ms 4360 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -