G2A Many GEOs
SHARE
TWEET

Sqrt-decomposition

gerkulesov Apr 5th, 2020 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int n;
  8.     cin >> n;
  9.     int n_block = sqrt(n)+1;
  10.  
  11.     vector <int> a(n);
  12.     vector <int> b(n_block);
  13.  
  14.     for (int i=0; i<n; i++) {
  15.         cin >> a[i];
  16.         b[i/n_block] += a[i];
  17.     }
  18.  
  19.     while (1) {
  20.         int l, r;
  21.         cin >> l >> r;
  22.         int l_block=l/n_block, r_block=r/n_block;
  23.         int s = 0;
  24.         int idx_end=(l_block+1)*n_block-1;
  25.         for (int i=l; i<=idx_end; i++)
  26.             s += a[i];
  27.         for (int i=l_block+1; i<r_block; i++)
  28.             s += b[i];
  29.         for (int i=r_block*n_block; i<=r; i++)
  30.             s += a[i];
  31.         cout << s << endl;
  32.     }
  33.  
  34.     return 0;
  35. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top