Submission #708651

#TimeUsernameProblemLanguageResultExecution timeMemory
708651cig32Pairs (IOI07_pairs)C++17
40 / 100
4061 ms180592 KiB
#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 - y.first < x.second - y.second; } bool cmp2(pair<int, int> x, pair<int, int> y) { return x.first + y.first < x.second + 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); for(int i=1; i<=n; i++) { ans += query(p[i].first, 75001-p[i].second); insert(p[i].first, 75001-p[i].second); } for(int i=1; i<=n; i++) erase(p[i].first, 75001-p[i].second); sort(p+1, p+1+n, cmp2); for(int i=1; i<=n; i++) { 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 (stderr)

pairs.cpp: In function 'void solve(long long int)':
pairs.cpp:91: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]
   91 |       for(int j=0; j+1<c[i].size(); j++) {
      |                    ~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...