HYSBZ 2038 small Z Socks (hose) (Mo team algorithm)

Source: Internet
Author: User
Tags gcd cmath

Test instructions: Chinese question.

Analysis: The very famous MO team algorithm, the first expression of this probability, should be the molecule: C (x1, 2) + C (x2, 2) + C (X3, 2) + ... + c (xn, 2) Denominator: C (n, 2), and then the expression of fractions, [X1 (x1-1) +x2 (x2-1) +...+XN (xn-1)]/(n (n-1)) then the simplification is obtained (Sigma (XI*XI)-N)/(n (n-1)), then the operation of each interval, offline, so that the sequence is divided into sqrt (n) block, and then with two pointers, the calculation of the data.

Be careful not to use i64d, the words wa to die ...

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 <assert.h> #include <sstream> #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 freopenr freopen ("In.txt "," R ", stdin) #define FREOPENW freopen (" OUT.txt "," w ", stdout) using namespace Std;typedef long long ll;typedef unsigned lo ng Long Ull;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const LL LNF = 0xffffffffffll;const double INF = 0x3  F3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-6;const int maxn = 50000 + 10;const int mod = 1e9 + 7;const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1 101 "," 1110 "," 1111 "};int N, m;const int mon[] = {0, 0,, +, +,,, 31};const int monn[] , d, N, 0, D, D, D, D,, D,, 31};inline bool is_in (int r, int c) {return R >= && r < N & amp;& C >= 0 && C < m;}  LL ANSX[MAXN], Ansy[maxn];int POS[MAXN], val[maxn];int cnt[maxn];struct node{int L, r, id;  BOOL operator < (const Node &p) const{return pos[l] < POS[P.L] | | pos[l] = = POS[P.L] && r < P.R; }}; Node A[MAXN];  ll x, y;void update (int l, int ok) {x-= Cnt[val[l]] * (LL) cnt[val[l]];  CNT[VAL[L]] + = OK;  x + = Cnt[val[l]] * (LL) cnt[val[l] "-OK; Y + = OK;}  void update (int i) {ansx[i] = x; Ansy[i] = y * (y-1);}  int main () {scanf ("%d%d", &n, &m);  for (int i = 1; I <= n; ++i) scanf ("%d", val+i); for (int i = 0; i < m; ++i) {scanf ("%d%d", &a[i].L, &AMP;A[I].R);  A[i].id = i;  } int t = sqrt (n + 0.5);  for (int i = 1; I <= n; ++i) pos[i] = i/t;  Sort (A, A + M);    for (int l = 1, i = 0, r = 0; i < m; ++i) {int L = A[I].L, r = A[I].R;    while (L < L) Update (l++,-1);    while (L > L) Update (--l, 1);    while (R < R) Update (++R, 1);    while (R > R) Update (r--,-1);  Update (a[i].id);    } for (int i = 0; i < m; ++i) {LL g = gcd (Ansx[i], ansy[i]);  printf ("%lld/%lld\n", ansx[i]/g, ansy[i]/g); } return 0;}

  

HYSBZ 2038 small Z Socks (hose) (Mo team algorithm)

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.