//============================================================================
// Name : SPOJ Multiple by 3 (Segment Tree)
// Author : Tarango Khan
// Copyright : Team Byteheads
// Description : !!!Hello World!!!
//============================================================================
#include <bits/stdc++.h>
using namespace std;
#define Size 100005
struct vert{
int cnt[3];
int prop;
vert(){
cnt[0] = cnt[1] = cnt[2] = prop = 0;
}
};
vert tree[Size*4];
void build_tree(int cur,int Start,int End){
if(Start == End){
tree[cur].cnt[0] = 1;
tree[cur].cnt[1] = 0;
tree[cur].cnt[2] = 0;
tree[cur].prop = 0;
return;
}
int left = cur*2;
int right = left+1;
int mid = (Start+End)/2;
build_tree(left,Start,mid);
build_tree(right,mid+1,End);
tree[cur].cnt[0] = tree[left].cnt[0] + tree[right].cnt[0];
tree[cur].cnt[1] = tree[left].cnt[1] + tree[right].cnt[1];
tree[cur].cnt[2] = tree[left].cnt[2] + tree[right].cnt[2];
tree[cur].prop = tree[left].prop + tree[right].prop;
}
void update_prop(int cur){
int tmp = tree[cur].cnt[2];
tree[cur].cnt[2] = tree[cur].cnt[1];
tree[cur].cnt[1] = tree[cur].cnt[0];
tree[cur].cnt[0] = tmp;
}
void update_tree(int cur,int Start,int End,int u,int v){
if(End<u || Start>v) return;
if(Start>=u && End<=v){
update_prop(cur);
tree[cur].prop += 1;
return;
}
int left = cur*2;
int right = left+1;
int mid = (Start+End)/2;
update_tree(left,Start,mid,u,v);
update_tree(right,mid+1,End,u,v);
tree[cur].cnt[0] = tree[left].cnt[0] + tree[right].cnt[0];
tree[cur].cnt[1] = tree[left].cnt[1] + tree[right].cnt[1];
tree[cur].cnt[2] = tree[left].cnt[2] + tree[right].cnt[2];
}
vert tree_query(int cur,int Start,int End,int u,int v,int carry){
vert newV;
if(End<u || Start>v){
newV.prop = -1;
return newV;
}
if(Start>=u && End<=v){
newV.cnt[0] = tree[cur].cnt[0];
newV.cnt[1] = tree[cur].cnt[1];
newV.cnt[2] = tree[cur].cnt[2];
newV.prop = tree[cur].prop;
for(int s = 0;s<carry;s++){
int tmp = newV.cnt[2];
newV.cnt[2] = newV.cnt[1];
newV.cnt[1] = newV.cnt[0];
newV.cnt[0] = tmp;
}
return newV;
}
int left = cur*2;
int right = left+1;
int mid = (Start+End)/2;
vert left_ret = tree_query(left,Start,mid,u,v,carry + tree[cur].prop);
vert right_ret = tree_query(right,mid+1,End,u,v,carry + tree[cur].prop);
if(left_ret.prop == -1) return right_ret;
if(right_ret.prop == -1) return left_ret;
newV.cnt[0] = left_ret.cnt[0] + right_ret.cnt[0];
newV.cnt[1] = left_ret.cnt[1] + right_ret.cnt[1];
newV.cnt[2] = left_ret.cnt[2] + right_ret.cnt[2];
return newV;
}
int main() {
int N,Q,u,v,type;
scanf("%d %d",&N,&Q);
build_tree(1,1,N);
for(int i = 1;i<=Q;i++){
scanf("%d",&type);
if(type == 0){
scanf("%d %d",&u,&v);
u++;v++;
update_tree(1,1,N,u,v);
}else{
scanf("%d %d",&u,&v);
u++;v++;
vert sum = tree_query(1,1,N,u,v,0);
printf("%d\n",sum.cnt[0]);
}
}
}
Ly89PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci8vIE5hbWUgICAgICAgIDogU1BPSiBNdWx0aXBsZSBieSAzIChTZWdtZW50IFRyZWUpCi8vIEF1dGhvciAgICAgIDogVGFyYW5nbyBLaGFuCi8vIENvcHlyaWdodCAgIDogVGVhbSBCeXRlaGVhZHMKLy8gRGVzY3JpcHRpb24gOiAhISFIZWxsbyBXb3JsZCEhIQovLz09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0KCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIFNpemUgMTAwMDA1CgpzdHJ1Y3QgdmVydHsKICAgIGludCBjbnRbM107CiAgICBpbnQgcHJvcDsKICAgIHZlcnQoKXsKICAg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