#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define ll long long int
#define MAX (100000+10) //////////////// change, added brackets :)
#define ull unsigned long long int
#define gcd(a,b) __gcd(a,b)
using namespace std;
struct info
{
int zero,one,two,lazy;
} tree[MAX*5];
void update_lazy(int node)
{
int temp = tree[node].zero;
tree[node].zero = tree[node].two;
tree[node].two = tree[node].one;
tree[node].one = temp;
}
void update_node(int node,int left)
{
tree[left].lazy += tree[node].lazy;
tree[left+1].lazy += tree[node].lazy;
tree[node].lazy%=3;
int shift = tree[node].lazy;
while(shift--)
{
update_lazy(left);
update_lazy(left+1);
}
tree[node].lazy=0;
}
void marge(int node, int left, int right)
{
tree[node].zero= tree[left].zero + tree[right].zero;
tree[node].one= tree[left].one + tree[right].one;
tree[node].two= tree[left].two + tree[right].two;
}
void build(int node, int b, int e)
{
if (b == e)
{
tree[node].one = 0;
tree[node].two = 0;
tree[node].lazy = 0;
tree[node].zero = 1;
return;
}
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
build(Left, b, mid);
build(Right, mid + 1, e);
marge(node,Left,Right);
}
void update(int node, int l, int r, int i, int j)
{
//cout<<node<<" "<<l<<" "<<r<<" "<<i<<" "<<j<<endl;
int Left = node*2;
if(tree[node].lazy!=0 && l != r){ //////////////// change, added condition l != r
update_node(node,Left);
}
//no overlap
if(l>r || l>j || r<i) return;
//total overlap
if(l>=i && r<=j){
tree[node].lazy++;
update_lazy(node);
return;
}
//partial overlap
int mid = (l+r)/2;
update(Left,l,mid,i,j);
update(Left+1,mid+1,r,i,j);
marge(node,Left,Left+1);
}
int query(int node,int l,int r,int i,int j) ///////////// change, some optimizations
{
if(l>r || l>j || r<i) return 0;
if(tree[node].lazy!=0 && l != r){ //////////////// change, added condition l != r
update_node(node,node*2);
}
if(l>=i && r<=j) return tree[node].zero;
int mid = (l+r)/2;
int Left = node*2;
if( j <= mid )
return query(Left,l,mid,i,j);
if(mid < i)
return query(Left+1,mid+1,r,i,j);
return query(Left,l,mid,i,j) + query(Left+1,mid+1,r,i,j);
}
int main()
{
int t,no=0;
//scanf("%d",&t);
//while(t--)
{
int n,q,a;
scanf("%d %d",&n,&q);
memset(tree,0,sizeof(tree));
build(1,1,n);
//printf("Case %d:\n",++no);
while(q--)
{
int type,l,r;
scanf("%d %d %d",&type,&l,&r);
l++;
r++;
if(type == 0)
{
update(1,1,n,l,r);
}
else
{
printf("%d\n",query(1,1,n,l,r));
}
}
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDxzdGRib29sLmg+CiNpbmNsdWRlIDxtYXRoLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nIGludAojZGVmaW5lIE1BWCAoMTAwMDAwKzEwKSAgICAgLy8vLy8vLy8vLy8vLy8vLyBjaGFuZ2UsIGFkZGVkIGJyYWNrZXRzIDopCiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZyBpbnQKI2RlZmluZSBnY2QoYSxiKSAgICBfX2djZChhLGIpCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IGluZm8KewogICAgaW50IHplcm8sb25lLHR3byxsYXp5Owp9IHRyZWVbTUFYKjVdOwoKdm9pZCB1cGRhdGVfbGF6eShpbnQgbm9kZSkKewogICAgaW50IHRlbXAgPSB0cmVlW25vZGVdLnplcm87CiAgICB0cmVlW25vZGVdLnplcm8gPSB0cmVlW25vZGVdLnR3bzsKICAgIHRyZWVbbm9kZV0udHdvID0gdHJlZVtub2RlXS5vbmU7CiAgICB0cmVlW25vZGVdLm9uZSA9IHRlbXA7Cn0KCnZvaWQgdXBkYXRlX25vZGUoaW50IG5vZGUsaW50IGxlZnQpCnsKICAgIHRyZWVbbGVmdF0ubGF6eSArPSAgdHJlZVtub2RlXS5sYXp5Owog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pZCA9IChsK3IpLzI7CiAgICBpbnQgTGVmdCA9IG5vZGUqMjsKICAgIAogICAgaWYoIGogPD0gbWlkICkKICAgIAlyZXR1cm4gcXVlcnkoTGVmdCxsLG1pZCxpLGopOwoJaWYobWlkIDwgaSkKCQlyZXR1cm4gcXVlcnkoTGVmdCsxLG1pZCsxLHIsaSxqKTsKICAgIAogICAgcmV0dXJuIHF1ZXJ5KExlZnQsbCxtaWQsaSxqKSArIHF1ZXJ5KExlZnQrMSxtaWQrMSxyLGksaik7Cn0KCmludCBtYWluKCkKewogICAgaW50IHQsbm89MDsKICAgIC8vc2NhbmYoIiVkIiwmdCk7CgogICAgLy93aGlsZSh0LS0pCiAgICB7CiAgICAgICAgaW50IG4scSxhOwogICAgICAgIHNjYW5mKCIlZCAlZCIsJm4sJnEpOwoKICAgICAgICBtZW1zZXQodHJlZSwwLHNpemVvZih0cmVlKSk7CiAgICAgICAgYnVpbGQoMSwxLG4pOwoKICAgICAgICAvL3ByaW50ZigiQ2FzZSAlZDpcbiIsKytubyk7CiAgICAgICAgd2hpbGUocS0tKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHR5cGUsbCxyOwogICAgICAgICAgICBzY2FuZigiJWQgJWQgJWQiLCZ0eXBlLCZsLCZyKTsKICAgICAgICAgICAgbCsrOwogICAgICAgICAgICByKys7CgogICAgICAgICAgICBpZih0eXBlID09IDApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHVwZGF0ZSgxLDEsbixsLHIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixxdWVyeSgxLDEsbixsLHIpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gMDsKfQo=