cosenza987

ssdasdasd

Jul 23rd, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. int sqt;
  8.  
  9. struct fuck {
  10.     int l, r, t;
  11.     friend bool operator < (const fuck a, const fuck b) {
  12.         if(a.l / sqt == b.l / sqt) {
  13.             return a.r < b.r;
  14.         } else {
  15.             return a.l / sqt < b.l / sqt;
  16.         }
  17.     }
  18. };
  19.  
  20.  
  21. int main() {
  22.     ios_base::sync_with_stdio(false);
  23.     cin.tie(nullptr);
  24.     int n, q;
  25.     cin >> n >> q;
  26.     vector<long long> v(n), ans(q), pr(n, -1);
  27.     vector<fuck> qq(q);
  28.     sqt = sqrtl(n);
  29.     for(int i = 0; i < n; i++) {
  30.         cin >> v[i];
  31.         map<int, int> mp;
  32.         int x = v[i];
  33.         for(long long j = 2; j * j <= x; j++) {
  34.             while(x % j == 0) {
  35.                 x /= j;
  36.                 mp[j]++;
  37.             }
  38.         }
  39.         if(x != 1) mp[x]++;
  40.         if(mp.size() == 1) {
  41.             pr[i] = mp.begin()->first;
  42.         } else if(mp.size() == 0) {
  43.             pr[i] = 4;
  44.         }
  45.     }
  46.     for(int i = 0; i < q; i++) {
  47.         cin >> qq[i].l >> qq[i].r;
  48.         qq[i].l--;
  49.         qq[i].r--;
  50.         qq[i].t = i;
  51.     }
  52.     sort(qq.begin(), qq.end());
  53.     int l = 0, r = -1;
  54.     int cnt = 0, oth = 0;
  55.     map<int, int> mp;
  56.     multiset<int> ms;
  57.     ms.insert(0);
  58.     for(int i = 0; i < q; i++) {
  59.         while(l > qq[i].l) {
  60.             l--;
  61.             if(pr[l] == -1) {
  62.                 cnt++;
  63.             } else {
  64.                 if(mp[pr[l]] != 0) {
  65.                     ms.erase(ms.find(mp[pr[l]]));
  66.                 }
  67.                 mp[pr[l]]++;
  68.                 oth++;
  69.                 ms.insert(mp[pr[l]]);
  70.             }
  71.         }
  72.         while(r > qq[i].r) {
  73.             if(pr[r] == -1) {
  74.                 cnt--;
  75.             } else {
  76.                 ms.erase(ms.find(mp[pr[r]]));
  77.                 mp[pr[r]]--;
  78.                 oth--;
  79.                 if(mp[pr[r]] == 0) {
  80.                     mp.erase(pr[r]);
  81.                 } else {
  82.                     ms.insert(mp[pr[r]]);
  83.                 }
  84.             }
  85.             r--;
  86.         }
  87.         while(l < qq[i].l) {
  88.             if(pr[l] == -1) {
  89.                 cnt--;
  90.             } else {
  91.                 ms.erase(ms.find(mp[pr[l]]));
  92.                 mp[pr[l]]--;
  93.                 oth--;
  94.                 if(mp[pr[l]] == 0) {
  95.                     mp.erase(pr[l]);
  96.                 } else {
  97.                     ms.insert(mp[pr[l]]);
  98.                 }
  99.             }
  100.             l++;
  101.         }
  102.         while(r < qq[i].r) {
  103.             r++;
  104.             if(pr[r] == -1) {
  105.                 cnt++;
  106.             } else {
  107.                 if(mp[pr[r]] != 0) {
  108.                     ms.erase(ms.find(mp[pr[r]]));
  109.                 }
  110.                 mp[pr[r]]++;
  111.                 oth++;
  112.                 ms.insert(mp[pr[r]]);
  113.             }
  114.         }
  115.         if(!cnt and (mp.size() == 1 or (mp.size() == 2 and mp.find(4) != mp.end()))) {
  116.             ans[qq[i].t] = -1;
  117.         } else {
  118.             int mx = *ms.rbegin();
  119.             if(mx * 2 > oth) {
  120.                 ans[qq[i].t] = mx;
  121.                 if(mp.find(4) != mp.end()) {
  122.                     if(mx == mp[4]) {
  123.                         auto itr = ms.end();
  124.                         itr--; itr--;
  125.                         int nmx = *itr;
  126.                         if(nmx * 2 > oth - mp[4]) {
  127.                             ans[qq[i].t] = nmx + mp[4];
  128.                         } else {
  129.                             ans[qq[i].t] = (oth - mp[4] + 1) / 2 + mp[4];
  130.                         }
  131.                     } else {
  132.                         ans[qq[i].t] = mx + mp[4];
  133.                     }
  134.                 }
  135.             } else {
  136.                 ans[qq[i].t] = (oth + 1) / 2;
  137.                 if(mp.find(4) != mp.end()) {
  138.                     if(mx * 2 > oth - mp[4] and mx != mp[4]) {
  139.                         ans[qq[i].t] = mx + mp[4];
  140.                     } else {
  141.                         if(mx * 2 == oth and mp.size() == 2) {
  142.                             ans[qq[i].t] = oth;
  143.                         } else {
  144.                             auto itr = ms.end();
  145.                             itr--; itr--;
  146.                             int nmx = *itr;
  147.                             if(nmx * 2 > oth - mp[4]) {
  148.                                 ans[qq[i].t] = nmx + mp[4];
  149.                             } else {
  150.                                 ans[qq[i].t] = (oth - mp[4] + 1) / 2 + mp[4];
  151.                             }
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     for(auto e : ans) {
  159.         cout << e << "\n";
  160.     }
  161.     return 0;
  162. }
Add Comment
Please, Sign In to add comment