To P1314 smart quality supervision , for example
the first writing:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int N=200000+10; int v[N],w[N],n,m; int l[N],r[N]; int sum[N],cnt[N]; LL f(int W) { memset(sum,0,sizeof(sum)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) { if(w[i]>=W) { cnt[i]=cnt[i-1]+1; sum[i]=sum[i-1]+v[i]; } else { cnt[i]=cnt[i-1]; sum[i]=sum[i-1]; } } LL res=0; for(int i=1;i<=m;i++) { res=res+(sum[r[i]]-sum[l[i]-1])*(cnt[r[i]]-cnt[l[i]-1]); } return res; } int main() { LL S; cin>>n>>m>>S; for(int i=1;i<=n;i++) scanf("%d%d",w+i,v+i); for(int i=1;i<=m;i++) scanf("%d%d",l+i,r+i); int L=0,R=1000000+1,mid; the while (L <R & lt) // where demand is a function of the value of a decreasing function of the abscissa is strictly less than the minimum point S { MID = (L + R & lt) / 2 ; // the rounding board IF (F ( mid) < S) R = mid; // mid of the function value is less than S, can not be guaranteed at the mid-1 function value is less than S, R can be reduced to mid the else L = mid + . 1 ; // function value is greater than or equal at the mid S, L can be directly reduced to make the final mid + 1 L and R will be equal, not infinite loop } COUT << min (Sf of (L), F (L- . 1 ) -S) << endl; return 0 ; }
The second writing:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int N=200000+10; int v[N],w[N],n,m; int l[N],r[N]; int sum[N],cnt[N]; LL f(int W) { memset(sum,0,sizeof(sum)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) { if(w[i]>=W) { cnt[i]=cnt[i-1]+1; sum[i]=sum[i-1]+v[i]; } else { cnt[i]=cnt[i-1]; sum[i]=sum[i-1]; } } LL res=0; for(int i=1;i<=m;i++) { res=res+(sum[r[i]]-sum[l[i]-1])*(cnt[r[i]]-cnt[l[i]-1]); } return res; } int main() { LL S; cin>>n>>m>>S; for(int i=1;i<=n;i++) scanf("%d%d",w+i,v+i); for(int i=1;i<=m;i++) scanf("%d%d",l+i,r+i); int L=0,R=1000000+1,mid; the while (L <R & lt) // where demand is a function of the value of a decreasing function exceeds the maximum of the abscissa of the point S { MID = (L + R & lt + . 1 ) / 2 ; // the rounding board IF (F (mid)> = S) L = mid; // function value is greater than or equal at mid S, can not be guaranteed at the mid + 1 is greater than the value of the function equal to S, L can be reduced to mid the else R & lt = mid- 1 ; // mid the function value is less than S, R can be directly reduced to make the final mid-1 L and R will be equal, not infinite loop } COUT << min (F (L) -S, Sf of (L + . 1 )) << endl; return 0 ; }