CodeForces - 1207F Remainder Problem(分块)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1207F Remainder Problem(分块)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個長度為 500000500000500000 的數組,初始時全部為 000,需要執行 nnn 次操作,每次操作分為兩種類型:
題目分析:考慮根號分塊,即小范圍暴力大范圍通過預處理獲得,當 x≤500000x \le \sqrt {500000}x≤500000? 時,直接維護一個前綴和 sumi,jsum_{i,j}sumi,j? 代表 x=i,y=jx = i,y = jx=i,y=j 時的答案;當 x≥500000x \ge \sqrt {500000}x≥500000? 時,暴力查詢的時間復雜度是 500000\sqrt {500000}500000?
考慮修改,每次直接暴力修改 sumsumsum 數組即可,時間復雜度同樣是 500000\sqrt {500000}500000? 的,所以這個題分塊實現兩個操作的時間復雜度均攤下來就是 O(nn)O(n\sqrt n)O(nn?) 的
代碼:
// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; LL sum[1010][1010],a[N]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;read(n);for(int i=1;i<=n;i++){int op,x,y;read(op),read(x),read(y);if(op==1){for(int i=1;i<=1000;i++) sum[i][x%i]+=y;a[x]+=y;}else{if(x<=1000) write(sum[x][y]),putchar('\n');else{LL ans=0;for(int i=y;i<=500000;i+=x) ans+=a[i];write(ans),putchar('\n');}}}return 0; }總結
以上是生活随笔為你收集整理的CodeForces - 1207F Remainder Problem(分块)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1217F F
- 下一篇: CodeForces - 1465E P