#include //#include #include using namespace std; using namespace atcoder; //g++ hoge.cpp (-std=c++17) -I . で実行 #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") typedef long long ll; typedef pair prl; typedef vector vcl; typedef map mapl; typedef unordered_map umap; #define pb push_back #define all(v) v.begin(), v.end() #define rep(i,a,b) for(ll i=a;i<=b;i++) #define repi(i,a,b) for(int i=a;i<=b;i++) #define repr(i,a,b) for(ll i=a;i>=b;i--) #define reps(i,v) for(ll i=0;i void chmin(T &a, const T &b) { a = min(a, b); } template void chmax(T &a, const T &b) { a = max(a, b); } ll myceil(ll a, ll b) { return (a+b-1) / b; } //const ll mod = 1e9+7; //const ll mod = 998244353; //typedef modint1000000007 mint; //typedef modint998244353 mint; //typedef modint mint //cout << sum.val() << endl; //mint::set_mod(mod); modが固定でないとき ll n,q,c[50000],s[50000],a,b,d,x,js[100005]; vector> vs[10]; int main() { // your code goes here cin >> n >> q; rep(i,1,q){ cin >> c[i] >> s[i]; } if(n<=10){ rep(i,1,q){ rep(j,1,n){ if(c[i]==1 && j>=s[i]) js[j] += (j-s[i]+1)*(j-s[i]+1); if(c[i]==2 && j<=s[i]) js[j] += (s[i]-j+1)*(s[i]-j+1); } } //rep(i,1,n) print(js[i]); rep(i,1,n) x += js[i]; print(x); rep(i,1,n){ rep(j,1,js[i]) print2(1,i); } return 0; } rep(i,1,q){ if(c[i]==1){ if(s[i]==1) a += 1; if(s[i]==1) b += 3; if(s[i]==2) b += 1; if(s[i]<=2) d += 2; if(s[i]==3) d += 1; if(s[i]>=4){ x++; vs[1].pb({1,{s[i],0}}); } if(s[i]>=3 && s[i]<=n-1){ x++; vs[1].pb({1,{s[i]+1,0}}); } } else { a += s[i]*s[i]; b += (3 - 2 * s[i]); if(s[i]>=2) d += 2; if(s[i]>=2&&s[i]<=n-2){ x++; vs[1].pb({2,{s[i]+2,0}}); } if(s[i]>=1&&s[i]<=n-3){ x++; vs[1].pb({2,{s[i]+3,0}}); } } } while(a>0){ x++; if(a%2==0){ vs[2].pb({3,{1,1}}); a /= 2; } else { vs[2].pb({1,{1,0}}); a -= 1; } } while(b>0){ x++; if(b%2==0){ vs[3].pb({3,{2,2}}); b /= 2; } else { vs[3].pb({1,{2,0}}); b -= 1; } } while(d>0){ x++; if(d%2==0){ vs[4].pb({3,{3,3}}); d /= 2; } else { vs[4].pb({1,{3,0}}); d -= 1; } } print(x+(n-3)+(n-2)+(n-1)); repr(i,4,1){ if(i>=2) reverse(all(vs[i])); for(auto to: vs[i]){ if(to.first<=2){ print2(to.first,to.second.first); } else { print3(to.first,to.second.first,to.second.second); } } } repr(i,3,1){ rep(j,i+1,n){ print3(3,j,j-1); } } return 0; }