I want to know the best way to implement this (approach). I will bee given set of coordinates (x,y). I will then be queried based on those coordinates like
1.C a b => where a and b are integer indexes in the initial set of coordinates. So I need to output the number of points that are in 1st ,2nd ,3rd and fourth quadrant that are in index range a to b.
2.X a b => where a and b are integer indexes in the initial set of coordinates. So I need to mirror the ath to bth indexed coordinates along x axis.
3.Y a b => where a and b are integer indexes in the initial set of coordinates. So I need to mirror the ath to bth indexed coordinates along y axis.
there can be at most 100000 coordinates or points and 500000 such queries on them.
I tried a brute force method looping through on every query but it had TLE(Time Limit Exceeded).
What should I do in such type of questions?
Here is my code
#include <iostream>
#include <stdio.h>
using namespace std;
char flipX[4] = { 3, 2, 1, 0 };
char flipY[4] = { 1, 0, 3, 2 };
int main(int argc, char **argv)
{
int n,x,y;
char c[100000];
scanf("%d",&n);
//coord *c=new coord[n];
for(int i=0;i<n;i++)
{
scanf("%d",&x);
scanf("%d",&y);
if(x<0 && y<0)
c[i]=2;
else if(x>0 && y>0)
c[i]=0;
else if(x>0 && y<0)
c[i]=3;
else
c[i]=1;
}
int q,i,j,a,cnt[4];
char ch;
scanf("%d",&q);
while(q--)
{
//cout<<"q:"<<q<<endl;
cin>>ch;
scanf("%d",&i);
scanf("%d",&j);
i--;j--;
if(ch=='X')
{
//case 'X':
for(a=i;a<=j;a++)
c[a]=flipX[c[a]];
// break;
}
else if(ch=='Y')
{
//case 'Y':
for(a=i;a<=j;a++)
c[a]=flipY[c[a]];
// break;
}
else if(ch=='C')
{
cnt[0]=cnt[1]=cnt[2]=cnt[3]=0;
for(a=i;a<=j;a++)
{
cnt[c[a]]++;
}
printf("%d %d %d %d\n",cnt[0],cnt[1],cnt[2],cnt[3]);
}
}
return 0;
}
Please Help.
Ok. Lazy segment trees did the trick. Thanx everyone for your help