425D - Sereja and Squares - CodeForces Solution


binary search data structures hashing *2300

Please click on ads to support us..

C++ Code:

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
  
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>
  
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk
#define plus fsghsf
#define minus ytryr
  
using namespace std;
  
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }
  
const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = 0;
const ld eps = 1e-12;
const double pi = atan2(0, -1);
const int ABC = 26;

const int B = 333;

int c_xs[N + 1], c_ys[N + 1];
vector<int> ys[N + 1];
unordered_set<int> xs[N + 1];

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    //ll TL = 10.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n;
    cin >> n;
    rep(i, 0, n) {
        int x, y;
        cin >> x >> y;
        c_ys[x]++; ys[x].push_back(y);
        c_xs[y]++; xs[y].insert(x);
    }
    int ans = 0;
    vector<int> big_x;
    rep(x, 0, N + 1) {
        sort(ys[x].begin(), ys[x].end());
        if (c_ys[x] <= B) {
            for (int i = 0; i < c_ys[x]; i++) {
                for (int j = i + 1; j < c_ys[x]; j++) {
                    int y1 = ys[x][i], y2 = ys[x][j];
                    int d = y2 - y1;
                    if (x - d >= 0 && c_ys[x - d] > B && xs[y1].find(x - d) != xs[y1].end() && xs[y2].find(x - d) != xs[y2].end()) {
                        ans++;
                    }
                    if (xs[y1].find(x + d) != xs[y1].end() && xs[y2].find(x + d) != xs[y2].end()) {
                        ans++;
                    }
                }
            }
        } else {
            big_x.push_back(x);
        }
    }
    rep(i, 0, sz(big_x)) {
        rep(j, i + 1, sz(big_x)) {
            int x1 = big_x[i], x2 = big_x[j];
            int d = x2 - x1;
            int l1 = 0, r1 = 0, l2 = 0, r2 = 0;
            while (l1 < c_ys[x1] && l2 < c_ys[x2]) {
                if (ys[x1][l1] == ys[x2][l2]) {
                    while (r1 < c_ys[x1] && ys[x1][r1] < ys[x1][l1] + d) {
                        r1++;
                    }
                    while (r2 < c_ys[x2] && ys[x2][r2] < ys[x2][l2] + d) {
                        r2++;
                    }
                    if (r1 < c_ys[x1] && ys[x1][r1] == ys[x1][l1] + d && r2 < c_ys[x2] && ys[x2][r2] == ys[x2][l2] + d) {
                        ans++;
                    }
                }
                if (ys[x1][l1] < ys[x2][l2]) {
                    l1++;
                } else {
                    l2++;
                }
            }
        }
    }
    cout << ans << "\n";
    return 0;
}


Comments

Submit
0 Comments
More Questions

1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number