There is a row of two-position switches aligned in a row and numbered consecutively starting from 1. Each of the switches is initially in its "Off" position. Over some number of operations N, a left and right index will be provided. When a current is applied to two switches, a NOT operation is applied to each switch in the inclusive interval between those switches. That is, if a switch is off, it is turned on, and vice versa. Given a series of operations, determine their final state. Calculate the sum of all indices where a switch is on.
N<=10^5
Example
operations-[[1, 4], [2, 6], [1,6]]
0utput -9
#include<bits/stdc++.h>
using namespace std;
int solve(vector<pair<int,int>>& operations){
int n = 0;
for(auto operation : operations){
n = max(n,operation.second);
}
vector<int> toggle(n+1,0);
for(auto operation : operations){
for(int i=operation.first;i<=operation.second;i++){
toggle[i] = 1 - toggle[i];
}
}
int sum = 0;
for(int i=1;i<=n;i++){
if(toggle[i] == 1){
sum+=i;
}
}
return sum;
}
int main(){
int x;
cin>>x;
vector<pair<int,int>> operations;
for(int i=0;i<x;i++){
int x,y;
cin>>x>>y;
operations.push_back({x,y});
}
cout<<solve(operations);
}Will this give TLE? If yes plz give optimized code