C11LOCK - Ổ khóa đặc biệt

Tác giả: RR

Ngôn ngữ: C++

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (REACHEOF) return 0;\
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

int n, S, a[10][511];
int x[2][511*511], cnt[2][511*511], nx[2];

int main() {
    while (scanf("%d%d", &n, &S) == 2) {
        FOR(i,1,5) FOR(j,1,n) scanf("%d", &a[i][j]);
        long long res = 0;

        REP(turn,2) {
            int u, v;
            if (turn == 0) u = 2, v = 3;
            else u = 4, v = 5;

            nx[turn] = 0;
            FOR(i,1,n) FOR(j,1,n) {
                x[turn][++nx[turn]] = a[u][i] + a[v][j];
            }
            sort(x[turn]+1, x[turn]+nx[turn]+1);

            int j = 0;
            x[turn][0] = x[turn][1] - 1;
            FOR(i,1,nx[turn]) {
                if (x[turn][i] != x[turn][j]) {
                    ++j;
                    x[turn][j] = x[turn][i];
                    cnt[turn][j] = 1;
                }
                else ++cnt[turn][j];
            }
            nx[turn] = j;
        }

        FOR(t,1,n) {
            int need = S - a[1][t];
            int i = 1, j = nx[1];
            while (i <= nx[0] && j >= 1) {
                if (x[0][i] + x[1][j] == need) {
                    res += cnt[0][i] * (long long) cnt[1][j];
                    ++i; --j;
                }
                else if (x[0][i] + x[1][j] < need) {
                    ++i;
                }
                else --j;
            }
        }

        cout << res << endl;
    }
    return 0;
}

Download