Describe
Small T is a quality supervisor, who recently checked the quality of a batch of minerals. This batch of minerals has n ore, numbered from 1 to N, each of which has its own weight of WI and the value VI. The process of inspecting minerals is:
1, given m interval [Li,ri];
2, select a parameter w;
3. For an interval [Li,ri], calculate the value of the ore on this interval Yi:
Yi =∑1*∑vj,j∈[li, Ri] and wj≥w,j is the ore number
The test results of this batch of minerals are the sum of the values of each interval. That is: Y =∑yi,i∈[1, M]
If the inspection result of this batch of minerals is too different from the standard value s, it is necessary to test another batch of minerals. Small t don't want to take the time to test another batch of minerals, so he wants to adjust the value of the parameter w, so that the test results as close as possible to the standard value s, even if the absolute minimum s-y. Please help to find the minimum value. Format input Format
The first line contains three integer n,m,s, representing the number of ores, the number of intervals, and the standard values.
The next n rows, 2 integers per line, separated by spaces in the middle, i+1 line represents the weight of I ore, WI and Value VI.
The next m line, which represents the interval, is 2 integers per line, separated by spaces, and the i+n+1 line represents the two endpoints of the interval [Li,ri] Li and Ri. Note: Different intervals may coincide or overlap each other. Output format
The output has only one row and contains an integer that represents the minimum value to be asked for. Sample Example Input
5 3
1 5
2 5
3 5 4 5
5 5 1 5 2 4 3-3
Sample output
10
Limit
1s tips
Sample Description: When the W selected 4, the three interval on the test value of 20, 5, 0, the test results of this batch of minerals 25, at this time and the standard value of the difference is the minimum of 10.
For 10% of the data, there are 1≤n,m≤10;
For 30% of the data, there are 1≤n,m≤500;
For 50% of the data, there are 1≤n,m≤5,000;
For 70% of the data, there are 1≤n,m≤10,000;
For 100% of the data, there are 1≤n,m≤200,000,0 < WI, vi≤10^6,0 < s≤10^12,1≤li≤ri≤n. Source
NOIp2011 the second problem of improving group Day2
To say that the beginning of the expression of Yi can not understand the example of thanks to the sample ...
We find that this problem allows the minimum of the difference and the monotone of the difference to increase with the decrease of the W
We can look for a test value that is greater than or equal to S, and a test value that is less than and closest to S, with a minimum value of two and S.
But because s<=10^12 let me start with int to read the child crying at the beginning only 30 points still think the two-point is dead.
Be sure to pay attention in the future ...
Because M and n are all int but be aware that the data in the same row is not necessarily a type ...
How do we obtain the test value SAV after the second cent W?
If you enumerate each interval and then enumerate all the ores from left to right
Because m can equal N and l=1 r=n is also possible, so the worst is the n^3 rhythm ...
We can use prefixes and ideas with g[i].x to denote the number of W in the 1-i over mid g[i].y indicates that the 1-i W exceeds mid Sigma V
Each time with the interval right endpoint minus the left end point minus one to get the number of matches on this interval
Per preprocessing complexity on
Evaluate the complexity of the test value OM
The total complexity is NLOGN level of the other
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <
Algorithm> using namespace std;
int m,n,a,b,c;
Long Long maxv,acstd; struct Self{long long x,y;}
S[200222],T[200222],G[200222]; S[i].x==>wi S[i].y==>vi T[i].x==>sigma J t[i].y==>sigma Vj g[i].x==>l g[i].y==>r Long Long L,r,mi
D,ANS,Z=99999999999999LL;
Long Long Sav;
void Yuchuli (int lim) {int A;
for (a=1;a<=m;a++) {t[a].x=t[a-1].x;
T[A].Y=T[A-1].Y;
if (S[a].x>=lim) {t[a].x++;
T[A].Y+=S[A].Y;
}} Long Long Jisuan () {int A;
Long Long l,r;
Long Long ret=0;
for (a=1;a<=n;a++) {l=t[g[a].y].x-t[g[a].x-1].x;
R=T[G[A].Y].Y-T[G[A].X-1].Y;
ret=ret+ (long Long) l*r;
return ret;
int main () {scanf ("%d%d%lld", &M,&N,&ACSTD); for (a=1;a<=m;a++) {scanf ("%lld%lld", &s[a].x,&s[a).y);
Maxv=max (S[A].X,MAXV);
for (a=1;a<=n;a++) scanf ("%lld%lld", &g[a].x,&g[a].y);
L=0;r=maxv;ans=-1;
while (l<=r) {mid= (l+r) >>1;
Yuchuli (mid);
Sav=jisuan ();
if (SAV<ACSTD) r=mid-1;
else {ans=sav;
l=mid+1;
} if (Ans!=-1) z=min (Z,abs (Acstd-ans));
L=0;r=maxv;ans=-1;
while (l<=r) {mid= (l+r) >>1;
Yuchuli (mid);
Sav=jisuan ();
if (SAV<ACSTD) {ans=sav;
R=mid-1;
else l=mid+1;
} if (Ans!=-1) z=min (Z,abs (Acstd-ans));
cout<<z<<endl;
return 0;
}