# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
708661 |
2023-03-12T06:55:46 Z |
cig32 |
Pairs (IOI07_pairs) |
C++17 |
|
4000 ms |
163560 KB |
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
#define int long long
using namespace __gnu_pbds;
typedef tree<pair<pair<int,int>,int>,null_type,less<pair<pair<int,int>,int> >,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
ordered_set bit[75010]; // 2D bit in regular matrices but only supporting single add/del
int id = 0;
map<pair<int, int>, queue<int> > mp;
void insert(int x, int y) {
id++;
mp[{y, x}].push(id);
for(;x<75010;x+=x&-x) bit[x].insert({{y, x}, id});
}
void erase(int x, int y) {
int ono = mp[{y, x}].front(); mp[{y, x}].pop();
for(;x<75010;x+=x&-x) bit[x].erase({{y, x}, ono});
}
int query(int x, int y) {
int sum = 0;
for(;x;x-=x&-x) sum += bit[x].order_of_key({{y+1, 0}, 0});
return sum;
}
bool cmp1(pair<int, int> x, pair<int, int> y) {
return x.first - x.second < y.first - y.second;
}
bool cmp2(pair<int, int> x, pair<int, int> y) {
return x.first + x.second < y.first + y.second;
}
void solve(int tc) {
int b, n, d, m;
cin >> b >> n >> d >> m;
if(b == 1) {
int a[n+1];
for(int i=1; i<=n; i++) cin >> a[i];
sort(a+1, a+1+n);
int ans = 0;
for(int i=1; i<n; i++) {
int lb = i+1, rb = n;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(a[mid] - a[i] <= d) lb = mid;
else rb = mid - 1;
}
if(a[lb] - a[i] <= d) ans += lb - i;
}
cout << ans << "\n";
return;
}
if(b == 2) {
int ans = 0;
pair<int, int> p[n+1];
for(int i=1; i<=n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p+1, p+1+n, cmp1);
int j = 1;
for(int i=1; i<=n; i++) {
while(j < i && (p[i].first - p[i].second) - (p[j].first - p[j].second) > d) {
erase(p[j].first, 75001-p[j].second);
j++;
}
ans += query(p[i].first, 75001-p[i].second);
insert(p[i].first, 75001-p[i].second);
}
for(int i=j; i<=n; i++) erase(p[i].first, 75001-p[i].second);
j = 1;
sort(p+1, p+1+n, cmp2);
for(int i=1; i<=n; i++) {
while(j < i && (p[i].first + p[i].second) - (p[j].first + p[j].second) > d) {
erase(p[j].first, p[j].second);
j++;
}
ans += query(p[i].first, p[i].second);
insert(p[i].first, p[i].second);
}
vector<int> c[75001];
for(int i=1; i<=n; i++) c[p[i].second].push_back(p[i].first);
for(int i=1; i<=75000; i++) sort(c[i].begin(), c[i].end());
for(int i=1; i<=75000; i++) {
for(int j=0; j+1<c[i].size(); j++) {
int lb = j+1, rb = c[i].size() - 1;
while(lb < rb) {
int mid = (lb + rb + 1) >> 1;
if(c[i][mid] - c[i][j] <= d) lb = mid;
else rb = mid-1;
}
if(c[i][lb] - c[i][j] <= d) ans -= (lb - j);
}
}
cout << ans << "\n"; return;
}
if(b == 3) {
int x[n+1], y[n+1], z[n+1];
for(int i=1; i<=n; i++) cin >> x[i] >> y[i] >> z[i];
int ans = 0;
for(int i=1; i<=n; i++) for(int j=i+1; j<=n; j++) ans += (abs(x[i] - x[j]) + abs(y[i] - y[j]) + abs(z[i] - z[j]) <= d);
cout << ans << "\n"; return;
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
Compilation message
pairs.cpp: In function 'void solve(long long int)':
pairs.cpp:103:23: 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]
103 | for(int j=0; j+1<c[i].size(); j++) {
| ~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10196 KB |
Output is correct |
2 |
Correct |
6 ms |
10196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
10208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
11112 KB |
Output is correct |
2 |
Correct |
20 ms |
11084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
10964 KB |
Output is correct |
2 |
Correct |
24 ms |
11084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11088 KB |
Output is correct |
2 |
Correct |
27 ms |
11084 KB |
Output is correct |
3 |
Correct |
26 ms |
11092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
11732 KB |
Output is correct |
2 |
Incorrect |
14 ms |
11876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1632 ms |
30912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2998 ms |
163560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2624 ms |
161688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10324 KB |
Output is correct |
2 |
Correct |
10 ms |
10328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4043 ms |
12556 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4049 ms |
12628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4064 ms |
12628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |