Codeforces #526 F. Pudding monsters

Source: Internet
Author: User

Title Description:

gives a matrix of n * N, each row, column, and only one special point. Ask how many k * k matrices have exactly K special points in the matrix.

Problem Solving Ideas:

Under the transformation model, the matrix can be regarded as an arrangement of N, and the number of successive sub-sequences is a continuous one, that is, the maximum number minus the minimum number equals the length minus one. Then we can consider the division of the solution, for the cross-division of the case of M, can be divided into two major categories of consideration, the maximum value of the points on the side of the division, the same side of the point of treatment. First, you can directly enumerate the side length, you can calculate the length of the other side, in judging whether it is legal. The second, if I is on the left, J in the right, a case is max[j]-min[i] = I-j, then can be changed to max[j] + j = min[i] + I, because Max and Min are monotonous, so can be maintained with a monotonous queue and buckets.

Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #definei64 Long Long5 using namespacestd;6 7 Const intN = 5e4 +Ten;8 intN, S[n], mn[n], mx[n], Sum[n *2];9 i64 ans;Ten  One voidDivideintLintr) { A     if(L = = r)return; -     intMID = L + R >>1; - Divide (L, mid); theDivide (mid +1, R); -Mn[mid] = Mx[mid] =S[mid]; -Mn[mid +1] = Mx[mid +1] = S[mid +1]; -      for(inti = mid-1; I >= l; I--) mn[i] = min (S[i], mn[i +1]), mx[i] = max (S[i], mx[i +1]); +      for(inti = mid +2; I <= R; i + +) mn[i] = min (s[i], mn[i-1]), mx[i] = max (S[i], mx[i-1]); -      for(inti = mid; I >= l; I--) { +         intj = Mx[i]-Mn[i] +i; A         if(J > Mid && J <= R && Mx[j] < Mx[i] && Mn[j] > Mn[i]) ans + +; at     } -      for(intj = Mid +1; J <= R; J + +) { -         inti = J-mx[j] +Mn[j]; -         if(I >= l && I <= mid && mx[j] > Mx[i] && mn[j] < Mn[i]) ans + +; -     } -     intHea, Tai; inHea = Tai = mid +1; -      for(inti = mid; I >= l; I--) { to          while(Tai <= R && Mn[tai] > Mn[i]) + + Sum[mx[tai]-Tai + N], + +Tai; +          while(Hea < Tai && Mx[hea] < mx[i])--Sum[mx[hea]-Hea + n], + +Hea; -Ans + = (i64) sum[mn[i]-i +n]; the     } *      while(Hea < Tai)--Sum[mx[hea]-Hea + n], + +Hea; $Hea = Tai =mid;Panax Notoginseng      for(intj = Mid +1; J <= R; J + +) { -          while(Hea >= l && Mn[hea] > Mn[j]) + Sum[mx[hea] + hea],--Hea; the          while(Tai > Hea && Mx[tai] < mx[j])--sum[mx[tai] + Tai],--Tai; +Ans + = (i64) sum[mn[j] +j]; A     } the      while(Tai > Hea)--Sum[mx[tai] + Tai],--Tai; + } -  $ intMain () { $Ans =3e10; -scanf"%d", &n); -      for(inti =1; I <= N; i + +) { the         intx, y; -scanf"%d%d", &x, &y);WuyiS[X] =y; the     } -Divide1, n); Wuprintf"%lld", ans +(i64) n); -     return 0; About}

Codeforces #526 F. Pudding monsters

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.