Advertisement
Guest User

reeeeee (with bad comments)

a guest
Apr 4th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define memFill(arr, val) memset(arr, val, sizeof(arr))
  3. #define memCreate(arr, val, n) int arr[n]; memset(arr, val, sizeof(arr))
  4. #define loop(i, n) for (int i = 1; i <= n; i++)
  5. #define elif else if
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. typedef unsigned long long ull;
  10. typedef double db;
  11. typedef vector<int> vi;
  12. typedef vector<vi> vvi;
  13. typedef pair<int, int> pii;
  14. typedef vector<pii> vpii;
  15. typedef set<pii> spii;
  16. typedef pair<int, pii> pipii;
  17. typedef map<int, int> mii;
  18. typedef map<string, int> msi;
  19. typedef unordered_map<int, int> umii;
  20. typedef pair<db, db> pdd;
  21. typedef vector<pdd> vpdd;
  22.  
  23. #define M 150010
  24. #define B 400
  25. #define I INT_MAX
  26. #define f first
  27. #define s second
  28.  
  29. int n, m, q, b, l[M], a[M], pos[M], sm[B], md[M];
  30. vi bs[M], ls[M];
  31.  
  32. inline int G(int x){ // Parse the position
  33.     if (x == -1) return 0;
  34.     int y = l[x]; // get the value of the position in the current line
  35.     return a[ls[y][(pos[x] - md[y] % ls[y].size() + ls[y].size()) % ls[y].size()]]; // A bit overcomplicated but checking the current passenger, and the moved increment with the line size to get the optimal value
  36. }
  37.  
  38. inline int Q(int x, int y){ // Query Function (x and y act as left and right)
  39.     int res = 0;
  40.     while (x % b != 0 and x <= y) res += G(x), x++; // Update Res based on Processed Value while block isn't even with left
  41.     while (y % b != b - 1 and y >= x) res += G(y), y--; // Update Res based on Processed Value while right isn't B
  42.     while (x <= y) res += sm[x / b], x += b; // close off the left to the right, and update Res with sum[x / b] and increment x by block
  43.     return res; // Return the sum
  44. }
  45.  
  46. inline void U(int x){ // Update the Block
  47.     for (int i = 0; i < bs[x].size(); i++){
  48.         int h = G(bs[x][i]); // Get the Processed value
  49.         sm[bs[x][i] / b] -= h; // Update the Sum array
  50.         sm[bs[x][(i+1) % bs[x].size()] / b] += h; // Update the Sum Array
  51.     } md[x]++; // Update the moved array
  52. }
  53.  
  54. int main(){
  55.     ios_base::sync_with_stdio(false);
  56.     cin.tie(NULL);
  57.    
  58.     cin >> n >> m >> q; b = sqrt(n); // Get values. Set the block value to sqrt(n)
  59.     for (int i = 0; i < n; i++) cin >> l[i], l[i]--, ls[l[i]].push_back(i), pos[i] = ls[l[i]].size() - 1; // Take in values. Update the position
  60.     for (int i = 0; i < n; i++) cin >> a[i]; // Take in the passengers
  61.     for (int x = 0; x < n; x += b){ // Loop left value
  62.         int y = min(n-1, x + b - 1); // Get the right value
  63.         for (int i = x; i <= y; i++){ // From left -> right
  64.             sm[x / b] += a[i]; // Update sum array with the passenger based on block
  65.             int h = l[i]; // Temp Variable to store line[left]
  66.             if (bs[h].empty() or bs[h].back() / b != x / b) bs[h].push_back(i); // Set the bounds based on block
  67.             else bs[h][bs[h].size() - 1] = i; // Else Set last bound as left
  68.         }
  69.     } int z, x, y;
  70.     while (q--){ // get the queries
  71.         cin >> z;
  72.         if (z == 1) cin >> x >> y, y--, x--, cout << Q(x, y) << '\n'; // Query and Output
  73.         else cin >> x, x--, U(x); // Update
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement