Language: C++


//ある区間が条件を満たすならば、
//それを包含する上位区間も条件を満たす問題を解くクラス
template <typename T>
class abstract_inchworm_superset
{
private:
    virtual void move_left_pointer() = 0;//lを右に動かす処理
    virtual void move_right_pointer() = 0;//rを右に動かす処理
    virtual bool is_cond() = 0; //条件成立か否かを返す
    virtual bool is_right_end() = 0;//rが右端に到達したかを返す
    virtual std::pair<T, T> no_next() = 0;//次の要素がない場合に終了値を返す
    virtual std::pair<T, T> get_LR() = 0;//lとrを返す
public:
    virtual std::pair<T, T> next(); //条件を満たすデータを返し、ポインタを次に進める
};

template <typename T>
std::pair<T, T> abstract_inchworm_superset<T>::next(){
    while (is_cond() == false && is_right_end() == false) {
            move_right_pointer();
    }
    if (is_cond() == false)
        return no_next();
    std::pair<T, T> ret = get_LR();
    move_left_pointer();
    return ret;
}

//ある区間が条件を満たすならば、
//それが包含する部分区間も条件を満たす問題を解くクラス
template <typename T>
class abstract_inchworm_subset
{
private:
    virtual void move_left_pointer() = 0;//lを右に動かす処理
    virtual void move_right_pointer() = 0;//rを右に動かす処理
    virtual bool is_cond() = 0; //条件成立か否かを返す
    virtual bool is_right_end() = 0;//rが右端に到達したかを返す
    virtual std::pair<T, T> no_next() = 0;//次の要素がない場合に終了値を返す
    virtual std::pair<T, T> get_LR() = 0;//lとrを返す
    virtual bool is_left_eq_right() = 0;
public:
    virtual std::pair<T, T> next(); //条件を満たすデータを返し、ポインタを次に進める
};

template <typename T>
std::pair<T, T> abstract_inchworm_subset<T>::next() {
    std::pair<T, T> ret;
    while (is_right_end() == false && is_cond() == true) {
        move_right_pointer();
    }
    ret = get_LR();
    if (is_left_eq_right() == true) {
        return no_next();
    }
    
    move_left_pointer();
    return ret;
}

#include <vector>
#include <stdio.h>
class xor_sum :
    public abstract_inchworm_subset<int>
{
private:
    int Left, Right;
    
    int N;
    long long LR_XOR;
    long long LRsum;
    std::vector<long long> sequence;
protected:
    virtual void move_left_pointer();
    virtual void move_right_pointer();
    virtual bool is_cond(); //条件成立を返す
    virtual bool is_right_end();
    virtual std::pair<int, int> no_next();
    virtual std::pair<int, int> get_LR();
    virtual bool is_left_eq_right();
public:

    xor_sum(std::vector<long long>& sequence, int N, int start);
};

xor_sum::xor_sum(std::vector<long long>& sequence, int N, int start) :
    sequence(sequence),
    N(N),
    Left(start),
    Right(start),
    LR_XOR(0),
    LRsum(0)
{
}


void xor_sum::move_left_pointer() {
    LRsum -= sequence[Left];
    LR_XOR ^= sequence[Left];
    Left++;

}

void xor_sum::move_right_pointer() {
    if (Right < N) {
        LRsum += sequence[Right];
        LR_XOR ^= sequence[Right];
        Right++;
    }
}

bool xor_sum::is_cond() {
    if (Left >= N) 
        return false;
    if ((LRsum + sequence[Right]) == (LR_XOR ^ sequence[Right]))
        return true;
    return false;
}

bool xor_sum::is_right_end() {
    if (Right >= N)
        return true;
    return false;
}

std::pair<int, int> xor_sum::no_next() {
    return std::make_pair(-1, -1);
}

std::pair<int, int> xor_sum::get_LR() {
    return std::make_pair(Left, Right);
}

bool xor_sum::is_left_eq_right() {
    return (Left == Right);
}


int main()
{
    int N;
    std::vector<long long> A;
    scanf("%d", &N);

    for (int i = 0; i < N; i++) {
        int a;
        scanf("%d", &a);
        A.push_back(a);
    }
    xor_sum xs(A, N, 0);
    int count = 0;
    while (1) {
        std::pair<int, int> LR = xs.next();
        if (LR.first == -1) break;
        count += LR.second - LR.first ;
    }
    printf("%d\n", count);
    return 0;
}

Compiler output:
a.cc:12:18: error: 'pair' in namespace 'std' does not name a template type
   12 |     virtual std::pair<T, T> no_next() = 0;//次の要素がない場合に終了値を返す
      |                  ^~~~
a.cc:1:1: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
  +++ |+#include <utility>
    1 | 
a.cc:13:18: error: 'pair' in namespace 'std' does not name a template type
   13 |     virtual std::pair<T, T> get_LR() = 0;//lとrを返す
      |                  ^~~~
a.cc:13:13: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   13 |     virtual std::pair<T, T> get_LR() = 0;//lとrを返す
      |             ^~~
a.cc:15:18: error: 'pair' in namespace 'std' does not name a template type
   15 |     virtual std::pair<T, T> next(); //条件を満たすデータを返し、ポインタを次に進める
      |                  ^~~~
a.cc:15:13: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   15 |     virtual std::pair<T, T> next(); //条件を満たすデータを返し、ポインタを次に進める
      |             ^~~
a.cc:19:6: error: 'pair' in namespace 'std' does not name a template type
   19 | std::pair<T, T> abstract_inchworm_superset<T>::next(){
      |      ^~~~
a.cc:19:1: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   19 | std::pair<T, T> abstract_inchworm_superset<T>::next(){
      | ^~~
a.cc:40:18: error: 'pair' in namespace 'std' does not name a template type
   40 |     virtual std::pair<T, T> no_next() = 0;//次の要素がない場合に終了値を返す
      |                  ^~~~
a.cc:40:13: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   40 |     virtual std::pair<T, T> no_next() = 0;//次の要素がない場合に終了値を返す
      |             ^~~
a.cc:41:18: error: 'pair' in namespace 'std' does not name a template type
   41 |     virtual std::pair<T, T> get_LR() = 0;//lとrを返す
      |                  ^~~~
a.cc:41:13: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   41 |     virtual std::pair<T, T> get_LR() = 0;//lとrを返す
      |             ^~~
a.cc:44:18: error: 'pair' in namespace 'std' does not name a template type
   44 |     virtual std::pair<T, T> next(); //条件を満たすデータを返し、ポインタを次に進める
      |                  ^~~~
a.cc:44:13: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   44 |     virtual std::pair<T, T> next(); //条件を満たすデータを返し、ポインタを次に進める
      |             ^~~
a.cc:48:6: error: 'pair' in namespace 'std' does not name a template type
   48 | std::pair<T, T> abstract_inchworm_subset<T>::next() {
      |      ^~~~
a.cc:48:1: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
   48 | std::pair<T, T> abstract_inchworm_subset<T>::next() {
      | ^~~
a.cc: In function 'int main()':
a.cc:154:37: error: 'class xor_sum' has no member named 'next'
  154 |         std::pair<int, int> LR = xs.next();
      |                                     ^~~~