UVa1152 - 4 Values whose Sum is 0 (hash, two-point writing)

Drawing on the blogs of the two big guys :
https://blog.csdn.net/a197p/article/details/46575743
https://blog.csdn.net/qq_21057881/article/details/50531784
hash structure:

struct Hash_map
{
    static const int mask=0x7fffff;
    int p[8388608],q[8388608];
    void clear_()
    {
        for(int i=0; i<=mask; ++i)
            q[i]=0;
    }
    int& operator [](int k)
    {
        int i;
        for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask);
            p[i]=k;
        return q[i];
    }
}hash_;

This is a hash structure overloading [] in the structure body, you can directly use hash[10000000000]++; to mark it successfully.
When querying, you can directly int x = hash[10000000000] ; it is very convenient, haha, that is to say, this hash directly realizes the marking of particularly large numbers.

Hash code:

#include<stdio.h>
#include<iostream>
#include<map>
#include<vector>
#include<stdlib.h>
#include <algorithm>

using namespace std;
struct Hash_map
{
    static const int mask=0x7fffff;
    int p[8388608],q[8388608];
    void clear_()
    {
        for(int i=0; i<=mask; ++i)
            q[i]=0;
    }
    int& operator [](int k)
    {
        int i;
        for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask);
            p[i]=k;
        return q[i];
    }
}hash_;
int main()
{
    int n,kase = 0;
    cin>>n;
    while(n--)
    {
        if(kase) printf("\n");
        vector<int> v1,v2,v3,v4,v5,v6;
        int m,ji = 0;
        cin>>m;
        while(m--)
        {
            int temp;
            cin>>temp;
            v1.push_back(temp);
            cin>>temp;
            v2.push_back(temp);
            cin>>temp;
            v3.push_back(temp);
            cin>>temp;
            v4.push_back(temp);
        }
        int len1 = v1.size(),len2 = v2.size(),len3 = v3.size(),len4 = v4.size();
        hash_.clear_();
        for(int i = 0;i < len1;i++)
            for(int j = 0;j < len2;j++)
                hash_[v1[i] + v2[j]]++;
        for(int i = 0;i < len3;i++)
            for(int j = 0;j < len4;j++)
                ji += hash_[-v3[i] -v4[j]];       printf("%d\n",ji);
        kase = 1;
    }
    return 0;
}

Dichotomous code:

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 10000
#define LL long long
int cas=1,T;
int a[maxn],b[maxn],c[maxn],d[maxn];
int e[4005*4005];
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        int n;
        scanf("%d",&n);
        for (int i = 0;i<n;i++)
        {
            scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
        }
        int k = 0;
        for (int i = 0;i<n;i++)
            for (int j = 0;j<n;j++)
            {
              e[k] = a[i]+b[j];
              k++;
            }
        sort(e,e+k);
       int ans = 0;
       for (int i = 0;i<n;i++)
           for (int j = 0;j<n;j++)
           {
               ans+=upper_bound(e,e+k,-c[i]-d[j]) - lower_bound(e,e+k,-c[i]-d[j]);
           }
       printf("%d\n",ans);
       if (T)
           printf("\n");
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326007938&siteId=291194637