https://www.acmicpc.net/problem/10942
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static Reader scanner = new Reader();
static int N, M, S, E;
static int[] series;
static void input() {
N = scanner.nextInt();
series = new int[N];
for(int index = 0; index < N; index++) series[index] = scanner.nextInt();
M = scanner.nextInt();
for(int question = 0; question < M; question++) {
S = scanner.nextInt();
E = scanner.nextInt();
isPalindrome();
}
}
static void isPalindrome() {
if(S == E) {
sb.append(1).append('\n');
return;
}
for(int start = S - 1, end = E - 1; start < end; start++, end--) {
if(series[start] != series[end]) {
sb.append(0).append('\n');
return;
}
}
sb.append(1).append('\n');
return;
}
public static void main(String[] args) {
input();
System.out.println(sb);
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static Reader scanner = new Reader();
static int N, M, S, E;
static boolean[][] palindrome;
static int[] series;
static void input() {
N = scanner.nextInt();
series = new int[N + 1];
for(int index = 1; index <= N; index++) series[index] = scanner.nextInt();
findAllPalindrome();
M = scanner.nextInt();
for(int question = 0; question < M; question++) {
S = scanner.nextInt();
E = scanner.nextInt();
if(palindrome[S][E]) sb.append(1).append('\n');
else sb.append(0).append('\n');
}
}
static void findAllPalindrome() {
palindrome = new boolean[N + 1][N + 1];
for(int start = 1; start <= N; start++) palindrome[start][start] = true;
for(int start = 1; start < N; start++) {
if(series[start] == series[start + 1]) palindrome[start][start + 1] = true;
}
for(int len = 3; len <= N; len++) {
for(int start = 1; start + len - 1 <= N; start++) {
if(series[start] == series[start + len - 1] && palindrome[start + 1][start + len - 2])
palindrome[start][start + len - 1] = true;
}
}
}
public static void main(String[] args) {
input();
System.out.println(sb);
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}