#include <bits/stdc++.h> using namespace std; long long tab[200005], n, m, k, wynik, a, b, najw; long long pref[1000060], ile[1000060], tab2[200005]; int main() { scanf("%lld%lld", &n, &m); for (int i=1; i<=n; i++) { scanf("%lld", &tab[i]); } for (int i=1; i<=m; i++) { scanf("%lld", &tab2[i]); } for (int i=1; i<=n; i++) { pref[(tab[i]-tab[i-1])]++; ile[(tab[i]-tab[i-1])]++; } for (int i=1; i<=16; i++) { pref[i]*=i; pref[i]+=pref[i-1]; ile[i]+=ile[i-1]; //cout << i << " " << pref[i] << " " << ile[i] << endl; } for (int i=1; i<=m; i++) { //cout << ile[tab2[i]] << " " << tab2[i] << " " << pref[tab2[i]] << endl; najw=(ile[tab2[i]]*tab2[i])-pref[tab2[i]]; printf("%lld\n", najw); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <bits/stdc++.h> using namespace std; long long tab[200005], n, m, k, wynik, a, b, najw; long long pref[1000060], ile[1000060], tab2[200005]; int main() { scanf("%lld%lld", &n, &m); for (int i=1; i<=n; i++) { scanf("%lld", &tab[i]); } for (int i=1; i<=m; i++) { scanf("%lld", &tab2[i]); } for (int i=1; i<=n; i++) { pref[(tab[i]-tab[i-1])]++; ile[(tab[i]-tab[i-1])]++; } for (int i=1; i<=16; i++) { pref[i]*=i; pref[i]+=pref[i-1]; ile[i]+=ile[i-1]; //cout << i << " " << pref[i] << " " << ile[i] << endl; } for (int i=1; i<=m; i++) { //cout << ile[tab2[i]] << " " << tab2[i] << " " << pref[tab2[i]] << endl; najw=(ile[tab2[i]]*tab2[i])-pref[tab2[i]]; printf("%lld\n", najw); } } |