Advertisement
welleyth

RMI/BOI 2022 Day 2 B

Oct 7th, 2022
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8. #define int long long
  9. #define mp make_pair
  10. #define pb push_back
  11.  
  12. #pragma GCC optimize("Ofast")
  13. #pragma GCC optimize("unroll-loops")
  14. #pragma GCC target("avx2")
  15.  
  16. constexpr int N = (int)8e4+11;
  17. constexpr int INF = (int)1e9 + 12315;
  18. constexpr int md = (int)1e9 + 7;
  19. constexpr int K = 1;
  20.  
  21. int A[N],B[N];
  22.  
  23. /// a+b is maximized, so b < M-a and b is maximum
  24. /// a+b-M is maximized, so b is just maximum
  25. int n,M,q;
  26. int f(vector<int>& v,int val){
  27.     int x = M - val;
  28.     if(v[0] >= x) return 0;
  29.     int pos = upper_bound(v.begin(),v.end(),x) - v.begin();
  30.     pos = min(pos,(int)v.size()-1);
  31.     while(pos >= 0 && v[pos] >= x) pos--;
  32.     int S = (val + v[pos]);
  33.     if(S >= M) S -= M;
  34.     return S;
  35. }
  36.  
  37. void solve(){
  38.     cin >> n >> M >> q;
  39.  
  40.     for(int i = 0; i < n; i++) cin >> A[i];
  41.     for(int i = 0; i < n; i++) cin >> B[i];
  42.  
  43.     int prec[n/K+1][n/K+1];
  44.     vector<int> AA[n/K+1];
  45.     vector<int> BB[n/K+1];
  46.     memset(prec,0,sizeof prec);
  47.     vector<int> v;
  48.  
  49.     {
  50.         set<int> st[2][n/K+1];
  51.         for(int i = 0; i < n; i++){
  52.             st[0][i/K].insert(A[i]);
  53.             st[1][i/K].insert(B[i]);
  54.         }
  55.         for(int i = 0; i < n/K; i++){
  56.             AA[i] = vector<int>(st[0][i].begin(),st[0][i].end());
  57.             BB[i] = vector<int>(st[1][i].begin(),st[1][i].end());
  58.         }
  59.     }
  60.     set<int> st;
  61.  
  62.     for(int i = 0; K * i < n; i++){
  63.         for(int j = 0; K * j < n; j++){
  64.             int p1 = K * i, p2 = K*j;
  65.             int mx1 = 0, mx2 = 0;
  66.             st.clear();
  67.             for(int t = p2; t < min(n,p2 + K); t++){
  68.                 st.insert(B[t]);
  69.             }
  70.             v = vector<int>(st.begin(),st.end());
  71.             for(int t = p1; t < min(n,p1 + K); t++){
  72.                 mx1 = max(mx1,A[t]);
  73.                 prec[i][j] = max(prec[i][j],f(v,A[t]));
  74.             }
  75.  
  76.             st.clear();
  77.             for(int t = p1; t < min(n,p1 + K); t++){
  78.                 st.insert(A[t]);
  79.             }
  80.             v = vector<int>(st.begin(),st.end());
  81.             for(int t = p2; t < min(n,p2 + K); t++){
  82.                 mx2 = max(mx2,B[t]);
  83.                 prec[i][j] = max(prec[i][j],f(v,B[t]));
  84.             }
  85.             prec[i][j] = max(prec[i][j],(mx1+mx2)%M);
  86.         }
  87.     }
  88.  
  89.     while(q--){
  90.         int a,b,c,d;
  91.         cin >> a >> b >> c >> d;
  92.         a--, b--, c--, d--;
  93.  
  94.         int block11 = a/K;
  95.         int block12 = b/K;
  96.         int block21 = c/K;
  97.         int block22 = d/K;
  98.  
  99.         int ans = 0;
  100.         int MX1 = 0, MX2 = 0;
  101. //        for(int i = a; i <= b; i++) MX1 = max(MX1,A[i]);
  102. //        for(int i = c; i <= d; i++) MX2 = max(MX2,B[i]);
  103.  
  104.         for(int i = block11+1; i <= block12-1; i++){
  105.             for(int j = block21+1; j <= block22-1; j++){
  106.                 ans = max(ans,prec[i][j]);
  107. //                cerr << "Segments [" << i*K+1 << ", " << (i+1)*K << "], [" << j*K+1 << ", " << (j+1)*K << "]\n";
  108.             }
  109.         }
  110.         int r1 =
  111.  
  112.         for(int i = a; i < min(b+1,(block11+1)*K); i++){
  113.             MX1 = max(MX1,A[i]);
  114.             for(int j = c; j < min(d+1,(block21+1)*K); j++){
  115.                 int S = A[i] + B[j];
  116.                 if(S >= M) S -= M;
  117.                 ans = max(ans,S);
  118.             }
  119.             for(int j = max(c,block22*K); j <= d; j++){
  120.                 int S = A[i] + B[j];
  121.                 if(S >= M) S -= M;
  122.                 ans = max(ans,S);
  123.             }
  124.             for(int j = block21+1; j <= block22-1; j++){
  125.                 MX2 = max(MX2,BB[j].back());
  126.                 ans = max(ans,f(BB[j],A[i]));
  127.             }
  128.         }
  129.  
  130.         for(int i = max(a,block12*K); i <= b; i++){
  131.             MX1 = max(MX1,A[i]);
  132.             for(int j = c; j < min(d+1,(block21+1)*K); j++){
  133.                 int S = A[i] + B[j];
  134.                 if(S >= M) S -= M;
  135.                 ans = max(ans,S);
  136.             }
  137.             for(int j = max(c,block22*K); j <= d; j++){
  138.                 int S = A[i] + B[j];
  139.                 if(S >= M) S -= M;
  140.                 ans = max(ans,S);
  141.             }
  142.             for(int j = block21+1; j <= block22-1; j++){
  143.                 MX2 = max(MX2,BB[j].back());
  144.                 ans = max(ans,f(BB[j],A[i]));
  145.             }
  146.         }
  147.  
  148.         for(int j = c; j < min(d+1,(block21+1)*K); j++){
  149.             MX2 = max(MX2,B[j]);
  150.             for(int i = a; i < min(b+1,(block11+1)*K); i++){
  151.                 int S = A[i] + B[j];
  152.                 if(S >= M) S -= M;
  153.                 ans = max(ans,S);
  154.             }
  155.             for(int i = max(a,block12*K); i <= b; i++){
  156.                 int S = A[i] + B[j];
  157.                 if(S >= M) S -= M;
  158.                 ans = max(ans,S);
  159.             }
  160.             for(int i = block11+1; j <= block12-1; j++){
  161.                 MX1 = max(MX1,AA[i].back());
  162.                 ans = max(ans,f(AA[i],B[j]));
  163.             }
  164.         }
  165.         for(int j = max(c,block22*K); j <= d; j++){
  166.             MX2 = max(MX2,B[j]);
  167.             for(int i = a; i < min(b+1,(block11+1)*K); i++){
  168.                 int S = A[i] + B[j];
  169.                 if(S >= M) S -= M;
  170.                 ans = max(ans,S);
  171.             }
  172.             for(int i = max(a,block12*K); i <= b; i++){
  173.                 int S = A[i] + B[j];
  174.                 if(S >= M) S -= M;
  175.                 ans = max(ans,S);
  176.             }
  177.             for(int i = block11+1; j <= block12-1; j++){
  178.                 MX1 = max(MX1,AA[i].back());
  179.                 ans = max(ans,f(AA[i],B[j]));
  180.             }
  181.         }
  182.         ans = max(ans,(MX1+MX2)%M);
  183.  
  184.         cout << ans << "\n";
  185.     }
  186.  
  187.     return;
  188. }
  189.  
  190. signed main(){
  191.     ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
  192.     int tests = 1;
  193. //    cin >> tests;
  194.     for(int test = 1; test <= tests; test++){
  195.         solve();
  196.     }
  197.     return 0;
  198. }
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement