#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long
#define arr array<int, 2>
int N = 1<<18;
int M = 1<<17;
int K = 17;
int go(int a, int b, int d, vector<vector<arr>> &bin, vector<int> &counter, vector<vector<int>> &upd)
{
if (bin[a][b][d]==-1)
{
upd[a].push_back(0);
bin[a].push_back({-1, -1});
bin[a][b][d] = counter[a];
counter[a]++;
}
return bin[a][b][d];
}
void update(int a, int b, vector<vector<arr>> &bin, vector<int> &counter, vector<vector<int>> &upd)
{
a += N;
while (a)
{
int curr = 0;
int bb = b;
for (int i = K; i >= 0; i--)
{
upd[a][curr]++;
if (bb>=(1<<i))
{
bb -= 1<<i;
curr = go(a, curr, 1, bin, counter, upd);
}
else curr = go(a, curr, 0, bin, counter, upd);
}
upd[a][curr]++;
a/=2;
}
}
ll ac2d(int a, int curr, int l, int r, int x, int y, vector<vector<arr>> &bin, vector<vector<int>> &upd)
{
if (r <=x || l >= y) return 0;
if (l >= x && r <= y)
{
return upd[a][curr];
}
int m = (l+r)/2;
ll sol = 0;
if (bin[a][curr][0]!=-1) sol += ac2d(a, bin[a][curr][0], l, m, x, y, bin, upd);
if (bin[a][curr][1]!=-1) sol += ac2d(a, bin[a][curr][1], m, r, x, y, bin, upd);
return sol;
}
ll acc(int curr, int l, int r, int a, int b, int x, int y, vector<vector<arr>> &bin, vector<vector<int>> &upd)
{
if (r <=a || l >= b) return 0;
if (l >= a && r <= b)
{
return ac2d(curr, 0, 0, N, x, y, bin, upd);
}
int m = (l+r)/2;
return acc(curr*2, l, m, a, b, x, y, bin, upd)+ acc(curr*2+1, m, r, a, b, x, y, bin, upd);
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int b, n, d, m;
cin >> b >> n >> d >> m;
if (b == 1)
{
vector<int> nums(n);
for (int i =0; i < n; i++) cin >> nums[i];
sort(nums.begin(), nums.end());
ll sol = 0;
queue<int> q;
for (int i = 0; i < n; i++)
{
while (q.size() && nums[i]-q.front()> d) q.pop();
sol += q.size();
q.push(nums[i]);
}
cout << sol;
}
else if (b==2)
{
vector<vector<arr>> bin;
vector<int> counter;
vector<vector<int>> upd;
bin.assign(2*N, vector<arr>(1, {-1, -1}));
counter.assign(2*N, 1);
upd.assign(2*N, vector<int>(1, 0));
ll sol = 0;
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
sol += acc(1, 0, N, x+y-d, x+y+d+1, y-x+M-d, y-x+M+d+1, bin, upd);
update(x+y, y-x+M, bin, counter, upd);
}
cout << sol;
}
else if (b==3)
{
N = 1<<8;
M = 1<<7;
K = 7;
vector<vector<vector<arr>>> bin(76, vector<vector<arr>>(2*N, vector<arr>(1, {-1, -1})));
vector<vector<int>> counter(76, vector<int>(2*N, 1));
vector<vector<vector<int>>> upd(76, vector<vector<int>>(2*N, vector<int>(1, 0)));
ll sol = 0;
for (int i =0; i < n; i++)
{
int x, y, z;
cin >> x >> y >> z;
for (int j = 0; j <= z; j++)
{
int h = d-(z-j);
if (h <0) continue;
sol += acc(1, 0, N, x+y-h, x+y+h+1, y-x+M-h, y-x+M+h+1, bin[j],upd[j]);
}
for (int j = z+1; j < 76; j++)
{
int h = d-(j-z);
if (h <0) break;
sol += acc(1, 0, N, x+y-h, x+y+h+1, y-x+M-h, y-x+M+h+1, bin[j],upd[j]);
}
update(x+y, y-x+M, bin[z], counter[z], upd[z]);
}
cout << sol;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
11 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
604 KB |
Output is correct |
2 |
Correct |
14 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
860 KB |
Output is correct |
3 |
Correct |
14 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
65108 KB |
Output is correct |
2 |
Correct |
63 ms |
65104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
61568 KB |
Output is correct |
2 |
Correct |
266 ms |
61472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
719 ms |
97736 KB |
Output is correct |
2 |
Correct |
660 ms |
97620 KB |
Output is correct |
3 |
Correct |
523 ms |
80616 KB |
Output is correct |
4 |
Correct |
588 ms |
90964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2407 ms |
337788 KB |
Output is correct |
2 |
Correct |
2212 ms |
337832 KB |
Output is correct |
3 |
Correct |
708 ms |
102160 KB |
Output is correct |
4 |
Correct |
699 ms |
92732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5976 KB |
Output is correct |
2 |
Correct |
13 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
5456 KB |
Output is correct |
2 |
Correct |
496 ms |
5712 KB |
Output is correct |
3 |
Correct |
325 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1504 ms |
24336 KB |
Output is correct |
2 |
Execution timed out |
4019 ms |
21680 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4051 ms |
30520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |