Title Description
小T
He is a quality supervisor, recently responsible for inspecting the quality of a number of minerals. These minerals There \ (n \) th ore from \ (1 \) to \ (n \) individually numbered, and each mineral has its own weight \ (w_i \) and the value of \ (v_i \) . Mineral inspection process is:
Given m intervals \ (L_i, R_i \)
Select a parameter \ (W \) ;
For an interval \ ([L_i, R_i] \) , calculated on this interval ore check value the Y I :
\[ Y_i= ( \sum_j 1)(\sum_j {v_j}),\quad j \in [L_i,R_i] ,\quad w_j \geq W \]
Which, \ (J \) is the number of ore.
Test results of these minerals \ (Y \) to test the value of each section and. Namely: \ (Y_1 + ... + Y_2 Y_M \)
If these test results and minerals given standard value \ (S \) too much difference, you need to go check another batch of minerals. 小T
Do not want time-consuming to test another group of minerals, so he wanted to adjust the parameters \ (W \) values, so that the test results as much as possible close to the standard value \ (S \) , even if have to \ (SY \) the absolute value of the minimum . Please help find the minimum value.
Input Format
The first line contains three integers \ (n-, m \) , each number represents the number of standard values and ore section.
The next \ (n-\) rows, each row \ (2 \) integers, separated by a space, the \ (i + 1 \) line represents \ (I \) by weight of the ore number \ (W_i \) and the value of \ (v_i \) .
The next \ (m \) line, representing the interval, each row \ (2 \) integer, the intermediate separated by spaces, the \ (i + n + 1 \ ) line representing the interval \ ([L_i, R_i] \ ) two endpoints \ (L_i \) and \ (R_i \) . Note: Different intervals may coincide or overlap.
Output Format
An integer that indicates the minimum value required.
Sample input and output
Input # 1
. 5 15. 3
. 1. 5
2. 5
. 3. 5
. 4. 5
. 5. 5
. 1. 5
2. 4
. 3. 3
Output # 1
10
Description / Tips
Example Description [O]
When \ (W is \) selected from the group \ (4 \) when the value of the three test intervals were \ (20,5,0 \) , mineral test results for these \ (25 \) , then the standard value \ (S \) differ minimum \ (10 \) .
【data range】
For \ (10 \% \) data, there are \ (. 1 ≤n, m≤10 \) ;
For \ (30 \% \) of data, there are \ (. 1 ≤n, m≤500 \) ;
For \ (50 \% \) data, there are \ (. 1 ≤n, m≤5,000 \) ;
For \ (70 \% \) data, there are \ (. 1 ≤n, m≤10,000 \) ;
For \ (100 \% \) of data, there are \ (1 ≤n, m≤200,000,0 <w_i , v_i≤10 ^ 6,0 <S≤10 ^ {12}, 1 ≤L_i ≤R_i ≤n \ )
The solution of the problem
This is obviously a half board, without thinking of the TLE
I feel this topic Background say to any extent is very strange ah
First consider the function \ (Y (W_ {cur} ) \) , intuition tells us \ (Y (W_ {cur} ) \) is monotonic.Prove too lazy to write. In fact, I am not going to write
\[ Y(W_{cur})=\sum_i^m ( ( \sum_j 1) (\sum_j {v_i})),\quad j \in [L_i,R_i] ,\quad w_j \geq W_{cur} \]
Next is to find the distance \ (Y (W_ {cur} ) - S \) zeros nearest integer value. Since \ (Y (W_ {cur} ) \) is monotonic, it points directly on the titanium.
\ (. 1 ^ {\ CIRC} \ Quad \) \ (the Y (W_} {CUR) - S \ GEQ 0 \) , \ (CUR W_} {\) is too small, which transfer large;
\ (2 ^ {\ CIRC} \ Quad \) \ (the Y (W_} {CUR) - S <0 \) , \ (CUR W_} {\) is too large, it will transfer a small;
↓ Then just hit a \ (Y (W_ {cur} ) \) function, exploded.Even the wording of the beginning of the violence index also wrong, even still here for a long time cards
inline ll FY1(ll curW)
{
ll Y=0,cnt_j=0,sumv_of_range=0;
for(int i=1;i<=m;i++)
{
cnt_j=0;
sumv_of_range=0;
for(int j=L[i];j<=R[i];j++)
{
if(w[j]>=curW)
{
cnt_j++;
sumv_of_range+=v[j];
}
}
Y+=sumv_of_range*cnt_j;
}
return Y;
}
The idea here is not difficult to optimize, two prefixes array \ (pre_n [], pre_v [ ] \) can be.
But no way, each invocation \ (the Y-(CUR W_ {}) \) , \ (pre_n [], pre_v [] \) must be recalculated. But the speed is much faster than the original.
Refer to code.
code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
char c=getchar();int x=0;
for(;!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())
x=x*10+c-'0';
return x;
}
const int N=200010;
const ll LLINF=0x3f3f3f3f3f3f3f3f;
int v[N],w[N],L[N],R[N];
ll n,m,S;
ll pre_n[N],pre_v[N];
// inline ll FY1(ll curW)
// {
// ll Y=0,cnt_j=0,sumv_of_range=0;
// for(int i=1;i<=m;i++)
// {
// cnt_j=0;
// sumv_of_range=0;
// for(int j=L[i];j<=R[i];j++)
// {
// if(w[j]>=curW)
// {
// cnt_j++;
// sumv_of_range+=v[j];
// }
// }
// Y+=sumv_of_range*cnt_j;
// }
// return Y;
// }
inline ll FY(int curW)
{
ll Y=0;
memset(pre_n,0,sizeof(pre_n));
memset(pre_v,0,sizeof(pre_v));
for(int i=1;i<=n;i++)
{
if(w[i]>=curW)
{
pre_n[i]=pre_n[i-1]+1;
pre_v[i]=pre_v[i-1]+v[i];
}
else
{
pre_n[i]=pre_n[i-1];
pre_v[i]=pre_v[i-1];
}
}
for(int i=1;i<=m;i++)
Y+=(pre_n[R[i]]-pre_n[L[i]-1])*(pre_v[R[i]]-pre_v[L[i]-1]);
return Y;
}
int main()
{
//freopen("C:\\Users\\Administrator\\Downloads\\testdata (2).in","r",stdin);
ll minw=LLINF,maxw=0;
cin>>n>>m>>S;
for(int i=1;i<=n;i++)
{
w[i]=read();
v[i]=read();
if(maxw<w[i])maxw=w[i];
if(minw>w[i])minw=w[i];
}
for(int i=1;i<=m;i++)
{
L[i]=read();
R[i]=read();
}
ll l=minw-1,r=maxw+2,mid,ans=LLINF;
while(l<=r)
{
mid=((l+r)>>1);
ll Y=FY(mid);
if(Y>=S)l=mid+1;
else r=mid-1;
ans=min(ans,llabs(S-Y));
}
printf("%lld",ans);
return 0;
}