Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define memFill(arr, val) memset(arr, val, sizeof(arr))
- #define memCreate(arr, val, n) int arr[n]; memset(arr, val, sizeof(arr))
- #define loop(i, n) for (int i = 1; i <= n; i++)
- #define elif else if
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef double db;
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef pair<int, int> pii;
- typedef vector<pii> vpii;
- typedef set<pii> spii;
- typedef pair<int, pii> pipii;
- typedef map<int, int> mii;
- typedef map<string, int> msi;
- typedef unordered_map<int, int> umii;
- typedef pair<db, db> pdd;
- typedef vector<pdd> vpdd;
- #define M 150010
- #define B 400
- #define I INT_MAX
- #define f first
- #define s second
- int n, m, q, b, l[M], a[M], pos[M], sm[B], md[M];
- vi bs[M], ls[M];
- inline int G(int x){ // Parse the position
- if (x == -1) return 0;
- int y = l[x]; // get the value of the position in the current line
- 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
- }
- inline int Q(int x, int y){ // Query Function (x and y act as left and right)
- int res = 0;
- while (x % b != 0 and x <= y) res += G(x), x++; // Update Res based on Processed Value while block isn't even with left
- while (y % b != b - 1 and y >= x) res += G(y), y--; // Update Res based on Processed Value while right isn't B
- 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
- return res; // Return the sum
- }
- inline void U(int x){ // Update the Block
- for (int i = 0; i < bs[x].size(); i++){
- int h = G(bs[x][i]); // Get the Processed value
- sm[bs[x][i] / b] -= h; // Update the Sum array
- sm[bs[x][(i+1) % bs[x].size()] / b] += h; // Update the Sum Array
- } md[x]++; // Update the moved array
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cin >> n >> m >> q; b = sqrt(n); // Get values. Set the block value to sqrt(n)
- 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
- for (int i = 0; i < n; i++) cin >> a[i]; // Take in the passengers
- for (int x = 0; x < n; x += b){ // Loop left value
- int y = min(n-1, x + b - 1); // Get the right value
- for (int i = x; i <= y; i++){ // From left -> right
- sm[x / b] += a[i]; // Update sum array with the passenger based on block
- int h = l[i]; // Temp Variable to store line[left]
- if (bs[h].empty() or bs[h].back() / b != x / b) bs[h].push_back(i); // Set the bounds based on block
- else bs[h][bs[h].size() - 1] = i; // Else Set last bound as left
- }
- } int z, x, y;
- while (q--){ // get the queries
- cin >> z;
- if (z == 1) cin >> x >> y, y--, x--, cout << Q(x, y) << '\n'; // Query and Output
- else cin >> x, x--, U(x); // Update
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement