HDU 5792 World is exploding (discretization + tree Array)

Source: Internet
Author: User
Tags cmath

Test instructions: Given n number, let you count a < b && C < d && A! = b! = c! = d && Aa < Ab && Ac > Ad.

Analysis: First, the number of the given is too large, first to be discretized, and then first to calculate AA < Ab and Ac > Ad. This can be solved with a tree-like array, a forward, a reverse, and then four kinds of numbers, and then subtract, just fine.

The code is as follows:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include < Bitset> #define DEBUG () puts ("++++"); #define GCD (A, B) __gcd (A, b) #define Lson l,m,rt<<1#define Rson m+1,r,rt <<1|1#define fi first#define se second#define pb push_back#define sqr (x) ((x) * (x)) #define MS (A, b) memset (A, C, Sizeo f a)//#define SZ size () #define PU push_up#define PD push_down#define CL Clear () #define ALL 1,n,1#define for (x,n) for (int i = (x); I < (n); ++i) #define FREOPENR freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using namespace std; typedef long Long Ll;typedef unsigned longLong Ull;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const LL LNF = 1e15;const Double INF = 1e20;const Doubl E PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 5e4 + 100;const int mod = 7;const int dr[] = {-1, 0, 1, 0};cons t int dc[] = {0, 1, 0, -1};const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "10 "1010", "1011", "1100", "1101", "1110", "1111"};int N, m;const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31  , 31};const int monn[] = {0, d, D, D, D,, D, D,, D,, 31};inline bool is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;} vector<int> v;int a[maxn];int sum[2][maxn<<1];int lowbit (int x) {return-x&x;}    void Add (int pos, int x) {while (x <= N) {++sum[pos][x];  x + = Lowbit (x);  }}int query (int pos, int x) {int ans = 0;    while (x) {ans + = sum[pos][x];  X-= Lowbit (x); } return ans; int GetPos (int x) {returnLower_bound (V.begin (), V.end (), X)-V.begin ();    int B[MAXN], C[MAXN], D[MAXN], E[maxn];int main () {while (scanf ("%d", &n) = = 1) {v.cl; MS (sum, 0);    V.PB (-1);      for (int i = 0; i < n; ++i) {scanf ("%d", a+i);    V.PB (A[i]);    } sort (V.begin (), V.end ());    V.erase (Unique (V.begin (), V.end ()), V.end ());    LL ans = 0;    LL tmp1 = 0, tmp2 = 0;    LL Tmp3 = 0, Tmp4 = 0, tmp5 = 0;      for (int i = 0; i < n; ++i) {int pos = GetPos (A[i]);      B[i] = query (0, pos-1); TMP1 + = B[i];      Min C[i] = i-query (0, POS);      Tmp3 + = (LL) b[i] * C[i];    Add (0, POS);      } for (int i = n-1; I >= 0; i.) {int pos = GetPos (A[i]);      D[i] = query (1, pos-1);  TMP2 + = D[i];      Right min e[i] = (n-i-1)-Query (1, POS);      Tmp4 + = (LL) d[i] * E[i]; Tmp4 + = (n-i-1)-Query (1, POS);    Right Max Add (1, POS);    } LL tmp6 = 0;      for (int i = 0; i < n; ++i) {TMP5 + = (LL) c[i] * E[i];    TMP6 + = (LL) b[i] * D[i]; } ans = tmp1 * TMP2-TMP3-TMP4-TMP5-TMP6;  printf ("%i64d\n", ans); } return 0;}

  

HDU 5792 World is exploding (discretization + tree Array)

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.