Submission #964642

# Submission time Handle Problem Language Result Execution time Memory
964642 2024-04-17T09:02:25 Z Esgeer Pairs (IOI07_pairs) C++17
60 / 100
97 ms 20560 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <unistd.h>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < (int) n; i++)
#define pb push_back
#define fi first
#define se second

int mod = 1e9+7;
int inf = 1e6;
const int N = 3e5;

int t[N];

template<typename T> struct bit2d {
    vector<T> bit;
    vi xs;
    vi st;
    vi cnt;
    int n, alllen;
    void init(int N, vpi Q) {
        n = N;
        cnt.assign(N+1, 0);
        st.resize(N+1);
        sort(Q.begin(), Q.end(), [](pii &a, pii &b) { return a.se > b.se; });
        F(i, 0, Q.size()) {
            for(int j = Q[i].fi; j > 0 && j < N; j += (j & -j)) cnt[j]++;
        }
        st[0] = cnt[0]+1;
        F(i, 1, N) {
            st[i] = st[i-1] + cnt[i];
        }
        alllen = st[N-1];
        xs.assign(alllen+3, 0);
        bit.assign(alllen+3, 0);
        F(i, 0, Q.size()) {
            for(int j = Q[i].fi; j > 0 && j < N; j += (j & -j)) xs[--st[j]] = Q[i].se;
        }
    }

    int find(int y, int el) {
        return upper_bound(xs.begin() + st[y], xs.begin() + st[y] + cnt[y], el) - xs.begin() - st[y];
    }

    void update(int y, int x, T el) {
        while(y > 0 && y < n) {
            //cout << y sp x sp find(y, x) sp cnt[y] << endl;
            for(int i = find(y, x); i > 0 && i <= cnt[y]; i += (i & -i)) {
                //cout << y sp i << endl;
                bit[i + st[y]] += el;
            }
            y += (y & -y);
        }
    }

    T query(int y, int x) {
        T res = T();
        while(y > 0 && y < n) {
            //cout << y sp x sp find(y, x) sp cnt[y] << endl;
            for(int i = find(y, x); i > 0 && i <= cnt[y]; i -= (i & -i)) {
                //cout << y sp i << endl;
                res += bit[i + st[y]];
            }
            y -= (y & -y);
        }
        return res;
    }
};

template<class T, int SZ> struct OffBIT2D { 
	bool mode = 0; // mode = 1 -> initialized
	vpi todo; // locations of updates to process
	int cnt[SZ], st[SZ];
	vi val; vector<T> bit; // store all BITs in single vector
	void init() { assert(!mode); mode = 1;
		int lst[SZ]; F(i,0,SZ) lst[i] = cnt[i] = 0;
		sort(todo.begin(), todo.end(),[](const pii& a, const pii& b) { 
			return a.se < b.se; });
		for(auto t : todo) for (int x = t.fi; x < SZ; x += x&-x) 
			if (lst[x] != t.se) lst[x] = t.se, cnt[x] ++;
		int sum = 0; F(i,0,SZ) lst[i] = 0, st[i] = (sum += cnt[i]);
		val.resize(sum); bit.resize(sum); reverse(todo.begin(), todo.end());
		for(auto t : todo) for (int x = t.fi; x < SZ; x += x&-x) 
			if (lst[x] != t.se) lst[x] = t.se, val[--st[x]] = t.se;
	}
	int rank(int y, int l, int r) {
		return upper_bound(val.begin()+l,val.begin()+r,y)-val.begin()-l; }
	void UPD(int x, int y, T t) {
		for (y = rank(y,st[x],st[x]+cnt[x]); y <= cnt[x]; y += y&-y) 
			bit[st[x]+y-1] += t; }
	void upd(int x, int y, T t) { 
		if (!mode) todo.pb({x,y});
		else for (;x<SZ;x+=x&-x) UPD(x,y,t); }
	int QUERY(int x, int y) { T res = 0;
		for (y = rank(y,st[x],st[x]+cnt[x]); y; y -= y&-y) res += bit[st[x]+y-1];
		return res; }
	T query(int x, int y) { assert(mode);
		T res = 0; for (;x;x-=x&-x) res += QUERY(x,y);
		return res; }
	T query(int xl, int xr, int yl, int yr) { 
		return query(xr,yr)-query(xl-1,yr)
			-query(xr,yl-1)+query(xl-1,yl-1); }
};

void solve() {
    int d, n, k, m;
    cin >> d >> n >> k >> m;
    if(d == 1) {
        vi a(n);
        F(i, 0, n) cin >> a[i];
        sort(a.begin(), a.end());
        queue<int> last;
        int ans = 0;
        F(i, 0, n) {
            while(last.size() && last.front() < a[i] - k) last.pop();
            ans += last.size();
            last.push(a[i]);
        }
        cout << ans << endl;
    } else if(d == 2) {
        vpi a(n);
        F(i, 0, n) cin >> a[i].fi >> a[i].se;
        F(i, 0, n) {
            int y = a[i].fi, x = a[i].se;
            a[i].fi = x - y;
            a[i].se = x + y;
        }
        sort(a.begin(), a.end());
        Tree<pii> st;
        int ans = 0;
        queue<int> q;
        F(i, 0, n) {
            while(q.size() && a[q.front()].fi < a[i].fi - k) {
                st.erase({a[q.front()].se, q.front()});
                q.pop();
            }
            ans += st.order_of_key({a[i].se + k, inf}) - st.order_of_key({a[i].se - k, -1});
            st.insert({a[i].se, i});
            q.push(i);
        }
        cout << ans << endl;
    } else if(d == 3) {
        vector<pair<int, pii>> a(n);
        F(i, 0, n) {
            cin >> a[i].se.fi >> a[i].se.se >> a[i].fi;
            int y = a[i].se.fi, x = a[i].se.se;
            a[i].se.fi = x + y;
            a[i].se.se = x - y + m;
        }
        sort(a.begin(), a.end());
        int MAX_IDK = 2*m+5;
        vpi Q[MAX_IDK];
        F(i, 0, n) Q[a[i].fi].pb({a[i].se.fi, a[i].se.se});
        OffBIT2D<int, 155> bit[MAX_IDK];
        F(i, 0, MAX_IDK) bit[i].todo = Q[i], bit[i].init();
        int ans = 0;
        queue<int> q;
        F(i, 0, n) {
            //while(q.size() && a[q.front()].fi < a[i].fi - )
            F(z, 0, MAX_IDK) {
                int nk = k - abs(z - a[i].fi);
                if(nk < 0) continue;
                ans += bit[z].query(a[i].se.fi + nk, a[i].se.se + nk) - bit[z].query(a[i].se.fi + nk, a[i].se.se - nk - 1) - bit[z].query(a[i].se.fi - nk - 1, a[i].se.se + nk) + bit[z].query(a[i].se.fi - nk - 1, a[i].se.se - nk - 1);
            }
            q.push(i);
            bit[a[i].fi].upd(a[i].se.fi, a[i].se.se, 1);
        }
        cout << ans << endl;
    }
}

void setIO() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    #ifdef Local
        freopen("local.in", "r", stdin);
        freopen("local.out", "w", stdout);
    #else 
        // freopen("friendcross.in","r",stdin);
        // freopen("friendcross.out","w",stdout);
    #endif
}

signed main() {
    setIO();
    int t = 1;
    //cin >> t;
    while(t--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1256 KB Output is correct
2 Correct 10 ms 2136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 1112 KB Output is correct
2 Correct 16 ms 2028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 1112 KB Output is correct
2 Correct 15 ms 1372 KB Output is correct
3 Correct 15 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 2332 KB Output is correct
2 Correct 48 ms 8724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 2472 KB Output is correct
2 Correct 97 ms 5952 KB Output is correct
3 Correct 84 ms 4592 KB Output is correct
4 Correct 79 ms 4868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 2628 KB Output is correct
2 Correct 97 ms 5620 KB Output is correct
3 Correct 68 ms 5368 KB Output is correct
4 Correct 66 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 1628 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 68 ms 6676 KB Output is correct
2 Runtime error 31 ms 12072 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 46 ms 18512 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 44 ms 20560 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -