﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab6
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(ConsoleReadLine());
            double logn = Math.Log(n, 2);
            int lognceil = (int)Math.Ceiling(logn);

            var buckets = new LinkedList<Tuple<int, int>>();
            int time = 0;

            while (true)
            {
                string line = ConsoleReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    return;
                }

                if (line[0] == 'q')
                {
                    int query = int.Parse(line.Split(' ')[1]);
                    //int time0 = kbuckets.First().Item2;

                    //int queryAnswer = GetQueryAnswer(query, stream);
                    int sum = 0;

                    foreach (var b in buckets)
                    {
                        int tmq = time - query;
                        if (b.Item2 >= tmq)
                        {
                            sum += b.Item1;
                        }
                        else
                        {
                            sum += b.Item1 / 2;
                            break;
                        }
                    }

                    ConsoleWriteLine(sum);
                }
                else
                {

                    foreach (char c in line)
                    {
                        time++;

                        // Remove oldest bucket if timed out
                        if (buckets.Count > 1)
                        {
                            var lastBucket = buckets.Last;
                            if (time - lastBucket.Previous.Value.Item2 > n)
                            {
                                buckets.RemoveLast();
                            }
                        }

                        if (c == '1')
                        {
                            // Add 1 to bucket

                            var first = buckets.AddFirst(Tuple.Create(1, time));
                            LinkedListNode<Tuple<int, int>> current = first;

                            do
                            {
                                if (current.Next != null && current.Next.Next != null && current.Next.Next.Value.Item1 == current.Value.Item1)
                                {
                                    var newBucket = Tuple.Create(current.Value.Item1 * 2, current.Next.Next.Value.Item2);
                                    buckets.Remove(current.Next.Next);
                                    buckets.Remove(current.Next);

                                    buckets.AddAfter(current, newBucket);
                                }

                                current = current.Next;

                            } while (current != null && current != first);
                        }
                    }
                }
            }

            Console.ReadLine();
        }
        
        static List<string> lines2 = new List<string>();
        static int linesI2 = 0;

        private static void ConsoleWriteLine(int sum)
        {
            Console.WriteLine(sum);
            
            ////return Console.ReadLine();
            //if (lines2.Count == 0)
            //{
            //    lines2 = Resource1.tout.Split(new[] { "\n" }, StringSplitOptions.None).ToList();
            //    linesI2 = 0;
            //}

            //var res = lines2[linesI2++];

            //Debug.Assert(sum.ToString() == res);
        }

        static List<string> lines = new List<string>();
        static int linesI = 0;

        private static string ConsoleReadLine()
        {
            //return Console.ReadLine();
            if (lines.Count == 0)
            {
                lines = Resource1.tin.Split(new[] { "\n" }, StringSplitOptions.None).ToList();
                linesI = 0;
            }

            return lines[linesI++];
        }
    }
}
