P1314, intelligent quality supervisor.
Test Instructions Topic Description
Small \ (t\) is a quality supervisor, recently responsible for testing the quality of a number of minerals. These minerals have a total of n\ ore, numbered from \ (1\) to \ (n\) , each with its own weight (w_i\) and Value (v_i\). The process for testing minerals is:
Given a \ (m\) interval \ ([l_i,r_i]\)
Select a parameter \ (w\);
For an interval \ ([l_i,r_i]\), calculate the ore's test value on this interval \ (y_i\)
The test results of this batch of minerals (y\) are the sum of the test values for each interval. That is:\ (y_1+y_2...+y_m\)
If the test results of this batch of minerals and the given standard value \ (s\) is too much difference, it is necessary to test another batch of minerals. Small \ (t\) do not want to take time to test another batch of minerals, so he wants to adjust the parameter \ (w\) value, so that the test results as close as possible to the standard value \ (s\), even if the absolute value of the (s-y\) is the smallest. Please help to find out the minimum value.
Input output Format input format:
The first line contains three integers \ (n,m,s\), each representing the number of ores, the number of intervals, and the standard values.
Next \ (n\) line, each line \ (2\) integer, the middle is separated by a space, the first \ (i+1\) line represents the weight of the \ (i\) ore (w_i\) and value \ (v_i\).
The next \ (m\) line, representing the interval, each line \ (2\) , is separated by a space, and the first \ (i+n+1\) line represents the interval \ ([l_i,r_i]\) two endpoints \ ( l_i\) and \ (r_i\). Note: Different intervals may overlap or coincide with each other.
Output format:
An integer that represents the minimum value that is being asked.
Input and Output Sample input example:
5 3 151 52 53 54 55 51 52 43 3
Sample output:
10
Description
"Input and Output sample description"
When \ (w\) selected \ (4\) , three interval on the test value is \ (20,5,0\), the test result of this batch of minerals is \ (25\), at this time with the standard value \ (s\ ) is the minimum difference between \ (10\).
"Data Range"
For \ (\%\) data, there are \ (1 \leq n,m \leq 10\);
For \ (\%\) data, there are \ (1 \leq n,m \leq 500\);
For \ (\%\) data, there are \ (1 \leq n,m \leq 5,000\);
For \ (\%\) data, there is \ (1≤n,m \leq 10,000\);
For \ (100\%\) data, there \ (1 \leq n,m \leq 200,000, \ 0<w_i,v_i≤10^6, \ 0<s \leq 10^{12},1 \leq l_i \leq R_i \le Q n\).
Ideas
I have the wrong \ (ans\) file. --alecli
This afternoon should alecli the big guy invited to brush \ (noip\) of the various blue questions, and then did this together.
The formula at the beginning of the picture looked at me with a face, but I think I can still like clear. There are altogether \ (n\) items, each with two attributes \ (w\) and \ (v\). We select a \ (w\), and then for each interval, where all the \ (w\) property is greater than \ (w\) the number of items multiplied by the sum of the \ (v\) values of these items, is the interval \ (y\) value. Add up the \ (y\) value of each interval, which is the total \ (y\) value. The title requires the minimum difference between the total \ (y\) value and the given number \ (s\) .
There are two points of obvious features:
- The larger the selected \ (w\) , the greater the number of items that each interval satisfies \ (w\) attribute greater than \ (w\) ;
- The larger the selected \ (w\) , the greater the sum of the v\ values of these items.
Because \ (y\) is positively related to these two properties, so \ (y\) about \ (w\) is monotonically incrementing, then we can calculate \ (y\) by the value of binary \ (w\) .
The second question is how to calculate \ (y\)quickly. Because there is no interval we are only asking for intervals and, so it is easy to think of preprocessing \ (o (n) \), query \ (o (1) \) prefix and. Specifically, we do this:
tmp=0;//统计Y值memset(s,0,sizeof s);//s是v的前缀和memset(cnt,0,sizeof cnt);//cnt是数量的前缀和for(LL i=1;i<=n;i++)//预处理{ if(w[i]>=W) s[i]=s[i-1]+v[i],cnt[i]=cnt[i-1]+1;//满足w>=W,对答案有贡献 else s[i]=s[i-1],cnt[i]=cnt[i-1];//对答案无贡献}for(LL i=1;i<=m;i++) tmp+=(s[r[i]]-s[l[i]-1])*(cnt[r[i]]-cnt[l[i]-1]);//查询每一段区间的Y值
Smooth \ (ac\).
AC Code
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const LL maxn=2e5+5; LL n,m,s,sum,tmp,ans=llong_max,l=llong_max,r=llong_min; LL W[MAXN],V[MAXN],L[MAXN],R[MAXN],S[MAXN],CNT[MAXN]; ll read () {ll re=0; Char Ch=getchar (); while (!isdigit (CH)) Ch=getchar (); while (IsDigit (CH)) re= (re<<3) + (re<<1) +ch-' 0 ', Ch=getchar (); return re;} ll check (ll lzq) {tmp=0; memset (s,0,sizeof s); memset (cnt,0,sizeof CNT); for (LL i=1;i<=n;i++) {if (w[i]>=lzq) s[i]=s[i-1]+v[i],cnt[i]=cnt[i-1]+1; else s[i]=s[i-1],cnt[i]=cnt[i-1]; } for (LL i=1;i<=m;i++) tmp+= (S[r[i]]-s[l[i]-1]) * (cnt[r[i]]-cnt[l[i]-1]); Sum=tmp-s; if (sum<0) sum=-sum; return tmp>s;} int main () {N=read (), M=read (), S=read (); for (LL i=1;i<=n;i++) W[i]=read (), V[i]=read (), L=min (L,w[i]), R=max (R,w[i]); for (LL i=1;i<=m;i++) L[i]=read (), R[i]=read (); while (l<=r) {LL mid= (l+r) >>1; if (!check (mid)) r=mid-1; else l=mid+1; if (ans>sum) ans=sum; } printf ("%lld", ans); return 0;}
Luogu P1314 Intelligent Quality Supervisor (binary + prefix and)