The value of t becomes 0 after the scanf statement cant understand why, t is affected by this statement, even if t=100 the program runs for only 1 iteration! PS first question here! and it took 100 minutes to write this! always pops up some problem! :@
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#define MOD 1000000009
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
vector< pair<long long int,char> > a(m);
long long int x;
int i;
char d[1];
for(i=0;i<m;i++)
{
scanf("%s%lld",d,&x);// t becomes zero after this
a[i]=make_pair(x,d[0]);
}
sort(a.begin(),a.end());
long long int ans=1;
for(i=0;i<m-1;i++)
{
if(a[i].second!=a[i+1].second)
{
ans=ans*(a[i+1].first-a[i].first);
ans=ans%MOD;
}
}
ans=ans%MOD;
printf("%lld\n",ans);
}
return 0;
}
You are causing a buffer overflow when calling scanf("%s%lld",d,&x). d only has room for 1 char, but %s reads until a white space character is encountered. Even if the user types in only 1 character before whitespace, it will still overflow because %s writes a null terminator at the end of the buffer it writes to. That is why t gets modified.
If you really want to read only 1 char, you need to either:
declare d as just a char and use %c:
char d;
scanf("%c%lld",&d,&x);
declare d as char d[2] so it has room for a null terminator and use %1s:
char d[2];
scanf("%1s%lld",d,&x);
BTW, you have to be careful with things like scanf("%d%d",&n,&m). Think of what happens if the user types in "123456" and you wanted to read it as 123 and 456 separately. The user would have to type "123 456" instead. So just be aware of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With