Thinking about it:
When I saw this problem, I immediately thought of the HDU 1496, the two topics have similarities. My personal hdu1496: Click here
Save all the first and second numbers and A + B, record the number of occurrences of each number, and then calculate each C + D, for each C + D, the query-(c + D) appears several times.
So the subject needs a way to save all A + B's and its occurrences.
The most straightforward approach is to use the map in the STL, but the map is easy to time out when the data is large, so this approach is not ideal here.
Another way is to create a hash table yourself, because and may be negative, so you can add offset, so that all and plus the value of offset is greater than 0, and this value as a key. A hash table can be represented by an array of vectors.
Ps:
Compared to my own previous practice in hdu1496, this time I naturally thought of using vectors to represent the hash array, a lot of convenience. In general, the way you think about it should be easier to impress.
Reference:
0.http://acm.lilingfei.com/71_71
Code:
/** * AC @ SEP 2nd */#include <bits/stdc++.h>using namespace std;typedef long long ll;const int MAXN = 4000 + 5 0;const int hashsize = 999983; 149993-2.528s 299993-2.205s 999983-2.658const int OFFSET = 1 << 30;struct node{int value; LL CNT; Node (int V, LL c) {value = V; CNT = C; } Node () {}};int lst[maxn][5], n;std::vector<node> table[hashsize];void init () {for (int i=0; i
UVA 1152 4 Values whose Sum is 0