1012 World is exploding
Test instructions: Select four numbers to meet A<b and A[a]<a[b] c<d and A[c]>a[d] Ask a few such collections
Ideas:
Tree-like array + offline
First, the left of each number is smaller than it, and the right is larger than its size, denoted by cnt[][i]. The last statistic, minus the duplicates, you can.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <sstream>5#include <string>6#include <algorithm>7#include <list>8#include <map>9#include <vector>Ten#include <queue> One#include <stack> A#include <cmath> -#include <cstdlib> - using namespacestd; thetypedefLong Longll; -ll cnt[4][50005]; -ll cnt1[50005],cnt2[50005]; -ll a[50005]; + - structnode{ + ll Num,id; A}sub_a[50005]; at - -ll MAXN =0; - ll N; -ll tree[100005]; - BOOLCMP (Node A, Node B) {//Sort by Number in returnA.num <B.num; - } to voidDiscrete () {//discretization of +Sort (sub_a+1, sub_a+1+N, CMP); -a[sub_a[1].id] =1; theMAXN =1; * for(LL i =2; I <= N; i++) { $ if(Sub_a[i].num! = sub_a[i-1].num)Panax NotoginsengA[sub_a[i].id] =i; - Else theA[sub_a[i].id] = a[sub_a[i-1].id]; +MAXN =Max (maxn,a[sub_a[i].id]); A } the } + voidAdd (ll k) { - while(k <=N) { $TREE[K] + +; $K + = k & (-k); - } - } the ll Read (ll K) { -ll ans =0;Wuyi while(k) { theAns + =Tree[k]; -K-= k & (-k); Wu } - returnans; About } $ - intMain () { - while(SCANF ("%i64d", &n)! =EOF) - { Amemset (Cnt1,0,sizeof(Cnt1)); +memset (Cnt2,0,sizeof(Cnt2)); the for(LL i =1; I <= N; i + +){ -scanf"%i64d",&sub_a[i].num); $Sub_a[i].id =i; the } the discrete (); thememset (Tree,0,sizeof(tree)); the //It 's smaller on its right. - for(ll i = n; I >=1; i--){ in the Add (A[i]); thecnt[0][i] = Read (A[i]-1); About } the //on the left of it is smaller than it thememset (Tree,0,sizeof(tree)); the for(LL i =1; I <= N; i + +){ + Add (A[i]); -cnt[1][i] = Read (A[i]-1); theA[i] = MAXN +1-A[i];Bayi } the //It 's bigger on its right. thememset (Tree,0,sizeof(tree)); - for(ll i = n; I >=1; I--){ - the Add (A[i]); thecnt[2][i] = Read (A[i]-1); the } the //It 's bigger than it on the left. -memset (Tree,0,sizeof(tree)); the for(LL i =1; I <= N; i + +){ the Add (A[i]); thecnt[3][i] = Read (A[i]-1);94A[i] = MAXN +1-A[i]; the } thell Cnta =0, CNTB =0; the for(LL i =1; I <= N; i + +){98Cnt1[i] = cnt[0][i] + cnt[3][i]; AboutCnta + =Cnt1[i]; -Cnt2[i] = cnt[1][i] + cnt[2][i];101CNTB + =Cnt2[i];102 }103Cnta = Cnta/2;104CNTB = CNTB/2; the Long LongAns = Cnta *Cntb;106 for(LL i =1; I <= N; i + +){107Ans-= cnt1[i] *Cnt2[i];108 }109printf"%i64d\n", ans); the }111 return 0; the}
Multi-university Training Contest 5 1012 World is exploding tree array + offline