malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include<bits/stdc++.h>   #define ls i<<1   #define rs i<<1|1   #define fi first   #define se second   #define pb push_back   #define mp make_pair   using namespace std;   typedef long long ll;   typedef vector<int> vct;   typedef pair<int,int> pii;   const int N=2E5+1;   const int inf=1<<30;   ll read()   {       ll a=0;       char b=1,c;       do if((c=getchar())==45)b=-1;while(c<48||c>57);       do a=(a<<3)+(a<<1)+(c&15);while((c=getchar())>47&&c<58);       return a*b;   }   void write(ll x,char c)   {       if(x<0)putchar(45),x=-x;       char a[20],s=0;       do a[++s]=x%10|48;while(x/=10);       do putchar(a[s]);while(--s);       putchar(c);   }   const int p=1E9+7;   int n,l,r;   int fac[N],inv[N];   vct a,ans;   int solve(int u,int v)   {       for(int i=0;i<n;++i)       {           int y=u-max(l+a[i]-1,0),z=v-max(n-r+a[i],0);           if(y<0||z<0)ans[i]=0;           else ans[i]=(fac[y+z]*1LL*inv[y]%p*inv[z]%p);       }       int res=0;       for(int i=0;i<n;++i)res=(res+(ans[i]-ans[i+1]+p)*1LL*a[i])%p;       return res;   }   int main()   {       fac[0]=fac[1]=inv[0]=inv[1]=1;       for(int i=2;i<N;++i)inv[i]=(p-p/i)*1LL*inv[p%i]%p;       for(int i=2;i<N;++i)       {           fac[i]=fac[i-1]*1LL*i%p;           inv[i]=inv[i-1]*1LL*inv[i]%p;       }       for(int x=read();x--;)       {           n=read(),l=read(),r=read();           a.resize(n);           ans.resize(n);           ans.pb(0);           iota(a.begin(),a.end(),min(1-l,r-n));           int m=n>>1;           if(n&1)write((solve(m,n-m)+solve(n-m,m))%p,  n );           else write(solve(m,m),  n );       }   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> mp;   int main() {     int i, j, n, m, t;     string s;     cin >> t;     int cnt = 0;     for (i = 0; i < t; i++) {       cin >> s;       int len = s.size();       string ans;       int chk = 1;       while (chk == 1) {         ans.clear();         chk = 0;         len = s.size();         for (j = 0; j < len - 1; j++) {           if (s[j] ==  o  && s[j + 1] ==  o ) {             ans.push_back( u );             j++;             chk = 1;           } else if (s[j] ==  k  && s[j + 1] ==  h ) {             ans.push_back( h );             j++;             chk = 1;           } else if (s[j] ==  u  && s[j + 1] ==  o ) {             s[j] =  o ;             s[j + 1] =  u ;             chk = 1;             ans.push_back( o );           } else             ans.push_back(s[j]);         }         if (j == len - 1) ans.push_back(s[j]);         s = ans;       }       if (mp[ans] == 0) {         cnt++;       }       mp[ans]++;     }     cout << cnt << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long int ans = 0;   vector<int> vx[200005];   bool vu[200005];   int n, k;   int dfs(int v, int p) {     int aa = 0, aa2 = 0;     for (int i = 0; i < vx[v].size(); i++) {       if (vx[v][i] == p) continue;       int pr = dfs(vx[v][i], v);       aa += pr;       aa2 += min(pr, 2 * k - pr);     }     ans += aa2;     if (vu[v]) aa++;     return aa;   }   int main() {     for (int i = 0; i < 200005; i++) vu[i] = 0;     cin >> n >> k;     int u, v;     for (int i = 0; i < k; i++) {       cin >> u >> v;       vu[u] = vu[v] = 1;     }     for (int i = 1; i < n; i++) {       cin >> u >> v;       vx[u].push_back(v);       vx[v].push_back(u);     }     dfs(1, -1);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Point {     double x, y;     Point() {}     Point(double _x, double _y) {       x = _x;       y = _y;     }     Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }     double operator^(const Point &b) const { return x * b.y - y * b.x; }     double operator*(const Point &b) const { return x * b.x + y * b.y; }     void transXY(double B) {       double tx = x, ty = y;       x = tx * cos(B) - ty * sin(B);       y = tx * sin(B) + ty * cos(B);     }   };   struct Line {     Point s, e;     Line() {}     Line(Point _s, Point _e) {       s = _s;       e = _e;     }   };   Point PointToLine(Point P, Line L) {     Point result;     double t = ((P - L.s) * (L.e - L.s)) / ((L.e - L.s) * (L.e - L.s));     result.x = L.s.x + (L.e.x - L.s.x) * t;     result.y = L.s.y + (L.e.y - L.s.y) * t;     return result;   }   Point ver[1010];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> ver[i].x >> ver[i].y;     ver[0] = ver[n];     ver[n + 1] = ver[1];     double ans = 1e12;     for (int i = 1; i <= n; ++i) {       Line l;       l.s = ver[i - 1];       l.e = ver[i + 1];       Point p = PointToLine(ver[i], l);       double len = sqrt((ver[i].x - p.x) * (ver[i].x - p.x) +                         (ver[i].y - p.y) * (ver[i].y - p.y));       ans = min(ans, len / 2);     }     printf( %.010lf n , ans);   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int a, b;     node() { a = b = 0; }   } node[10005];   int main() {     int n, v;     cin >> n >> v;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       node[a].a += b;     }     int ans = 0;     for (int i = 1; i <= 3001; i++) {       int vv = v;       if (vv < node[i].b) {         ans += vv;         node[i + 1].b += node[i].a;       } else {         ans += node[i].b;         vv -= node[i].b;         if (vv >= node[i].a) {           ans += node[i].a;         } else {           ans += vv;           node[i + 1].b += node[i].a - vv;         }       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 100;   const long long inf = (1LL << 30 - 1);   pair<int, int> A[maxn];   int fa[maxn], X[maxn], Y[maxn], Hx[maxn], Hy[maxn], ans[maxn];   int FindSet(int x) { return fa[x] == x ? x : fa[x] = FindSet(fa[x]); }   void UnionSet(int u, int v) {     u = FindSet(u);     v = FindSet(v);     if (u != v) fa[u] = v;   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n * m; i++) {       fa[i] = i;       scanf( %d , &A[i].first);       A[i].second = i;     }     sort(A, A + n * m);     int j = -1;     memset(X, 0, sizeof(X));     memset(Y, 0, sizeof(Y));     for (int i = 0; i < n * m; i++) {       if (i != n * m - 1 && A[i].first == A[i + 1].first) continue;       for (int k = j + 1; k <= i; k++) {         int p = A[k].second;         int x = A[k].second / m, y = A[k].second % m;         Hx[x] = p;         Hy[y] = p;       }       for (int k = j + 1; k <= i; k++) {         int p = A[k].second;         int x = A[k].second / m, y = A[k].second % m;         UnionSet(Hx[x], p);         UnionSet(Hy[y], p);       }       for (int k = j + 1; k <= i; k++) {         int p = A[k].second;         int x = A[k].second / m, y = A[k].second % m;         int pa = FindSet(p);         ans[pa] = max(ans[pa], max(X[x], Y[y]) + 1);       }       for (int k = j + 1; k <= i; k++) {         int p = A[k].second;         int x = A[k].second / m, y = A[k].second % m;         int pa = FindSet(p);         X[x] = max(X[x], ans[pa]);         Y[y] = max(Y[y], ans[pa]);       }       j = i;     }     for (int i = 0; i < n * m; i++) {       printf( %d , ans[FindSet(i)]);       if ((i + 1) % m == 0)         printf(  n );       else         printf(   );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> num(n + 1);     for (int i = 1; i <= n; i++) cin >> num[i];     if (num[1] % 2 == 0 || n % 2 == 0 || num[n] % 2 == 0) {       cout <<  No ;       return 0;     }     cout <<  Yes ;     return 0;   }
#include <bits/stdc++.h>   using std::cin;   using std::cout;   const int N = 17;   char ch[N];   int n, r[N], b[N];   int sr, sb, red[1 << N], blue[1 << N], f[1 << N][123];   int main() {     std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> ch[i] >> r[i] >> b[i];       sr += r[i];       sb += b[i];     }     for (int i = 0; i < (1 << n); ++i) {       for (int j = 1; j <= n; ++j) {         if (i & (1 << (j - 1))) {           if (ch[j] ==  R )             ++red[i];           else             ++blue[i];         }       }     }     memset(f, -1, sizeof(f));     f[0][0] = 0;     for (int i = 0; i < (1 << n); ++i) {       for (int j = 0; j <= 121; ++j) {         if (f[i][j] == -1) continue;         for (int k = 1; k <= n; ++k) {           if (i & (1 << (k - 1))) continue;           int T = i | (1 << (k - 1));           f[T][j + std::min(r[k], red[i])] =               std::max(f[T][j + std::min(r[k], red[i])],                        f[i][j] + std::min(b[k], blue[i]));         }       }     }     int res = 1e9;     for (int i = 0; i <= 121; ++i) {       if (f[(1 << n) - 1][i] == -1) continue;       res = std::min(res, std::max(sr - i, sb - f[(1 << n) - 1][i]));     }     cout << res + n <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     map<string, int> table;     table.insert(pair<string, int>( void , 0));     while (n--) {       string cmd;       cin >> cmd;       if (cmd ==  typedef ) {         string from, to, raw;         cin >> from >> to;         int depth = 0, i = 0, j = from.size() - 1;         for (; i < j; ++i) {           if (from[i] !=  & ) {             break;           }           --depth;         }         for (; i < j; --j) {           if (from[j] !=  * ) {             break;           }           ++depth;         }         raw = from.substr(i, j - i + 1);         map<string, int>::iterator it = table.find(raw);         if (it == table.end()) {           table[to] = -1;         } else if (it->second < 0 || it->second + depth < 0) {           table[to] = -1;         } else {           table[to] = it->second + depth;         }       } else if (cmd ==  typeof ) {         string type, raw;         cin >> type;         int depth = 0, i = 0, j = type.size() - 1;         for (; i < j; ++i) {           if (type[i] !=  & ) {             break;           }           --depth;         }         for (; i < j; --j) {           if (type[j] !=  * ) {             break;           }           ++depth;         }         raw = type.substr(i, j - i + 1);         map<string, int>::iterator it = table.find(raw);         if (it == table.end()) {           cout <<  errtype  << endl;         } else if (it->second < 0 || it->second + depth < 0) {           cout <<  errtype  << endl;         } else {           cout <<  void  << string(it->second + depth,  * ) << endl;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long MAX = pow(2, 32) - 1;     int n;     cin >> n;     stack<int> fors;     long long val = 0;     long long num = 1;     int i = 0;     while (i != n) {       string str;       cin >> str;       if (str ==  add ) {         val += num;       } else if (str ==  for ) {         int j;         cin >> j;         num *= j;         fors.push(j);       } else {         num /= fors.top();         fors.pop();       }       if (val > MAX) {         cout <<  OVERFLOW!!!  << endl;         return 0;       }       if (num > MAX) {         int end = 1;         while (end != 0) {           string s;           cin >> s;           if (s ==  add ) {             cout <<  OVERFLOW!!!  << endl;             return 0;           } else if (s ==  for ) {             int k;             cin >> k;             end++;           } else {             end--;           }           i++;         }         num /= fors.top();         fors.pop();       }       i++;     }     cout << val << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2005;   const long long mod = 1e9 + 7;   const long long INF = 1e18 + 1;   long long goal, n, m;   long long umbrella[N], rain[N], cache[N][N];   pair<long long, long long> a[N], b[N];   long long dp(long long x, long long y) {     if (x == goal) return 0;     long long &ans = cache[x][y];     if (ans != -1) return ans;     ans = INF;     if (umbrella[x]) ans = dp(x, x);     if (rain[x] and y == goal + 1) return ans;     if (rain[x]) {       ans = min(ans, umbrella[y] + min(dp(x + 1, y), dp(x + 1, goal + 1)));     } else       ans = min({ans, dp(x + 1, goal + 1),                  umbrella[y] + min(dp(x + 1, y), dp(x + 1, goal + 1))});     return ans;   }   void solve() {     memset(cache, -1, sizeof(cache));     cin >> goal >> n >> m;     for (long long i = 1; i < n + 1; i++) {       cin >> a[i].first >> a[i].second;       for (long long j = a[i].first; j < a[i].second; j++) rain[j] = 1;     }     for (long long i = 1; i < m + 1; i++) {       cin >> b[i].first >> b[i].second;       if (umbrella[b[i].first] == 0)         umbrella[b[i].first] = b[i].second;       else         umbrella[b[i].first] = min(b[i].second, umbrella[b[i].first]);     }     sort(a + 1, a + n + 1);     sort(b + 1, b + m + 1);     if (a[1].first < b[1].first) {       cout << -1;       return;     }     cout << dp(0, goal + 1);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, cnt;   vector<int> e[N];   vector<pair<int, int> > ans;   void init() {     scanf( %d , &n);     for (int i = 1, x, y; i < n; ++i) {       scanf( %d %d , &x, &y);       e[x].push_back(y);       e[y].push_back(x);     }   }   void dfs(int x, int father, int last_idx) {     int pre = last_idx;     for (int i = 0; i < e[x].size(); ++i)       if (e[x][i] != father) {         printf( 2 %d %d n , x, e[x][i]);         cnt++;         if (pre != -1) ans.push_back(make_pair(pre, cnt));         pre = cnt;         dfs(e[x][i], x, cnt);       }   }   void solve() {     printf( %d n , n - 1);     cnt = 0;     dfs(1, -1, -1);     for (int i = 0; i < ans.size(); ++i)       printf( %d %d n , ans[i].first, ans[i].second);   }   int main() {     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a, b, c;   long long x, y, z, n;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int main() {     cin >> n;     a = 1234567;     b = 123456;     c = 1234;     for (int i = 0; i * a <= n; i++) {       for (int j = 0; i * a + j * b <= n; j++) {         if ((n - i * a - j * b) % c == 0) {           cout <<  YES ;           return 0;         }       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   const int NUM = 200005;   const int MAXV = 1e6 + 100;   int blen, n, t, l, r, val;   int a[NUM], vcnt[MAXV];   long long int ans[NUM], answer;   struct query {     int l, r, ind;   } qs[NUM];   bool query_cmp(query a, query b) {     if (a.l / blen != b.l / blen) return a.l < b.l;     return a.r < b.r;   }   int main() {     int i;     std::scanf( %d%d , &n, &t);     blen = std::ceil(std::sqrt(n));     for (i = 0; i < n; ++i) std::scanf( %d , &a[i]);     for (i = 0; i < t; ++i) {       std::scanf( %d%d , &l, &r);       qs[i].l = l - 1;       qs[i].r = r;       qs[i].ind = i;     }     std::sort(qs, qs + t, query_cmp);     int ql, qr, cl, cr;     cl = 0;     cr = 0;     answer = 0;     for (i = 0; i < t; ++i) {       ql = qs[i].l;       qr = qs[i].r;       while (cl > ql) {         cl--;         val = a[cl];         answer -= (long long int)vcnt[val] * vcnt[val] * val;         vcnt[val]++;         answer += (long long int)vcnt[val] * vcnt[val] * val;       }       while (cl < ql) {         val = a[cl];         answer -= (long long int)vcnt[val] * vcnt[val] * val;         vcnt[val]--;         answer += (long long int)vcnt[val] * vcnt[val] * val;         cl++;       }       while (cr < qr) {         val = a[cr];         answer -= (long long int)vcnt[val] * vcnt[val] * val;         vcnt[val]++;         answer += (long long int)vcnt[val] * vcnt[val] * val;         cr++;       }       while (cr > qr) {         cr--;         val = a[cr];         answer -= (long long int)vcnt[val] * vcnt[val] * val;         vcnt[val]--;         answer += (long long int)vcnt[val] * vcnt[val] * val;       }       ans[qs[i].ind] = answer;     }     for (i = 0; i < t; ++i) printf( %I64d n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s, a;     a =  hello ;     cin >> s;     int i, j, k = 0;     for (i = 0, j = 0; j < a.size(); i++) {       if (i >= s.size()) break;       if (s[i] == a[j]) {         j++;       }     }     if (j == a.size())       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int k = 0;     cin >> s;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  a ) {         k++;       }     }     if (2 * k <= s.length()) {       cout << 2 * k - 1;     } else {       cout << s.length();     }   }
#include <bits/stdc++.h>   using namespace std;   void _fill_int(int* p, int val, int rep) {     int i;     for (i = 0; i < rep; i++) p[i] = val;   }   int GETi() {     int i;     scanf( %d , &i);     return i;   }   template <class T>   T sqr(T val) {     return val * val;   }   int W, H;   int TX[2][100001];   int TY[2][100001];   int CT = 0;   void solve() {     int f, r, i, j, k, l, x, y, cur, tar, dx, dy, sx, sy;     char st[4];     H = GETi();     W = GETi();     sy = GETi() - 1;     sx = GETi() - 1;     scanf( %s , st);     ;     dx = (st[1] ==  R ) ? 1 : -1;     dy = (st[0] ==  U ) ? -1 : 1;     for (x = 0; x < W; x++) {       if (((sx + sy) % 2) == (x % 2)) {         TX[0][x]++;         CT++;       }       if (((sx + sy) % 2) == ((x + H - 1) % 2)) {         TX[1][x]++;         CT++;       }     }     for (y = 0; y < H; y++) {       if (((sx + sy) % 2) == (y % 2)) {         TY[0][y]++;         CT++;       }       if (((sx + sy) % 2) == ((y + W - 1) % 2)) {         TY[1][y]++;         CT++;       }     }     signed long long res = 1;     for (i = 0; i < 1000000; i++) {       if (sx == 0 && TY[0][sy]) {         TY[0][sy] = 0;         CT--;       }       if (sx == W - 1 && TY[1][sy]) {         TY[1][sy] = 0;         CT--;       }       if (sy == 0 && TX[0][sx]) {         TX[0][sx] = 0;         CT--;       }       if (sy == H - 1 && TX[1][sx]) {         TX[1][sx] = 0;         CT--;       }       if (CT == 0) {         cout << res << endl;         return;       }       if (sx == 0) dx = 1;       if (sx == W - 1) dx = -1;       if (sy == 0) dy = 1;       if (sy == H - 1) dy = -1;       int ml = 1000000;       if (dx > 0) ml = min(ml, W - 1 - sx);       if (dx < 0) ml = min(ml, sx);       if (dy > 0) ml = min(ml, H - 1 - sy);       if (dy < 0) ml = min(ml, sy);       res += ml;       sx += ml * dx;       sy += ml * dy;     }     printf( -1 n );     return;   }   int main(int argc, char** argv) {     if (argc > 1) freopen(argv[1],  r , stdin);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void optimizeIO() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const int nmax = 150005;   const long long LINF = 1e17;   int ara[110];   set<int> SET;   int main() {     optimizeIO();     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ara[i];       SET.insert(ara[i]);     }     int siz = SET.size();     if (siz > 3) {       cout << -1;       return 0;     }     if (siz == 3) {       vector<int> v;       for (auto x : SET) v.push_back(x);       int a = v[2] - v[1];       int b = v[1] - v[0];       if (a != b) {         cout << -1;         return 0;       }       if (a == b) {         cout << a << endl;         return 0;       }     }     if (siz == 2) {       vector<int> v;       for (auto x : SET) v.push_back(x);       int a = v[1] - v[0];       if (a % 2 == 1) {         cout << a << endl;         return 0;       }       if (a % 2 == 0) {         cout << a / 2 << endl;         return 0;       }     }     if (siz == 1) {       cout << 0 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 30;   long long n, a[N], ans, limit = 1e9 + 10;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     reverse(a, a + n);     for (int i = 0; i < n; i++) {       ans += min(a[i], limit);       limit = min(limit - 1, a[i] - 1);       if (limit <= 0) break;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int Read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 3) + (x << 1) + ch -  0 ;       ch = getchar();     }     return x * f;   }   int n, m, q, a[200005], b[200005], f[200005], g[200005], h[200005],       minn[800005], rev[200005];   vector<int> pos[200005], pos2[200005];   void build(int o, int l, int r) {     if (l == r) {       minn[o] = (g[l] == -1) ? 1E9 : g[l];       return;     }     int mid = (l + r) >> 1;     build(o << 1, l, mid);     build(o << 1 | 1, mid + 1, r);     minn[o] = min(minn[o << 1], minn[o << 1 | 1]);   }   int query(int o, int l, int r, int nl, int nr) {     if (nl <= l && r <= nr) return minn[o];     int mid = (l + r) >> 1, ans = 1E9;     if (nl <= mid) ans = min(ans, query(o << 1, l, mid, nl, nr));     if (mid < nr) ans = min(ans, query(o << 1 | 1, mid + 1, r, nl, nr));     return ans;   }   signed main() {     n = Read(), m = Read(), q = Read();     for (int i = 1; i <= n; i++) a[i] = Read(), rev[a[i]] = i;     for (int i = 1; i <= m; i++) b[i] = Read(), pos[b[i]].push_back(i);     memset(f, -1, sizeof(f));     memset(g, -1, sizeof(g));     memset(h, -1, sizeof(h));     for (int i = n; i >= 1; i--) {       if (i == n) {         for (int j = 0; j < pos[a[i]].size(); j++) f[pos[a[i]][j]] = pos[a[i]][j];         continue;       }       for (int j = 0; j < pos[a[i]].size(); j++) {         vector<int>::iterator it =             lower_bound(pos[a[i + 1]].begin(), pos[a[i + 1]].end(), pos[a[i]][j]);         if (it == pos[a[i + 1]].end()) continue;         f[pos[a[i]][j]] = f[*it];       }     }     for (int i = 1; i <= n; i++) {       if (i == 1) {         for (int j = 0; j < pos[a[i]].size(); j++) h[pos[a[i]][j]] = pos[a[i]][j];         continue;       }       for (int j = 0; j < pos[a[i]].size(); j++) {         vector<int>::iterator it =             lower_bound(pos[a[i - 1]].begin(), pos[a[i - 1]].end(), pos[a[i]][j]);         if (it == pos[a[i - 1]].begin()) continue;         --it;         h[pos[a[i]][j]] = h[*it];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < pos[a[i]].size(); j++)         pos2[a[i]].push_back(h[pos[a[i]][j]]);     }     for (int i = 1; i <= m; i++) {       int ff = rev[b[i]];       if (ff == 1) {         g[i] = f[i];         continue;       }       if (f[i] == -1) continue;       vector<int>::iterator it =           lower_bound(pos2[a[ff - 1]].begin(), pos2[a[ff - 1]].end(), f[i]);       if (it == pos2[a[ff - 1]].end()) continue;       int Pos = it - pos2[a[ff - 1]].begin();       g[i] = pos[a[ff - 1]][Pos];     }     build(1, 1, m);     for (int i = 1; i <= q; i++) {       int l = Read(), r = Read();       int mpos = query(1, 1, m, l, r);       if (mpos <= r)         putchar( 1 );       else         putchar( 0 );     }     return 0;   }