\(\ Huge\text {Hello, I'm i64. I like violence algorithm very much, so I passed the problem with} \ \ \ Huge\text {violence.} \)
\(\ Huge\text {this article is not the correct solution to this problem, but uses a little bit of constant card skill} \)
No optimization
You can use kindergarten programming skills to write this code that can be written by the security man who smokes with his legs crossed under the building of my community every day.
#include<iostream> using namespace std; #define int long long const int maxn = 100010; int a[maxn]; signed main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } while (m--) { int opt; cin >> opt; if (opt == 1) { int l, r, x; cin >> l >> r >> x; for (int i = l;i <= r;i++) { if (a[i] % x == 0) { a[i] /= x; } } } if (opt == 2) { int l, r; cin >> l >> r; int ans = 0; for (int i = l;i <= r;i++) { ans += a[i]; } cout << ans << endl; } } return 0; }
Without O2, Xiti 15pts And this time is outrageous. I saw it last 1min for the first time(
With O2 on, Xiti 36pts
Try reading and writing fast
As we all know, the speed of CIN cout is slower than the old lady walking in the park in front of my house, so try to read and write quickly.
#include<iostream> using namespace std; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } void write(long long x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); return; } const int maxn = 100010; int a[maxn]; int opt; int n, m; int l, r, x; long long ans = 0; int i; signed main() { n = read(), m = read(); for (i = 1; i <= n; i++) { a[i] = read(); } while (m--) { opt = read(); if (opt == 1) { l = read(), r = read(), x = read(); for (int i = l;i <= r;i++) { if (a[i] % x == 0) { a[i] /= x; } } } if (opt == 2) { l = read(), r = read(); ans = 0; for (int i = l;i <= r;i++) { ans += a[i]; } write(ans); puts(""); } } return 0; }
Xiti 88pts , we are one step closer to AC!
Try loop expansion
As we all know, the pure for loop is a slow batch, so let's try to expand it.
#include <iostream> using namespace std; inline int read() { register int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } void write(register long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); return; } const int maxn = 100010; int a[maxn]; signed main() { register int opt; register int n, m; register int l, r, x; register long long sum = 0; register int i; n = read(), m = read(); for (i = 1; i <= n; i++) { a[i] = read(); } while (m--) { opt = read(); if (opt == 1) { l = read(), r = read(), x = read(); for (i = l; i <= r - 5; i += 6) { !(a[i] % x) ? a[i] /= x : 1; !(a[i + 1] % x) ? a[i + 1] /= x : 1; !(a[i + 2] % x) ? a[i + 2] /= x : 1; !(a[i + 3] % x) ? a[i + 3] /= x : 1; !(a[i + 4] % x) ? a[i + 4] /= x : 1; !(a[i + 5] % x) ? a[i + 5] /= x : 1; } while (r - i + 1) { !(a[i] % x) ? a[i] /= x : 1; i++; } } if (opt == 2) { sum = 0; l = read(), r = read(); for (i = l; i <= r - 5; i += 6) { sum += a[i] + a[i + 1] + a[i + 2] + a[i + 3] + a[i + 4] + a[i + 5]; } while (r - i + 1) { sum += a[i]; i++; } write(sum); puts(""); } } return 0; }
yeah, 97 pts Yes, it's almost AC!!!
At this time, we will be confused about this problem. You want to find the wonderful solution behind it. You know whether it is a positive solution, but you still want to master it. Otherwise, you will be uneasy, you will lose sleep, you will stay up late at night, sit in front of the computer and face the constant code code of Luogu and IDE, and you will soak in the computer room for years and months when you rest. You don't know how to start. You think you almost catch it, but every time you catch air
\(\ Huge\text {at this time, we need to use something called "problem solution"!} \)
Click in and you see an article Blog , and open it. I found that this blogger is big brother Wang Xiwen, and it is very similar to your idea!
He thought of the special judgment of \ (x \) as \ (1 \) and \ (2 \)!
If \ (x=1 \), exit directly! If \ (x=2 \), you can use bit operation to make your code faster!!!
So you
One last blow!
#include <iostream> using namespace std; inline int read() { register int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } void write(register long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); return; } const int maxn = 100010; int a[maxn]; signed main() { register int opt; register int n, m; register int l, r, x; register long long sum = 0; register int i; n = read(), m = read(); for (i = 1; i <= n; i++) { a[i] = read(); } while (m--) { opt = read(); if (opt == 1) { l = read(), r = read(), x = read(); if (x == 1) continue; if (x == 2) { for (i = l; i <= r - 5; i += 6) { !(a[i] & 1) ? a[i] >>= 1 : 1; !(a[i + 1] & 1) ? a[i + 1] >>= 1 : 1; !(a[i + 2] & 1) ? a[i + 2] >>= 1 : 1; !(a[i + 3] & 1) ? a[i + 3] >>= 1 : 1; !(a[i + 4] & 1) ? a[i + 4] >>= 1 : 1; !(a[i + 5] & 1) ? a[i + 5] >>= 1 : 1; } while (r - i + 1) { !(a[i] & 1) ? a[i] >>= 1 : 1; i++; } continue; } for (i = l; i <= r - 5; i += 6) { !(a[i] % x) ? a[i] /= x : 1; !(a[i + 1] % x) ? a[i + 1] /= x : 1; !(a[i + 2] % x) ? a[i + 2] /= x : 1; !(a[i + 3] % x) ? a[i + 3] /= x : 1; !(a[i + 4] % x) ? a[i + 4] /= x : 1; !(a[i + 5] % x) ? a[i + 5] /= x : 1; } while (r - i + 1) { !(a[i] % x) ? a[i] /= x : 1; i++; } } if (opt == 2) { sum = 0; l = read(), r = read(); for (i = l; i <= r - 5; i += 6) { sum += a[i] + a[i + 1] + a[i + 2] + a[i + 3] + a[i + 4] + a[i + 5]; } while (r - i + 1) { sum += a[i]; i++; } write(sum); puts(""); } } return 0; }