#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Binary Indexed Tree (Fenwick Tree) for range updates and queries
struct BIT {
int n;
vector<ll> tree;
BIT(int size){
n = size;
tree.assign(n+2, 0LL);
}
void update(int idx, ll val){
while(idx <= n){
tree[idx] += val;
idx += idx & -idx;
}
}
ll query(int idx) const {
ll res = 0;
int i = idx;
while(i > 0){
res += tree[i];
i -= i & -i;
}
return res;
}
};
// Interval management per x
struct IntervalSet {
set<pair<int, int>> intervals;
// Find all intervals that overlap with [a,b]
set<pair<int,int>>::iterator find_start(int a){
return intervals.lower_bound({a, -1});
}
// Add interval [a,b], return the list of newly added intervals
vector<pair<int,int>> add_interval(int a, int b){
vector<pair<int,int>> new_added;
if(intervals.empty()){
intervals.emplace(a, b);
new_added.emplace_back(a, b);
return new_added;
}
auto it = intervals.lower_bound({a, -1});
if(it != intervals.begin()){
--it;
}
// Merge overlapping intervals
int start = a, end = b;
vector<pair<int,int>> to_remove;
while(it != intervals.end() && it->first <= b){
if(it->second < a){
++it;
continue;
}
start = min(start, it->first);
end = max(end, it->second);
to_remove.emplace_back(*it);
++it;
}
// Remove old intervals
for(auto &p: to_remove) intervals.erase(p);
// Insert the merged interval
intervals.emplace(start, end);
// Now find the newly added parts: [a,b] minus existing overlapping
// To do this, iterate through existing intervals and subtract the overlaps
// The new added is [a,b] minus the union of existing intervals before merging
// Since we've merged, the entire [start, end] is now active
// The newly added parts are [a,b] minus the old overlapping intervals
// To find, we need to find the gaps in [a,b] that were not previously covered
// Since we removed all overlapping intervals, and merged into [start, end], the newly added is [start, end] - old intervals
// But it's complicated, alternatively, to find the difference during merging
// Instead, a simpler approach since we need to find what was not active before
// The new added parts are [start, end] minus the union of old intervals
// Since we already have the new [start, end], and we had [a,b], and overlapping intervals were removed
// The newly added parts are the parts of [start, end] that were not covered by the old intervals
// To compute difference, iterate from start to end and subtract old intervals
// Instead, since we have [a,b] and existing intervals before adding, let's track added
// To simplify, let's recompute the added parts by iterating [a,b] and checking coverage
// This is too slow, alternative is to track the added via [start, end] - union of to_remove
// So, calculate the total new interval [start, end], and subtract the to_remove intervals' overlaps with [start, end]
// The newly added parts are [start, end] minus the union of to_remove
// Implement this:
vector<pair<int,int>> overlaps;
for(auto &p: to_remove){
overlaps.emplace_back(max(p.first, a), min(p.second, b));
}
// Now compute [start, end] minus overlaps
vector<pair<int,int>> added;
int current = start;
for(auto &p: overlaps){
if(current < p.first){
added.emplace_back(current, p.first-1);
}
current = p.second +1;
}
if(current <= end){
added.emplace_back(current, end);
}
return added;
}
// Remove interval [a,b], return the list of removed intervals
vector<pair<int,int>> remove_interval(int a, int b){
vector<pair<int,int>> removed;
if(intervals.empty()) return removed;
auto it = intervals.lower_bound({a, -1});
if(it != intervals.begin()){
--it;
}
vector<pair<int,int>> to_remove;
vector<pair<int,int>> to_add;
while(it != intervals.end() && it->first <= b){
if(it->second < a){
++it;
continue;
}
// Overlapping with [a,b]
to_remove.emplace_back(*it);
// Calculate the parts not overlapping
if(it->first < a){
to_add.emplace_back(it->first, a-1);
}
if(it->second > b){
to_add.emplace_back(b+1, it->second);
}
++it;
}
// Remove old intervals
for(auto &p: to_remove) intervals.erase(p);
// Add back the non-overlapping parts
for(auto &p: to_add) intervals.emplace(p.first, p.second);
// The removed parts are the overlapping parts with [a,b]
for(auto &p: to_remove){
int rem_l = max(p.first, a);
int rem_r = min(p.second, b);
if(rem_l <= rem_r){
removed.emplace_back(rem_l, rem_r);
}
}
return removed;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
// Initialize BITs
BIT BIT1(n+2);
BIT BIT2(n+2);
// Map x to interval sets
// Since x can be up to 1e9 and q up to 5e5, map x to a compact index
// Use a vector and map to assign unique indices
unordered_map<int, int> x_map;
vector<IntervalSet> interval_sets;
int x_id = 0;
// To improve unordered_map performance, use a vector of unique x's
vector<int> operations_x;
// First pass to collect all unique x's
// But since it's online, we need to assign on the fly
for(int i=0;i<q;i++){
string op;
cin >> op;
if(op == "+" || op == "-"){
int a, b, x;
cin >> a >> b >> x;
operations_x.push_back(x);
}
else{
int a, b;
cin >> a >> b;
}
}
// Now assign unique ids
sort(operations_x.begin(), operations_x.end());
operations_x.erase(unique(operations_x.begin(), operations_x.end()), operations_x.end());
int total_x = operations_x.size();
// Assign x to indices 0 to total_x-1
// Create a mapping from x to id
// Use binary search on operations_x
// Re-read the input
cin.clear();
cin.seekg(0);
cin >> n >> q;
// Initialize interval_sets
interval_sets.resize(total_x);
for(int i=0;i<q;i++){
string op;
cin >> op;
if(op == "+"){
int a, b, x;
cin >> a >> b >> x;
// Find id of x
int idx = lower_bound(operations_x.begin(), operations_x.end(), x) - operations_x.begin();
// Add interval [a,b] to x's interval set
vector<pair<int,int>> added = interval_sets[idx].add_interval(a, b);
// For each added interval, update BITs
for(auto &[c,d]: added){
// Update BIT1: add 1 to [c,d]
BIT1.update(c, 1);
BIT1.update(d+1, -1);
// Update BIT2: add c to c and subtract (d+1)
BIT2.update(c, c);
BIT2.update(d+1, -(ll)d -1);
}
}
else if(op == "-"){
int a, b, x;
cin >> a >> b >> x;
// Find id of x
int idx = lower_bound(operations_x.begin(), operations_x.end(), x) - operations_x.begin();
// Remove interval [a,b] from x's interval set
vector<pair<int,int>> removed = interval_sets[idx].remove_interval(a, b);
// For each removed interval, update BITs
for(auto &[c,d]: removed){
// Update BIT1: subtract 1 from [c,d]
BIT1.update(c, -1);
BIT1.update(d+1, 1);
// Update BIT2: subtract c and add (d+1)
BIT2.update(c, -c);
BIT2.update(d+1, (ll)d +1);
}
}
else if(op == "?"){
int a, b;
cin >> a >> b;
// Calculate sum from a to b
// sum_{s=1}^b prefix_sum[s] = (b+1)*BIT1.query(b) - BIT2.query(b)
// sum_{s=1}^{a-1} prefix_sum[s] = (a)*BIT1.query(a-1) - BIT2.query(a-1)
ll sum_b = (ll)(b+1) * BIT1.query(b) - BIT2.query(b);
ll sum_a_1 = (ll)(a) * BIT1.query(a-1) - BIT2.query(a-1);
ll total = sum_b - sum_a_1;
cout << total << "\n";
}
}
}