To Increment or Not to Increment

To Increment or Not to Increment

So, I re-did POJ 2352 (Stars) today, and got a time limit exceeded error.
See the post earlier for a recap.
I spent a hour debugging while loops, no luck. It's the only source of infinite loops, after all.
ARRGGHH

So, here's my code:

#include <cstdio>
using namespace std;
int BIT[33001];
int ans[15001];
void incr(int ind)
{
while(ind<33000)

BIT[ind]++;
ind += (ind&(-ind));
}
}
int get(int ind)
{
int res=0;
while(ind)
{
res += BIT[ind];
ind -= (ind&(-ind));
}
return res;
}
int main()
{
int n; scanf("%d",&n);
for(int i=0;i<n;i++)
{
int x, y; scanf("%d %d",&x, &y);
ans[get(x)]++;
incr(x);
}
for(int i=0;i<n;i++)
printf("%d\n",ans[i]);
}
Ahem. See that disturbing red line over there?
"No?"
Maybe you're color blind. Okay.
But anyways, this little monster here:
ans[get(x)]++;
Read that line ten times, then read this line:
ans[get(++x)]++;
What's the difference?
"There's no difference!"
Okay, so you are plainly blind then.
But, the x, passed as an argument to get(), has a preincrement operator at its front.
Read the code again, particularly the get() part and the output part.
What difference does it make?
"I don't know!"
Well it solved the TLE problem. But why? It shouldn't make a problem.


Answer

Hmm! So you think you can cheat yourself out by looking at the "Answer" section, eh? Well too bad for you, it's not here today yet. So think about it more.

:P

Comments

Popular posts from this blog

USACO Training "subset": Subset Sums

USACO 2018 Open: Talent Show

OpenJudge 1768: Kadane's Algorithm on 2D Arrays