Advertisement
FHVirus

Untitled

Jan 7th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. // Knapsack DP is harder than FFT.
  2. #pragma GCC optimize("Ofast")
  3. #include<unistd.h>
  4. const int BUF_SIZE = 2e5 + 225;
  5. char OB[BUF_SIZE]; int OP;
  6. inline char RC(){static char buf[BUF_SIZE],*p=buf,*q=buf;return p==q&&(q=(p=buf)+read(0,buf,BUF_SIZE))==buf?-1:*p++;}
  7. inline int R(){static char c;int a;while((c=RC())<'0');a=c^'0';while((c=RC())>='0')a*=10,a+=c^'0';return a;}
  8. inline long long RL(){static char c;long long a;while((c=RC())<'0');a=c^'0';while((c=RC())>='0')a*=10,a+=c^'0';return a;}
  9. inline void W(long long n){static char buf[20],p;if(n==0)OB[OP++]='0';p=0;while(n)buf[p++]='0'+(n%10),n/=10;for(--p;p>=0;--p)OB[OP++]=buf[p];OB[OP++]='\n';if(OP>BUF_SIZE-20)write(1,OB,OP),OP=0;}
  10.  
  11. #include <algorithm>
  12. #include <vector>
  13. #include <queue>
  14. #define ll long long
  15. #define pii pair<int, ll>
  16. #define ff first
  17. #define ss second
  18. #define maxn 500005
  19. #define jizz 7122 + 1
  20. #define mod 1000000007
  21. using namespace std;
  22. const ll inf = 1LL<<60;
  23. ll a[jizz];
  24. bool done[jizz];
  25. ll edge[jizz], mind[jizz], pref[jizz], ans[maxn];
  26. vector<pii> que[jizz];
  27.  
  28. int x;
  29. int main() {
  30.     int n, m;
  31.     n = R(), m = R();
  32.     for(int i = 0; i < n; ++i) a[i] = R();
  33.     sort(a, a + n);
  34.     x = a[0];
  35.     for(int i = 0; i < x; ++i) edge[i] = inf, mind[i] = inf;
  36.     for(int i = 0; i < n; ++i){
  37.         edge[a[i] % x] = min(edge[a[i] % x], a[i]);
  38.     }
  39.     ll b;
  40.     for(int i = 0; i < m; ++i){
  41.         b = R();
  42.         edge[b % x] = min(edge[b % x], b);
  43.     }
  44.     //for (int i = 0;i < x;i++) cout << edge[i] << endl;
  45.     mind[0] = 0;
  46.     for (int i = 0;i < x;i++) {
  47.         ll mi = inf, ind = 0;
  48.         for (int j = 0;j < x;j++) {
  49.             if (!done[j] && mind[j] < mi) {
  50.                 mi = mind[j];
  51.                 ind = j;
  52.             }
  53.         }
  54.         done[ind] = true;
  55.         for (int j = 0;j < x;j++){
  56.             mind[j] = min(
  57.                 mind[j],
  58.                 mind[ind] + edge[j - ind < 0 ? j - ind + x : j - ind]
  59.             );
  60.         }
  61.     }
  62.     sort(mind, mind + x);
  63.     //for (int i = 0;i < x;i++) cout << mind[i] << endl;
  64.     int q = R();
  65.     ll l, r;
  66.     for (int i = 1;i <= q;i++) {
  67.         l = RL(); r = RL() + 1;
  68.         que[r % x].emplace_back( i, r);
  69.         que[l % x].emplace_back(-i, l);
  70.     }
  71.     for (int i = 0;i < x;i++) {
  72.         for (int j = 0;j < x;j++) {
  73.             pref[j] = (j ? pref[j - 1] : 0) + (i - mind[j] < 0 ? (i - mind[j]) / x : (i - mind[j] + x - 1) / x);
  74.             //cout << pref[j] << " ";
  75.         }
  76.         //cout << endl;
  77.         for (pii &&j: que[i]) {
  78.             int ind = lower_bound(mind, mind + x, j.ss) - mind;
  79.             //cout << j.ff << " " << ind << endl;
  80.             if (j.ff > 0) {
  81.                 ans[j.ff] += ind * (j.ss / x) + (ind ? pref[ind - 1] : 0);
  82.                 //cout << j.ff << " " << ind << " " << ind * (j.ss / x) + (ind ? pref[ind - 1] : 0) << endl;
  83.             } else {
  84.                 ans[-j.ff] -= ind * (j.ss / x) + (ind ? pref[ind - 1] : 0);
  85.             }
  86.         }
  87.     }
  88.     for (int i = 1;i <= q;i++) W(ans[i]);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement