I have been trying to get the problem Quadrant Queries correct for a while now. Although my approach is an optimal one, I am getting a TLE on 3 test cases. Please help me optimize my approach so that I can pass all test cases for the problem.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
typedef pair <int,int> pii;
void performCquery(int i,int j, const vector< pii > &pointList)
{
i = i-1;
j = j-1;
int q1 = 0;
int q2 = 0;
int q3 = 0;
int q4 = 0;
for (int k = i; k <=j ; k++) {
if ( (pointList[k].first > 0 ) && ( pointList[k].second > 0) ) {
q1++;
}
else if ( (pointList[k].first < 0) && ( pointList[k].second > 0) ) {
q2++;
}
else if ( ( pointList[k].first < 0) && (pointList[k].second < 0 ) ) {
q3++;
}
else if ( ( pointList[k].first > 0) && (pointList[k].second < 0 ) ) {
q4++;
}
}
cout << q1 << " " << q2 << " " << q3 << " " << q4 << endl;
}
void performXYquery(char qType, int i, int j, vector< pii > &pointList)
{
i = i-1;
j = j-1;
for ( int k = i; k <= j ;k++) {
pii pnt = pointList[k];
if ( qType == 'X') {
pointList[k].second = -pointList[k].second;
}
else if (qType == 'Y') {
pointList[k].first = -pointList[k].first;
}
}
}
int main(int argc, char* argv[])
{
int numOfPoints;
cin >> numOfPoints;
vector < pii > pointList;
for (int i = 0; i < numOfPoints ; i++) {
int x,y;
cin >> x >> y;
pii temp(x,y);
pointList.push_back(temp);
}
int numOfQueries;
cin >> numOfQueries;
for (int i = 0; i < numOfQueries; i++) {
char qName;
int startIndex;
int endIndex;
cin >> qName >> startIndex >> endIndex;
if (qName == 'C') {
performCquery(startIndex,endIndex, pointList);
}
else if (qName == 'X' || qName == 'Y') {
performXYquery(qName, startIndex,endIndex, pointList);
}
else {
cerr << "Error: This line should not be printed" <<endl;
}
}
return 0;
}