#include <iostream>
#include <cstdio>
using namespace std;
class node
{
public:
int left_r,right_r,count_0,count_1,count_2,change;
node *left,*right;
node() {
left_r=right_r=count_0=count_1=count_2=change=0;
left=right=NULL;
}
};
void maketree(int lower , int higher , node *current )
{
current->count_0=higher-lower+1;
current->left_r=lower;
current->right_r=higher;
if(higher!=lower) {
node *leftt=new node();
node *rightt=new node();
current->left=leftt;
current->right=rightt;
maketree(lower,(lower+higher)/2,leftt);
maketree((lower+higher)/2+1,higher,rightt);
}
}
void rotate(node *current)
{
int temp=current->count_2;
current->count_2=current->count_1;
current->count_1=current->count_0;
current->count_0=temp;
}
void copyit(node *copy,node *current)
{
copy->count_0=current->count_0;
copy->count_1=current->count_1;
copy->count_2=current->count_2;
copy->change=current->change;
}
void add(node *current,node *ch1,node *ch2)
{
current->count_0=ch1->count_0+ch2->count_0;
current->count_1=ch1->count_1+ch2->count_1;
current->count_2=ch1->count_2+ch2->count_2;
}
void get(node *current,node * child1,node *child2)
{
node *temp1_ch=new node();
node *temp2_ch=new node();
copyit(temp1_ch,child1);
copyit(temp2_ch,child2);
temp1_ch->change=temp1_ch->change%3;
temp2_ch->change=temp2_ch->change%3;
while(temp1_ch->change!=0) {
rotate(temp1_ch);
temp1_ch->change--;
}
while(temp2_ch->change!=0) {
rotate(temp2_ch);
temp2_ch->change--;
}
add(current,temp1_ch,temp2_ch);
}
void IS(node *current)
{
rotate(current);
current->change=0;
if(current->left!=NULL) {
current->left->change++;
current->right->change++;
}
}
void updateRMQ(int lower,int higher,node *current)
{
int mid=(current->left_r+current->right_r)/2;
if(current->change%3>0) {
IS(current);
}
if(current->change%3>0) {
IS(current);
}
if(lower==current->left_r&&higher==current->right_r) {
IS(current);
} else {
if(lower>mid) {
updateRMQ(lower,higher,current->right);
} else if(higher<=mid) {
updateRMQ(lower,higher,current->left);
} else {
updateRMQ(lower,mid,current->left);
updateRMQ(mid+1,higher,current->right);
}
get(current,current->left,current->right);
}
}
int countRMQ(int lower,int higher,node *current)
{
int mid=(current->left_r+current->right_r)/2;
if(current->change%3>0) {
IS(current);
}
if(current->change%3>0) {
IS(current);
}
if(lower==current->left_r&&higher==current->right_r) {
if(current->change%3>0) {
IS(current);
}
if(current->change%3>0) {
IS(current);
}
//cout<<current->count_0<<endl;
return current->count_0;
} else {
if(lower>mid) {
return countRMQ(lower,higher,current->right);
} else if(higher<=mid) {
return countRMQ(lower,higher,current->left);
} else {
return countRMQ(lower,mid,current->left)+countRMQ(mid+1,higher,current->right);
}
}
}
int main()
{
int N,Q;
//cin>>N>>Q;
scanf("%d%d",&N,&Q);
node *top=new node();
maketree(0,N-1,top);
while(Q--) {
int query,lower,higher;
scanf("%d%d%d",&query,&lower,&higher);
//cin>>query>>lower>>higher;
if(query) {
printf("%d\n",countRMQ(lower,higher,top));
//cout<<countRMQ(lower,higher,top)<<endl;
//cout<<top->left->count_0<<"\t"<<top->left->count_1<<"\t"<<top->left->count_2<<"\t"<<top->left->change<<endl;
//cout<<top->right->count_0<<"\t"<<top->right->count_1<<"\t"<<top->right->count_2<<"\t"<<top->right->change<<endl;
} else {
updateRMQ(lower,higher,top);
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjbGFzcyBub2RlCnsKcHVibGljOgogICAgICAgIGludCBsZWZ0X3IscmlnaHRfcixjb3VudF8wLGNvdW50XzEsY291bnRfMixjaGFuZ2U7CiAgICAgICAgbm9kZSAqbGVmdCwqcmlnaHQ7CiAgICAgICAgbm9kZSgpIHsKICAgICAgICAgICAgICAgIGxlZnRfcj1yaWdodF9yPWNvdW50XzA9Y291bnRfMT1jb3VudF8yPWNoYW5nZT0wOwogICAgICAgICAgICAgICAgbGVmdD1yaWdodD1OVUxMOwogICAgICAgIH0KfTsKdm9pZCBtYWtldHJlZShpbnQgbG93ZXIgLCBpbnQgaGlnaGVyICwgbm9kZSAqY3VycmVudCApCnsKICAgICAgICBjdXJyZW50LT5jb3VudF8wPWhpZ2hlci1sb3dlcisxOwogICAgICAgIGN1cnJlbnQtPmxlZnRfcj1sb3dlcjsKICAgICAgICBjdXJyZW50LT5yaWdodF9yPWhpZ2hlcjsKICAgICAgICBpZihoaWdoZXIhPWxvd2VyKSB7CiAgICAgICAgICAgICAgICBub2RlICpsZWZ0dD1uZXcgbm9kZSgpOwogICAgICAgICAgICAgICAgbm9kZSAqcmlnaHR0PW5ldyBub2RlKCk7CiAgICAgICAgICAgICAgICBjdXJyZW50LT5sZWZ0PWxlZnR0OwogICAgICAgICAgICAgICAgY3VycmVudC0+cmlnaHQ9cmlnaHR0OwogICAgICAgICAgICAgICAgbWFrZXRyZWUobG93ZXIsKGxvd2VyK2hpZ2hlcikvMixsZWZ0dCk7CiAgICAgICAgICAgICAgICBtYWtldHJlZSgobG93ZXIraGlnaGVyKS8yKzEsaGlnaGVyLHJpZ2h0dCk7CiAgICAgICAgfQp9CnZvaWQgcm90YXRlKG5vZGUgKmN1cnJlbnQpCnsKICAgICAgICBpbnQgdGVtcD1jdXJyZW50LT5jb3VudF8yOwogICAgICAgIGN1cnJlbnQtPmNvdW50XzI9Y3VycmVudC0+Y291bnRfMTsKICAgICAgICBjdXJyZW50LT5jb3VudF8xPWN1cnJlbnQtPmNvdW50XzA7CiAgICAgICAgY3VycmVudC0+Y291bnRfMD10ZW1wOwp9CnZvaWQgY29weWl0KG5vZGUgKmNvcHksbm9kZSAqY3VycmVudCkKewogICAgICAgIGNvcHktPmNvdW50XzA9Y3VycmVudC0+Y291bnRfMDsKICAgICAgICBjb3B5LT5jb3VudF8xPWN1cnJlbnQtPmNvdW50XzE7CiAgICAgICAgY29weS0+Y291bnRfMj1jdXJyZW50LT5jb3VudF8yOwogICAgICAgIGNvcHktPmNoYW5nZT1jdXJyZW50LT5jaGFuZ2U7Cn0Kdm9pZCBhZGQobm9kZSAqY3VycmVudCxub2RlICpjaDEsbm9kZSAqY2gyKQp7CiAgICAgICAgY3VycmVudC0+Y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PmNoYW5nZSsrOwogICAgICAgICAgICAgICAgY3VycmVudC0+cmlnaHQtPmNoYW5nZSsrOwogICAgICAgIH0KfQp2b2lkIHVwZGF0ZVJNUShpbnQgbG93ZXIsaW50IGhpZ2hlcixub2RlICpjdXJyZW50KQp7CiAgICAgICAgaW50IG1pZD0oY3VycmVudC0+bGVmdF9yK2N1cnJlbnQtPnJpZ2h0X3IpLzI7CiAgICAgICAgaWYoY3VycmVudC0+Y2hhbmdlJTM+MCkgewogICAgICAgICAgICAgICAgSVMoY3VycmVudCk7CiAgICAgICAgfQogICAgICAgIGlmKGN1cnJlbnQtPmNoYW5nZSUzPjApIHsKICAgICAgICAgICAgICAgIElTKGN1cnJlbnQpOwogICAgICAgIH0KICAgICAgICBpZihsb3dlcj09Y3VycmVudC0+bGVmdF9yJiZoaWdoZXI9PWN1cnJlbnQtPnJpZ2h0X3IpIHsKICAgICAgICAgICAgICAgIElTKGN1cnJlbnQpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBpZihsb3dlcj5taWQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgdXBkYXRlUk1RKGxvd2VyLGhpZ2hlcixjdXJyZW50LT5yaWdodCk7CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYoaGlnaGVyPD1taWQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgdXBkYXRlUk1RKGxvd2VyLGhpZ2hlcixjdXJyZW50LT5sZWZ0KTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHVwZGF0ZVJNUShsb3dlcixtaWQsY3VycmVudC0+bGVmdCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHVwZGF0ZVJNUShtaWQrMSxoaWdoZXIsY3VycmVudC0+cmlnaHQpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZ2V0KGN1cnJlbnQsY3VycmVudC0+bGVmdCxjdXJyZW50LT5yaWdodCk7CiAgICAgICAgfQp9CmludCBjb3VudFJNUShpbnQgbG93ZXIsaW50IGhpZ2hlcixub2RlICpjdXJyZW50KQp7CiAgICAgICAgaW50IG1pZD0oY3VycmVudC0+bGVmdF9yK2N1cnJlbnQtPnJpZ2h0X3IpLzI7CiAgICAgICAgaWYoY3VycmVudC0+Y2hhbmdlJTM+MCkgewogICAgICAgICAgICAgICAgSVMoY3VycmVudCk7CiAgICAgICAgfQogICAgICAgIGlmKGN1cnJlbnQtPmNoYW5nZSUzPjApIHsKICAgICAgICAgICAgICAgIElTKGN1cnJlbnQpOwogICAgICAgIH0KICAgICAgICBpZihsb3dlcj09Y3VycmVudC0+bGVmdF9yJiZoaWdoZXI9PWN1cnJlbnQtPnJpZ2h0X3IpIHsKICAgICAgICAgICAgICAgIGlmKGN1cnJlbnQtPmNoYW5nZSUzPjApIHsKICAgICAgICAgICAgICAgICAgICAgICAgSVMoY3VycmVudCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZihjdXJyZW50LT5jaGFuZ2UlMz4wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIElTKGN1cnJlbnQpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgLy9jb3V0PDxjdXJyZW50LT5jb3VudF8wPDxlbmRsOwogICAgICAgICAgICAgICAgcmV0dXJuIGN1cnJlbnQtPmNvdW50XzA7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGlmKGxvd2VyPm1pZCkgewogICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gY291bnRSTVEobG93ZXIsaGlnaGVyLGN1cnJlbnQtPnJpZ2h0KTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZihoaWdoZXI8PW1pZCkgewogICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gY291bnRSTVEobG93ZXIsaGlnaGVyLGN1cnJlbnQtPmxlZnQpOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGNvdW50Uk1RKGxvd2VyLG1pZCxjdXJyZW50LT5sZWZ0KStjb3VudFJNUShtaWQrMSxoaWdoZXIsY3VycmVudC0+cmlnaHQpOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KfQppbnQgbWFpbigpCnsKICAgICAgICBpbnQgTixROwogICAgICAgIC8vY2luPj5OPj5ROwogICAgICAgIHNjYW5mKCIlZCVkIiwmTiwmUSk7CiAgICAgICAgbm9kZSAqdG9wPW5ldyBub2RlKCk7CiAgICAgICAgbWFrZXRyZWUoMCxOLTEsdG9wKTsKICAgICAgICB3aGlsZShRLS0pIHsKICAgICAgICAgICAgICAgIGludCBxdWVyeSxsb3dlcixoaWdoZXI7CiAgICAgICAgICAgICAgICBzY2FuZigiJWQlZCVkIiwmcXVlcnksJmxvd2VyLCZoaWdoZXIpOwogICAgICAgICAgICAgICAgLy9jaW4+PnF1ZXJ5Pj5sb3dlcj4+aGlnaGVyOwogICAgICAgICAgICAgICAgaWYocXVlcnkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixjb3VudFJNUShsb3dlcixoaWdoZXIsdG9wKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vY291dDw8Y291bnRSTVEobG93ZXIsaGlnaGVyLHRvcCk8PGVuZGw7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vY291dDw8dG9wLT5sZWZ0LT5jb3VudF8wPDwiXHQiPDx0b3AtPmxlZnQtPmNvdW50XzE8PCJcdCI8PHRvcC0+bGVmdC0+Y291bnRfMjw8Ilx0Ijw8dG9wLT5sZWZ0LT5jaGFuZ2U8PGVuZGw7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vY291dDw8dG9wLT5yaWdodC0+Y291bnRfMDw8Ilx0Ijw8dG9wLT5yaWdodC0+Y291bnRfMTw8Ilx0Ijw8dG9wLT5yaWdodC0+Y291bnRfMjw8Ilx0Ijw8dG9wLT5yaWdodC0+Y2hhbmdlPDxlbmRsOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgdXBkYXRlUk1RKGxvd2VyLGhpZ2hlcix0b3ApOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gMDsKfQ==