Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <set>
  6. #include <map>
  7. #include <bitset>
  8. #include <vector>
  9. #include <queue>
  10. #include <memory.h>
  11. #include <deque>
  12. #include <iomanip>
  13. #include <utility>
  14. #include <string>
  15. #include <ext/rope>
  16. #include <iterator>
  17.  
  18. using namespace std;
  19. using namespace __gnu_cxx;
  20.  
  21. #define pb push_back
  22. #define mp make_pair
  23. #define F first
  24. #define S second
  25. #define sz size
  26. #define scf scanf
  27. #define prf printf      
  28. #define all(x) x.begin(), x.end()
  29. #define rall(x) x.rbegin(), x.rend()
  30. #define gcd(a,b) __gcd(a,b)
  31. #define print(x) prf("%d\n", x.sz()); for (int i = 0; i < x.sz(); i++) prf("%d ", x[i]);
  32. #define getBit(x,i) ((x&(1<<i))!=0 ? 1 : 0)
  33. #define For(i,n) for (int i = 0; i < n; ++i)
  34. #define FOR(i,begin,end,move) for (int i = begin; i != end; i += move)
  35.  
  36. typedef long long ll;
  37.  
  38. const ll base = 1000000007LL;
  39. const ll INF = 10000000000000LL;
  40.  
  41. int q;
  42. int l, r;
  43. int cnt = 0;
  44. int curPos;
  45. string s;
  46. int typeOf[100500];
  47. bool used[100500] = {false};
  48. int next[100500] = {0};
  49. vector < pair < int, char > > st;
  50. vector < int > g[100500];
  51.  
  52. bool canFind(int pos, int data)
  53. {
  54.     int l = 0;
  55.     int r = g[pos].sz() - 1;
  56.     if (min(l, r) < 0)
  57.         return false;
  58.     while (l < r)
  59.     {
  60.         int mid = (l + r) >> 1;
  61.         if (g[pos][mid] >= data)
  62.             r = mid;
  63.         else l = mid + 1;
  64.     }
  65.     return g[pos][l] == data;
  66. }
  67.  
  68. int main()
  69. {
  70.     ios_base::sync_with_stdio(false);
  71.     getline(cin, s);
  72.     For(i, s.sz())
  73.     {
  74.         if (st.sz() > 0)
  75.         {
  76.             if (st.back().S - 'A' == s[i] - 'a')
  77.             {
  78.                 next[st.back().F] = i + 1;
  79.                 st.pop_back();
  80.                 continue;
  81.             }
  82.         }
  83.         st.pb(mp(i + 1, s[i]));
  84.     }
  85.     int n = s.sz();
  86.     For(i, n + 1)
  87.         if (!used[i] && next[i] != 0)
  88.         {
  89.             cnt++;
  90.             curPos = i;
  91.             while (true)
  92.             {
  93.                 if (next[curPos] == 0)
  94.                     break;
  95.                 used[curPos] = true;
  96.                 typeOf[curPos] = cnt;
  97.                 g[cnt].pb(next[curPos]);
  98.                 curPos = next[curPos] + 1;
  99.             }
  100.         }        
  101.     cin >> q;
  102.     while (q--)
  103.     {                      
  104.         cin >> l >> r;
  105.         if (canFind(typeOf[l], r))
  106.             cout << 1;
  107.         else cout << 0;
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement