Monk and Otakuland
Problem
Monk lives in Otakuland. Otakuland consists of N vertices and N-1 directed edges. i-th edge is a directed edge either from i-th vertex to i+1-th vertex or from i+1-th vertex to i-th vertex. You are given M Queries. Queries are 2 types:
- 1 l r - Reverse the direction of the edges between l-th vertex and r-th vertex.
- 2 f t - Output the minimum number of edges which you have to reverse the direction to arrive from f to t.
Input:
The first line contains two integers N, the number of vertices and M, the number of queries. The next line will containsN-1 characters which represent the direction of the edges. i-th character is either '>' or '<': '>' represents that only i -> i+1is valid, '<' represents that only i+1 -> i is valid. The following M lines will each contain a query like the ones mentioned above.
Output:
For query 2, print the answer in a new line.
Constraints:
2 ≤ N ≤ 200000
1 ≤ M ≤ 200000
1 ≤ l < r ≤ N
1 ≤ f , t ≤ N
1 ≤ M ≤ 200000
1 ≤ l < r ≤ N
1 ≤ f , t ≤ N
Sample Input
(Plaintext Link)6 6 >><<> 2 1 6 2 6 1 1 3 5 2 1 6 1 1 6 2 6 1
Sample Output
(Plaintext Link)2 3 0 0
Explanation
In the sample testcase the graph is like this, 1->2->3<-4<-5->6. At the first query, Monk have to reverse the direction of 3rd edge and 4th edges to arrive from 1st vertex to 6th vertex. Second, Monk have to reverse the direction of 1st, 2nd, and 5th edges to arrive from 6th vertex to 1st vertex. Third, Reverse the direction of the edges between 3rd vertex and 5th vertex. After the query, graph is like this, 1->2->3->4->5->6. Fourth, Monk don't have to reverse the direction of any edge. Fifth, Reverse the direction of the edges between 1st vertex and 6th vertex. After the query, graph is like this, 1<-2<-3<-4<-5<-6. Sixth, Monk don't have to reverse the direction of any edge.
------------------------------------------editorial-------------------------------------------------------------------------------------------------------------
Reach-ability of t from f
All the edges between f to t must be in the direction from f towards *t.
Lets say f<t, then all the edges must be towards the right. The edges we need to reverse are those edges that are in the opposite direction from t towards *f.
All the edges between f to t must be in the direction from f towards *t.
Lets say f<t, then all the edges must be towards the right. The edges we need to reverse are those edges that are in the opposite direction from t towards *f.
Thus the answer to any query is the count of edges that are in direction from t towards f. Without loss of generality assume f<t. The answer would be the number of edges in the left direction.
Thus to solve this problem we need to maintain the direction of each edge and efficiently count the number of edges facing left in any queried range.
The two tasks can be done easily using segment tree with lazy propagation ( as there are range updates and range queries). Any node of segment tree can store the number of edges in the left direction in its responsibility range. We can deal with updates lazily.
The case when f>t can be similarly answered. Just count the number of edges facing right =( total edges in between fand t) - (total edges facing left in the same range).
The two tasks can be done easily using segment tree with lazy propagation ( as there are range updates and range queries). Any node of segment tree can store the number of edges in the left direction in its responsibility range. We can deal with updates lazily.
The case when f>t can be similarly answered. Just count the number of edges facing right =( total edges in between fand t) - (total edges facing left in the same range).
Thus there is O(log N) per update and O(log N) per query making it O(N + Q * log N) final time complexity.
-------------------------------------------------------------------------code----------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int arr[6010000];
#define inf 999999999
struct st
{
int zero;
int one;
}
tree[6000000];
int lazy[6000000];
int ans=0;
int read_int(){
char r;
bool start=false,neg=false;
int ret=0;
while(true){
r=getchar();
if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
continue;
}
if((r-'0'<0 || r-'0'>9) && r!='-' && start){
break;
}
if(start)ret*=10;
start=true;
if(r=='-')neg=true;
else ret+=r-'0';
}
if(!neg)
return ret;
else
return -ret;
}
int query(int node,int start,int end,int r1,int r2,int tt)
{
// cout<<start<<" "<<end<<endl;
// cout<<" r1 "<<r1<<" r2 "<<r2<<endl;
if(lazy[node])
{
if(lazy[node]%2==1)
{
int temp=tree[node].zero;
tree[node].zero=tree[node].one;
tree[node].one=temp;
lazy[2*node]+=lazy[node];
lazy[2*node+1]+=lazy[node];
}
lazy[node]=0;
}
if(start>end || r1>end || r2<start || r1>r2) return 0;
if(r1<=start && r2>=end)
{
// cout<<"here "<<node<<endl;
if(tt==1)
{
// cout<<" returning "<<tree[node].zero<<endl;
ans+=tree[node].zero;
return tree[node].zero;
}
else
{//
// cout<<" returning "<<tree[node].one<<endl;
ans+=tree[node].one;
return tree[node].one;
}
}
else
{
int q1=query(2*node,start,(start+end)/2,r1,r2,tt);
int q2=query(2*node+1,((start+end)/2)+1,end,r1,r2,tt);
return (q1+q2);
}
}
void update(int node ,int start,int end,int r1,int r2,int val)
{
// cout<<" update in the range "<<start<<" "<<end<<endl;
if(lazy[node]!=0)
{
// cout<<" lazy node "<<node<<endl;
int times=lazy[node];
// cout<<" lazy node "<<node<<" times "<<times<<endl;
times%=2;
if(times==1)
{
int temp=tree[node].zero;
tree[node].zero=tree[node].one;
tree[node].one=temp;
// cout<<" making lazing "<<2*node<<" "<<2*node+1<<endl;
lazy[2*node+1]+=1;
lazy[2*node]+=1;
}
lazy[node]=0;
}
if(r1>end || r2<start || start>end) return ;
if(r1<=start && r2>=end)
{
int temp=tree[node].zero;
tree[node].zero=tree[node].one;
tree[node].one=temp;
if(start!=end)
{
lazy[2*node]+=1;
lazy[2*node+1]+=1;
// cout<<" making lazing "<<2*node<<" "<<2*node+1<<endl;
}
return ;
}
update(2*node, start,(start+end)/2,r1,r2,val);
update(2*node+1, ((start+end)/2)+1,end,r1,r2,val);
// cout<<" finalizing node "<<node<<endl;
tree[node].zero=tree[2*node].zero+tree[2*node+1].zero;
tree[node].one=tree[2*node].one+tree[2*node+1].one;
}
void build(int node , int start,int end)
{
if(start==end)
{
if(arr[start]==1)
{
tree[node].zero=0;
tree[node].one=1;
}
else
{
tree[node].zero=1;
tree[node].one=0;
}
// cout<<" at nod"<<node<<" zero "<<tree[node].zero<<" one "<<tree[node].one<<endl;
}
else if(start>end) return ;
else
{
build(2*node,start,(start+end)/2);
build(2*node+1,((start+end)/2)+1,end);
tree[node].zero=tree[2*node].zero+tree[2*node+1].zero;
tree[node].one=tree[2*node].one+tree[2*node+1].one;
}
}
int main()
{
int n,q;
// cin>>n>>q;
n=read_int();
q=read_int();
char ae[10000+n];
cin>>ae;
for(int i=0;i<n-1;i++)
{
if(ae[i]=='>')arr[i]=1;
else arr[i]=0;
}
for(int i=0;i<2*n+100;i++)
{
tree[i].zero=0;
tree[i].one=0;
}
build(1,0,n-2);
while(q--)
{
ans=0;
int typ,l,r;
// cin>>typ>>l>>r;
typ=read_int();
l=read_int();
r=read_int();
if(typ==2)
{
int res;
if(l<=r)
res=query(1,0,n-2,l-1,r-2,1);
else
{
res=query(1,0,n-2,r-1,l-2,0);
}
cout<<ans<<endl;
}
else
{
int val=0;
update(1,0,n-2,l-1,r-2,val);
}
}
return 0;
}
No comments:
Post a Comment