Hiho1123_ Good pairing

Source: Internet
Author: User

Topics

Given two sequences A and B, each sequence may contain duplicate numbers.
A pairing (I,J) is a good match when a number AI is selected from the first sequence, a number BJ is selected from the second sequence and satisfies the AI>BJ.
Give two sequences and ask how many good pairs exist.
Topic Link: good pairing

     have the topic requirements, know the data volume of the topic is larger: A and B, respectively, there are up to 10^5 different numbers, each number has a maximum of 10^4. Therefore, the algorithm is required to have O (NLOGN) time complexity.  
     first used two map,map1 for the number in sequence A and the corresponding numbers MAP2 is the number of digits in Series B that are smaller than x for the number x in sequence a. In this way, when you enter sequence a for the first time, you create MAP1, and the value in MAP2 is set to 0, and when you enter sequence B, if the current reading value is x, the number is y, it is searched forward from the end of the MAP1 until the current key value in MAP1 is less than or equal to X, the key, Value), value is prefixed with Y, indicating that the number of digits in the sequence B is smaller than the key value increases by Y.  
     Finally, walk through Map1 and map2, sum map1[key]*map2[ Key] to get the final result.  
     result Gorgeous timeout: in the b sequence of each number, from the end to the first traversal map1, constitute O (n ^2) The complexity of the.

After the timeout, work toward the complexity of O (NLOGN): Use the Balanced binary tree node to maintain the value x, the number of nodes equal to X, the number of the total number of subtrees represented by the node. When reading sequence A, build this balanced binary tree, the complexity is O (Nlogn), when reading sequence B, to each number in B x, from the balanced binary tree to obtain the total number of numbers greater than x sum (time complexity O (LOGN), the final result plus y*sum.
Total time complexity of O (NLOGN)
The balanced binary tree is implemented using TREAP.

Implement
#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <set > #include <map> #include <vector> #include <queue> #include <stack> #include <unordered_ map> #include <unordered_set> #include <algorithm>using namespace std;struct node{int val;int count;int Sum;int priority; node* childs[2]; Node () {val = count = Sum = 0;childs[0] = childs[1] = null;priority = rand ();} void Update () {sum = Count;if (childs[0]) sum + = Childs[0]->sum;if (childs[1]) sum + = childs[1]->sum;}}; struct treap{node* root; Treap () {root = NULL;} void Delete (node*& Node) {if (!node) return;if (Node->childs[0]) Delete (node->childs[0]); if (node->childs [1]) Delete (node->childs[1]);d elete node;node = NULL; Note the assignment is NULL, otherwise there is an error when using treap repeatedly}void Rotate (node*& Node, bool dir) {node* ch = node->childs[dir];node->childs[ DIR] = Ch->childs[!dir];ch->childs[!dir] = node;node->update (); Note the update because the structure of the tree is modified at this time node = ch;} void Insert (node*&Amp node, int val, int count) {if (node = = NULL) {node = new node (); node->val = Val;node->sum = Node->count = Count;ret Urn;} if (Node->val = = val) {Node->count + = count;node->sum + = Count;return;} bool ch = node->val < Val;insert (Node->childs[ch], Val, count); if (Node->childs[ch]->priority > node-& gt;priority) {Rotate (node, ch);} Node->update (); Update, the structure of the tree is modified at this time}int bigger (node* Node, int val) {if (!node) return 0;if (Node->val = val) return (Node->childs[1]? nod e->childs[1]->sum:0), else if (Node->val < val) return bigger (Node->childs[1], Val); Else{return (node- &GT;CHILDS[1]? node->childs[1]->sum:0) + Node->count + bigger (Node->childs[0], val);}}; int main () {int T, n, M, X, y;scanf ("%d", &t); Treap Treap;while (t--) {scanf ("%d%d", &n, &m); treap. Delete (Treap.root); for (int i = 0; i < n; i++) {scanf ("%d%d", &x, &y); treap. Insert (Treap.root, x, y);} Long Long result = 0;for (int i = 0; i < m; i++) {scanf("%d%d", &x, &y); long long int bigger = Treap. Bigger (treap.root, x); result + = Y*bigger;} printf ("%lld\n", result);} return 0;}

Hiho1123_ Good pairing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.