Submission #529298

# Submission time Handle Problem Language Result Execution time Memory
529298 2022-02-22T17:00:45 Z Evang Sails (IOI07_sails) C++17
100 / 100
129 ms 2536 KB
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;

#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)

#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb(x) push_back(x)
#define eb(args...) emplace_back(args)
#define ff first
#define ss second
#define die exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


const int N = 1e5+5;
const int H = 1e5+5;
int n, t, bit[H];
pi a[N];  // (H, K)
ll sum = 0;

void upd(int i, int x){
    while(i<=t){
        bit[i] += x;
        i += i&-i;
    }
}

int at(int i){
    int ans = 0;
    while(i>0){
        ans += bit[i];
        i -= i&-i;
    }
    return ans;
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> a[i].ff >> a[i].ss;
        t = max(t, a[i].ff);
    }
    sort(a,a+n);

    for(int i = 0; i < n; ++i){
        int j = t - a[i].ff + 1;
        int k = j + a[i].ss - 1;

        // 0 0 0 2 2 2 2 5 5 5 5 5 5 7
        // j             l k       r t

        int l = k, r = k;
        for(int u = t; u > 0; u /= 2){
            while(l - u >= j && at(l-u) == at(k))
                l -= u;
            while(r+u<=t && at(r+u)==at(k))
                r += u;
        }

        upd(j, 1);
        upd(l, -1);
        upd(r-(k-l+1)+1, 1);
        upd(r+1, -1);
    }

    ll ans = 0;
    for(int i = 1; i <= t; ++i){
        ll x = at(i);
        ans += x*(x-1)/2;
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 536 KB Output is correct
2 Correct 26 ms 864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 1444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 2152 KB Output is correct
2 Correct 87 ms 2096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 2396 KB Output is correct
2 Correct 76 ms 2044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 2536 KB Output is correct
2 Correct 73 ms 2228 KB Output is correct