1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <cstring>
#include <cstdio>
#define ull int int
using namespace std;
struct point{
int a;
int b;
int c;
};
point tree[600100];
point operator % (const point& first, const point& second)
{
point third;
third.a = first.a+second.a;
third.b = first.b+second.b;
third.c = first.c+second.c;
return third;
}
void build_tree(int node, int a, int b) {
if(a > b) return; // Out of range
if(a == b) { // Leaf node
tree[node].a = 1; // Init value
tree[node].b = 0;
tree[node].c = 0;
return;
}
build_tree(node*2, a, (a+b)/2); // Init left child
build_tree(node*2+1, 1+(a+b)/2, b); // Init right child
tree[node] = tree[node*2]%tree[node*2+1]; // Init root value
}
/**
* Increment elements within range [i, j] with value value
*/
void update_tree(int node, int a, int b, int i, int j, int value) {
if(a > b || a > j || b < i) // Current segment is not within range [i, j]
return;
if(a == b) { // Leaf node
int temp = tree[node].c;
tree[node].c = tree[node].b;
tree[node].b = tree[node].a;
tree[node].a = temp;
return;
}
update_tree(node*2, a, (a+b)/2, i, j, value); // Updating left child
update_tree(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child
tree[node] = tree[node*2]%tree[node*2+1]; // Updating root with max value
}
/**
* Query tree to get max element value within range [i, j]
*/
point query_tree(int node, int a, int b, int i, int j) {
point pp;
pp.a = 0;
pp.b = 0;
pp.c = 0;
if(a > b || a > j || b < i) return pp; // Out of range
if(a >= i && b <= j) // Current segment is totally within range [i, j]
return tree[node];
point q1 = query_tree(node*2, a, (a+b)/2, i, j); // Query left child
point q2 = query_tree(1+node*2, 1+(a+b)/2, b, i, j); // Query right child
point res = q1%q2; // Return final result
return res;
}
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
build_tree(1, 0, n-1);
for(int i=0; i<m; i++) {
char cmd; int a, b;
scanf(" %c %d %d", &cmd, &a, &b);
a--;
b--;
if (cmd == 'M') {
update_tree(1, 0, n-1, a, b, 1);
}
else {
point answer = query_tree(1, 0, n-1, a, b);
printf("%d %d %d\n", answer.a, answer.b, answer.c);
}
}
printf("\n");
}
}
|