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]);
}
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
Post a Comment