Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define NMAX 100001
- using namespace std;
- ifstream fin("consec.in");
- int tree_min[4*NMAX];
- int N, Q, arr[NMAX];
- long long sum[NMAX];
- int min(int a, int b)
- {
- return a < b ? a : b;
- }
- void build_min(int arr[], int node, int left, int right)
- {
- if (left == right)
- tree_min[node] = arr[left];
- else
- {
- build_min(arr, 2 * node, left,(left + right) / 2);
- build_min(arr, 2 * node + 1,(left + right) / 2 + 1, right);
- tree_min[node] = min(tree_min[2 * node],tree_min[2 * node + 1]);
- }
- }
- int query_min(int node, int c_l, int c_r, int l, int r)
- {
- if (c_l > r || c_r < l)
- return 1e9;
- if (c_l >= l && c_r <= r)
- return tree_min[node];
- else
- return min(query_min(2 * node, c_l,(c_l + c_r) / 2, l, r),
- query_min(2 * node + 1,(c_l + c_r) / 2 + 1,c_r, l, r));
- }
- int main()
- {
- int i, l, r, q;
- cin>>N>>arr[0];
- sum[0]=arr[0];
- for(i=1; i<N; ++i)
- {
- cin>>arr[i];
- sum[i]=(0LL + sum[i-1]+arr[i]);
- }
- build_min(arr, 1, 0, N - 1);
- cin>>Q;
- for(q=0; q<Q; ++q)
- {
- cin>>l>>r;
- l--,r--;
- int minim=query_min(1, 0, N - 1, l, r);
- if(l==0)
- cout<<(sum[r]==1LL*(r-l+1)*minim+1LL*(r-l)*(r-l+1)/2);
- else
- cout<<(sum[r]-sum[l-1]==1LL*(r-l+1)*minim+1LL*(r-l)*(r-l+1)/2);
- }
- return 0;
- }
- /**
- SO
- #include <bits/stdc++.h>
- #define nmax 100001
- using namespace std;
- int n, a[nmax], rmin[18][nmax], rmax[18][nmax];
- int P[nmax]; /// P[i] = j : secventa de la j la i are doar
- /// numere distincte
- int fr[nmax], lg[nmax];
- void Citire()
- {
- cin >> n;
- for (int i = 1; i <= n; i++)
- cin >> a[i];
- }
- void ConstructieP()
- {
- int i, j, x;
- j = 1;
- for (i = 1; i <= n; i++)
- {
- x = a[i];
- while (fr[x] != 0)
- {
- fr[a[j]]--;
- j++;
- }
- fr[x]++;
- P[i] = j;
- }
- }
- void RMQ()
- {
- int i, j, k;
- /// lg[i] = k - exponentul maxim cu proprietatea
- /// ca 2^k este mai mic sau egal cu i
- lg[1] = 0;
- for (i = 2; i <= n; i++)
- lg[i] = lg[i / 2] + 1;
- /// ---------------- rmin ------------------------
- for (i = 1; i <= n; i++)
- rmin[0][i] = a[i];
- for (i = 1; (1 << i) <= n; i++)
- for (j = (1 << i); j <= n; j++)
- {
- k = 1 << (i - 1);
- rmin[i][j] = min(rmin[i - 1][j], rmin[i - 1][j - k]);
- }
- /// ---------------- rmax ------------------------
- for (i = 1; i <= n; i++)
- rmax[0][i] = a[i];
- for (i = 1; (1 << i) <= n; i++)
- for (j = (1 << i); j <= n; j++)
- {
- k = 1 << (i - 1);
- rmax[i][j] = max(rmax[i - 1][j], rmax[i - 1][j - k]);
- }
- }
- void Interogari()
- {
- int Q, x, y, k, m, M;
- cin >> Q;
- while (Q--)
- {
- cin >> x >> y;
- if (P[y] <= x) /// secventa a[x..y] are doar nr. distincte
- {
- k=lg[y - x + 1];
- m = min(rmin[k][x + (1<<k) - 1], rmin[k][y]);
- M = max(rmax[k][x + (1<<k) - 1], rmax[k][y]);
- if (M - m == y - x) cout << "1";
- else cout << "0";
- }
- else cout << "0";
- }
- }
- int main()
- {
- Citire();
- ConstructieP();
- RMQ();
- Interogari();
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement