2511425938514_'s blog

By 2511425938514_, history, 23 months ago, In English

Can anyone explain why my code 188948183 for 1775B - Садовник и массив is TLE? Doesn't it work for O(∑k[i])?

Tags tle
  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
23 months ago, # |
  Vote: I like it +5 Vote: I do not like it

vector a[100005]; and int k[114514]; causes tle because n <= 1e5 so you are doing 2e5 * 1e5 operations

»
23 months ago, # |
Rev. 6   Vote: I like it 0 Vote: I do not like it

I have corrected it: https://mirror.codeforces.com/contest/1775/submission/188960544. Yes, your code indeed works in $$$O(\sum k_i)$$$, but there are too many minor issues in your code:

  1. The most important, you don't use fastio. fastio means cin.tie(0) -> sync_with_stdio(0). That is the key reason.
  2. What tn757 said is also a key reason. Sorry for my previous comments. It allocates a large array for every test case.
  3. #define int long long I know many people like this, but long long is slower than int. Here, int is enough. I once get TLE using long long, and get AC using int.
  4. if(s[a[i][j]]==1) { f=1; }: I suggest you to add a break: if(s[a[i][j]]==1) { f=1; break; }. This optimization also works for if(f == 0) {ans = "Yes\n";}.
  5. vector.push_back is slower than memory pre-allocation using resize.

Thanks for giving me a chance to improve my debugging ability.