P1494 [national training team] Small Z so

Source: Internet
Author: User

Secretly cut off the team's template question...

This question is also a very common MO team template question.

Asking you for a probability is actually asking you\ (\ Sum {\ frac {CNT [I] \ times (CNT [I]-1)} {2 }}\). If the denominator is human, please. After finding two things, you can divide them.

The numerator is actually a maintenance method in the same question of "B's inquiry.

The answer varies with each number. Then we will lose our current contribution to the answer, update the CNT array, and add a new contribution to the answer, so we can maintain it.

Code:

# Include <cstdio> # include <cmath> # include <algorithm> # define ll long longconst int maxn = 50005; struct query {ll l, R, ID ;} ques [maxn]; ll belong [maxn]; ll out1 [maxn], out2 [maxn]; ll CNT [maxn]; ll a [maxn]; ll n, m; ll l = 1, r = 0, ANS = 0; bool CMP (query a, query B) {If (belong [. l] = belong [B. l]) return. r <B. r; return belong [. l] <belong [B. l];} ll read () {ll ans = 0, S = 1; char CH = getchar (); While (CH> '9' | ch <'0') {If (CH = '-') S =-1; CH = getchar ();} while (CH> = '0' & Ch <= '9') ans = (ANS <3) + (ANS <1) + CH-'0 ', ch = getchar (); Return S * ans;} void add (ll x) {ANS-= (CNT [x] * (CNT [x]-1)/2; CNT [x] ++; ans + = (CNT [x] * (CNT [x]-1)/2;} void del (ll x) {ANS-= (CNT [x] * (CNT [x]-1)/2; CNT [x] --; ans + = (CNT [x] * (CNT [x]-1)/2;} void moqueue () {ll block = SQRT (n); (INT I = 1; I <= N; I ++) belong [I] = (I-1)/block + 1; STD: Sort (ques + 1, ques + m + 1, CMP); For (INT I = 1; I <= m; I ++) {If (ques [I]. L = ques [I]. r) {out1 [ques [I]. id] = 0, out2 [ques [I]. id] = 1; continue;} while (L <ques [I]. l) del (A [L ++]); While (L> ques [I]. l) add (A [-- l]); While (r> ques [I]. r) del (A [R --]); While (r <ques [I]. r) add (A [++ R]); out1 [ques [I]. id] = ans; out2 [ques [I]. id] = (R-l + 1) * (R-l)/2;} ll gcd (ll x, ll y) {return y = 0? X: gcd (Y, X % Y);} int main () {n = read (), M = read (); For (INT I = 1; I <= N; I ++) A [I] = read (); For (INT I = 1; I <= m; I ++) Ques [I]. L = read (), ques [I]. R = read (), ques [I]. id = I; moqueue (); For (INT I = 1; I <= m; I ++) {ll ans1 = out1 [I], ans2 = out2 [I]; ll G = gcd (ans1, ans2); ans1/= g; ans2/= g; printf ("% LLD/% LLD \ n", ans1, ans2 );} return 0 ;}

p1494 [national training team] Xiao Z's so

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.