To solve this problem, we need to determine the maximum number of valid buy-sell transactions within given queries on a sequence of nodes. Each node has an attribute (a_i) that indicates whether it's a buy (0) or sell (1) operation, and a type (G_i). The goal is to process multiple queries efficiently to determine the maximum number of valid transactions for each range [l, r].
Step-by-Step Reasoning:
Understanding Valid Transactions:
Buy (0): Can purchase one item of a specific type.
Sell (1): Can sell one item of a specific type, provided you have one to sell.
To ensure all items are sold by the end of the query range, the number of buys must equal the number of sells within that range for each type.
Preprocessing:
Grouping by Type: For each type t, maintain two lists:
buy_positions[t]: List of positions where a_i = 0 (buys) and G_i = t.
sell_positions[t]: List of positions where a_i = 1 (sells) and G_i = t.
Prefix Sums:
For each type t, compute prefix sums to quickly determine the number of buys and sells within any range [l, r].
Handling Queries:
For each query [l, r], for each type t, compute:
buys_t: Number of buys of type t in [l, r].
sells_t: Number of sells of type t in [l, r].
min(buys_t, sells_t): The number of valid transactions for type t.
The answer for the query is the sum of min(buys_t, sells_t) across all types.
Optimizations:
Binary Search: Utilize binary search to quickly count the number of buys and sells within the range [l, r] for each type.
Space Efficiency: Use vectors to store buy and sell positions instead of prefix sum arrays to save memory.
Edge Cases:
If there are more sells than buys for a type within a range, some sell operations cannot be matched and thus not counted.
Similarly, if there are more buys than sells, the excess buys cannot be matched and are not counted.
Final Implementation:
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Fast IO
struct FastIO {
FastIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
};
int main(){
FastIO fio;
int n, q;
cin >> n >> q;
vector<int> a(n+1);
for(int i=1;i<=n;i++) cin >> a[i];
vector<int> G(n+1);
for(int i=1;i<=n;i++) cin >> G[i];
// Collect buys and sells per type
// Assuming G[i] <= n
// To handle large G[i], use unordered_map with vector
// But for speed, assume G[i] <= 1e6 and use vector
int max_t = 0;
for(int i=1;i<=n;i++) max_t = max(max_t, G[i]);
// Initialize buy and sell lists
vector<vector<int>> buys(max_t+1, vector<int>());
vector<vector<int>> sells(max_t+1, vector<int>());
for(int i=1;i<=n;i++){
if(a[i]==0){
buys[G[i]].push_back(i);
}
else{
sells[G[i]].push_back(i);
}
}
// Read queries
struct Query{
int l, r, idx;
};
vector<Query> queries(q);
for(int i=0;i<q;i++){
cin >> queries[i].l >> queries[i].r;
queries[i].idx = i;
}
// Sort queries by r
sort(queries.begin(), queries.end(), [&](const Query &a, const Query &b) -> bool{
if(a.r != b.r) return a.r < b.r;
return a.l < b.l;
});
// For each type, sort buys and sells
for(int t=1;t<=max_t;t++){
sort(buys[t].begin(), buys[t].end());
sort(sells[t].begin(), sells[t].end());
}
// Prepare prefix sums for buys and sells
// To save memory, not using prefix arrays, using binary search on sorted lists
// Answer array
vector<int> ans(q, 0);
// Process each query
for(auto &query : queries){
int l = query.l;
int r = query.r;
int total = 0;
for(int t=1;t<=max_t;t++){
if(buys[t].empty() || sells[t].empty()) continue;
// Number of buys in [l,r]
int b = upper_bound(buys[t].begin(), buys[t].end(), r) - lower_bound(buys[t].begin(), buys[t].end(), l);
// Number of sells in [l,r]
int s = upper_bound(sells[t].begin(), sells[t].end(), r) - lower_bound(sells[t].begin(), sells[t].end(), l);
total += min(b, s);
}
ans[query.idx] = total;
}
// Restore the original query order
// Since queries were sorted by r, but we need to output in original order
// Create a vector to store answers in the original order
vector<int> final_ans(q, 0);
for(auto &query : queries){
final_ans[query.idx] = ans[query.idx];
}
for(int i=0;i<q;i++) cout << final_ans[i] << "\n";
}