#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];
struct bit2d {
vi bit;
int n, m;
void init(int n, int m) {
this->n = n, this->m = m;
bit.assign(n*m, 0);
}
void update(int y, int x, int val) {
y = min(y, n), x = min(x, m);
for(int i = y; i > 0 && i <= n; i += (i & -i)) {
for(int j = x; j > 0 && j <= m; j += (j & -j)) {
bit[(i-1)*m + j-1] += val;
}
}
}
int query(int y, int x) {
int res = 0;
y = min(y, n), x = min(x, m);
for(int i = y; i > 0 && i <= n; i -= (i & -i)) {
for(int j = x; j > 0 && j <= m; j -= (j & -j)) {
res += bit[(i-1)*m + j-1];
}
}
return res;
}
};
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 = 4*m+5;
bit2d bit[MAX_IDK];
F(i, 0, MAX_IDK) bit[i].init(MAX_IDK, MAX_IDK);
int ans = 0;
F(i, 0, n) {
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);
}
bit[a[i].fi].update(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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1628 KB |
Output is correct |
2 |
Correct |
11 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
2012 KB |
Output is correct |
2 |
Correct |
15 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2124 KB |
Output is correct |
2 |
Correct |
15 ms |
2140 KB |
Output is correct |
3 |
Correct |
19 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
2948 KB |
Output is correct |
2 |
Correct |
49 ms |
9204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
3440 KB |
Output is correct |
2 |
Correct |
93 ms |
6624 KB |
Output is correct |
3 |
Correct |
80 ms |
5388 KB |
Output is correct |
4 |
Correct |
85 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
3672 KB |
Output is correct |
2 |
Correct |
112 ms |
7008 KB |
Output is correct |
3 |
Correct |
80 ms |
6484 KB |
Output is correct |
4 |
Correct |
70 ms |
6224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
222868 KB |
Output is correct |
2 |
Correct |
142 ms |
222972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
3952 KB |
Output is correct |
2 |
Correct |
73 ms |
4140 KB |
Output is correct |
3 |
Correct |
93 ms |
3944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
119508 KB |
Output is correct |
2 |
Correct |
1044 ms |
119504 KB |
Output is correct |
3 |
Correct |
1845 ms |
119512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
909 ms |
226136 KB |
Output is correct |
2 |
Correct |
2061 ms |
226132 KB |
Output is correct |
3 |
Correct |
2893 ms |
226132 KB |
Output is correct |