#include <cmath>
#include <ctime>
#include <iostream>
#define WHITE -1
using namespace std;
const unsigned int TIME_CONVERSION_CONSTANT = 1000;
// Function, that teturn lowest power of 2, bigger than a given number
int lowestBiggerPowOf2Than(int number) {
int answer;
answer = 1 << (int)(log2((double)number - 1) + 1);
return answer;
}
// The structure describing the components from which consists a tree
struct Node {
int color;
long long sumOnSegment;
// The default constructor
Node() {
color = WHITE;
sumOnSegment = 0;
}
// Construct the node by its color and sum on corresponding segment
Node(int color, long long sumOnSegment) {
this->color = color;
this->sumOnSegment = sumOnSegment;
}
// Define, if this node is painted in some color (some number is assigned)
bool isColored() {
return color != WHITE;
}
};
// Function, that pushes information from parent to the children
void push(Node *tree, int currentNode, int left, int right) {
if (tree[currentNode].isColored()) {
int middle = (left + right) / 2;
int leftChild = 2 * currentNode;
tree[leftChild].color = tree[currentNode].color;
tree[leftChild + 1].color = tree[currentNode].color;
tree[leftChild].sumOnSegment = (long long)(middle - left + 1) * tree[leftChild].color;
tree[leftChild + 1].sumOnSegment = (long long)(right - middle) * tree[leftChild + 1].color;
tree[currentNode].color = WHITE;
}
}
// Function, that updates the value of all nodes on a segment for the specified
void update(Node *tree, int currentNode, int left, int right, int leftBorder, int rightBorder, int color) {
if (leftBorder == left && rightBorder == right) {
tree[currentNode] = Node(color, (long long)(right - left + 1) * color);
} else {
push(tree, currentNode, left, right);
int leftChild = 2 * currentNode;
int middle = (left + right) / 2;
if (rightBorder <= middle) {
update(tree, leftChild, left, middle, leftBorder, rightBorder, color);
} else if (leftBorder >= middle + 1) {
update(tree, leftChild + 1, middle + 1, right, leftBorder, rightBorder, color);
} else {
update(tree, leftChild, left, middle, leftBorder, middle, color);
update(tree, leftChild + 1, middle + 1, right, middle + 1, rightBorder, color);
}
tree[currentNode].sumOnSegment = tree[leftChild].sumOnSegment + tree[leftChild + 1].sumOnSegment;
}
}
// Function, that returns an array element by its position
Node getElement(Node *tree, int currentNode, int left, int right, int position) {
if (left == right) {
return tree[currentNode];
} else {
// Executes the retarded delayed update (pushes information)
push(tree, currentNode, left, right);
int middle = (left + right) / 2;
int leftChild = currentNode * 2;
if (position <= middle) {
return getElement(tree, leftChild, left, middle, position);
} else {
return getElement(tree, leftChild + 1, middle + 1, right, position);
}
}
}
// Function, that calculates the sum at a specified segment
long long sum(Node *tree, int currentNode, int left, int right, int leftBorder, int rightBorder) {
if (left == leftBorder && right == rightBorder) {
return tree[currentNode].sumOnSegment;
} else {
// Executes the retarded delayed update (pushes information)
push(tree, currentNode, left, right);
int leftChild = 2 * currentNode;
int middle = (left + right) / 2;
if (rightBorder <= middle) {
return sum(tree, leftChild, left, middle, leftBorder, rightBorder);
} else if (leftBorder >= middle + 1) {
return sum(tree, leftChild + 1, middle + 1, right, leftBorder, rightBorder);
} else {
return sum(tree, leftChild, left, middle, leftBorder, middle)
+ sum(tree, leftChild + 1, middle + 1, right, middle + 1, rightBorder);
}
}
}
int main() {
clock_t startTime;
ios::sync_with_stdio(false);
unsigned int quantityOfNumbers;
cin >> quantityOfNumbers;
unsigned int sizeOfTree = 2 * lowestBiggerPowOf2Than(quantityOfNumbers);
Node *tree = new Node[sizeOfTree];
unsigned int quantityOfRequests;
cin >> quantityOfRequests;
for (unsigned int indexOfRequest = 0; indexOfRequest < quantityOfRequests; indexOfRequest++) {
char request;
cin >> request;
unsigned int leftBorder, rightBorder;
if (request == 'A') {
int value;
cin >> leftBorder >> rightBorder >> value;
update(tree, 1, 0, quantityOfNumbers - 1, leftBorder - 1, rightBorder - 1, value);
} else if (request == 'Q') {
cin >> leftBorder >> rightBorder;
cout << sum(tree, 1, 0, quantityOfNumbers - 1, leftBorder - 1, rightBorder - 1) << endl;
}
}
cout << "Working time: " << (clock() - startTime) / (double) (CLOCKS_PER_SEC / TIME_CONVERSION_CONSTANT) << " ms" << endl;
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2RlZmluZSBXSElURSAtMQp1c2luZ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