Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /*
- Bismillahir Rahmanir Rahim
- Problem :
- Problem Link :
- Topics :
- Solver : Masud Parves
- I Love Code More than Sharks Love Blood <3
- */
- #define ff first
- #define ss second
- #define pb push_back
- #define mp make_pair
- #define all(a) a.begin(), a.end()
- #define sf(a) scanf("%d",&a)
- #define sff(a,b) scanf("%d%d",&a,&b)
- #define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
- #define f0(i,b) for(int i=0;i<(b);i++)
- #define f1(i,b) for(int i=1;i<=(b);i++)
- #define f2(i,a,b) for(int i=(a);i<=(b);i++)
- #define fr(i,b,a) for(int i=(b);i>=(a);i--)
- #define CIN ios_base::sync_with_stdio(0)
- #define mx 100005
- #define TEST_CASE(t) for(int z=1 ; z<=t ; z++)
- #define PRINT_CASE printf("Case %d: ",z)
- #define NOT_VISITED 0
- #define IS_VISITED 1
- int fx[4]= {1,-1,0,0};
- int fy[4]= {0,0,1,-1};
- const double PI = acos(-1.0);
- const double EPS = 1e-6;
- const int MOD = (int)1e9+7;
- const int maxn = (int)2e5+5;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef pair<int, int> pii;
- typedef pair<ll, int> plli;
- typedef pair<int, ll> pill;
- int arr[mx];
- struct segment
- {
- int ans,preValue,suffValue,preCnt,SuffCnt;
- void marge(segment left, segment right)
- {
- preValue=left.preValue;
- preCnt=left.preCnt; /// if value a[i] , a[i+1] are not same
- suffValue=right.suffValue;
- SuffCnt=right.SuffCnt; /// if value a[i] , a[i+1] are not same
- if(left.preValue==right.preValue) ///if value a[i] , a[i+1] are not same
- {
- preCnt=left.preCnt+right.preCnt;
- }
- if(left.suffValue==right.suffValue)
- {
- SuffCnt=left.SuffCnt+right.SuffCnt;
- }
- ans=max(left.ans, right.ans);
- if(left.suffValue==right.preValue)
- {
- ans=max(ans, left.SuffCnt+right.preCnt);
- }
- }
- } tree[4*mx];
- void BuildTree(int node, int b, int e )
- {
- if(b>e)
- return;
- if(b==e)
- {
- tree[node].preValue=tree[node].suffValue=arr[b];
- tree[node].preCnt=tree[node].SuffCnt=1;
- tree[node].ans=1;
- return;
- }
- int mid=(b+e)/2,left=node*2,right=left+1;
- BuildTree(left, b, mid);
- BuildTree(right, mid+1, e);
- tree[node].marge(tree[left], tree[right]);
- }
- segment Query(int node, int b, int e, int l, int r)
- {
- if(b>e || b>r || e<l)
- {
- segment res;
- res.preValue=0;
- res.suffValue=0;
- res.preCnt=0;
- res.SuffCnt=0;
- res.ans=0;
- return res;
- }
- if(b>=l && e<=r)
- {
- return tree[node];
- }
- int mid=(b+e)/2,left=node*2,right=left+1;
- segment lft,rgt,result;
- lft=Query(left, b, mid, l, r);
- rgt=Query(right, mid+1, e, l, r);
- result.marge(lft,rgt);
- return result;
- }
- int n,q,l,r;
- int main()
- {
- CIN;
- cin.tie(NULL);
- cout.tie(NULL);
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- /*
- 10 3
- -1 -1 1 1 1 1 3 10 10 10
- 2 3
- 1 10
- 5 10
- 0
- */
- sf(n);
- while(n)
- {
- sf(q);
- for(int i=1 ; i<=n ; i++)
- {
- sf(arr[i]);
- }
- BuildTree(1, 1, n);
- while(q--)
- {
- sff(l,r);
- printf("%d\n", Query(1,1,n,l,r).ans);
- }
- sf(n);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement