#include<iostream> #include<cstdio> #include<queue> #include<algorithm> # define R register # define LL long long
usingnamespace std;
int n,p,a[310][310],f[310],g[310],ans,t[60],x[310][310];
template <typename T> inlinevoidin(R T& a){ R char c=getchar(); R T x=0,f=1; while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) x=(x<<1)+(x<<3)+c-'0',c=getchar(); a=x*f; }
template <typename T> inlinevoidmaxx(R T&a,R T b){a<b? a=b:0;}
inlinevoidsolve(){ for (R int i=1; i<=n; ++i) f[i]=g[i]=0; for (R int i=1; i<=n; ++i) for (R int j=1; j<=n; ++j) a[i][j]=x[i][j],a[i][j]+=a[i][j-1]; for (R int l=1; l<=n; ++l) { for (R int r=l; r<=n; ++r) { R int sum=0; for (R int i=1; i<p; ++i) t[i]=-1; for (R int i=1; i<=n; ++i) { sum += a[i][r]-a[i][l-1]; sum %= p; if (t[sum]!=-1) maxx(f[r],(r-l+1)*(i-t[sum])),maxx(g[l],(r-l+1)*(i-t[sum])); else t[sum] = i; } } } for (R int i=n-1; i; --i) maxx(g[i],g[i+1]); for (R int i=1; i<n; ++i) maxx(ans,f[i]+g[i+1]); }
intmain(){ in(n),in(p); for (R int i=1; i<=n; ++i) for (R int j=1; j<=n; ++j) in(x[i][j]); solve(); for (R int i=1; i<=n; ++i) for (R int j=i+1; j<=n; ++j) x[i][j] ^= x[j][i] ^= x[i][j] ^= x[j][i]; solve(); printf("%d",ans); return0; }
紧接着我们需要做的就是找到一个在这个一维数组中找到一个尽可能大的区间使得区间和是𝑝的倍数。我们依然考虑前缀和,定义𝑓𝑖表示数组中前𝑖项和对𝑝取模之后的值,由此我们可知,如果存在𝑥和𝑦使得𝑓𝑥=𝑓𝑦且𝑥<𝑦,则由前缀和定义以及取模性质可知\(\begin{aligned}\sum_{x+1} ^ y a_i\% p =
0\end{aligned}\),即该区间和为𝑝的倍数。那么我们可以想到对于每一个固定的𝑦,我们只需要找到最小的𝑥使得𝑥<𝑦且𝑓𝑥=𝑓𝑦,那么这个区间就是以𝑦为右边界且区间和为𝑝的倍数的最大区间。而这个过程我没只需要从前往后扫一遍,维护一下对于𝑝取模后的每一个值在𝑓𝑖中出现的最早位置,同时查找一下与当前𝑓𝑖相等的最早出现的位置,二者做差即可,这个过程是𝑂(𝑛)的,这样我们便可以\(𝑂(𝑛^3)\)地求出面积最大的满足和是𝑝地倍数地矩阵。
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> # define R register # define LL long long # define inf 200000000 # define N 50010
usingnamespace std;
int n,m,x,y,z,e,h[N],t[N],f[N],ans[N][2],dis[N];
structzx{int v,w,pre;} ed[N*7]; structyy{ int x,w; booloperator < (const yy y) const {return w>y.w;} }; bool vis[N]; //priority_queue <yy> q; queue <int> q; template <typename T> inlinevoidin(R T& a){ R char c=getchar(); R T x=0,f=1; while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) x=(x<<1)+(x<<3)+c-'0',c=getchar(); a=x*f; }
template <typename T> inlinevoidmaxx(R T&a,R T b){a<b? a=b:0;} template <typename T> inlinevoidminn(R T&a,R T b){a>b? a=b:0;}
inlinevoidadd(R int x,R int y,R int z){ ed[++e] = (zx){y,z,h[x]}; h[x] = e; }
inlinevoiddij(R int a,R int b,R int c){ for (R int i=1; i<=n; ++i) if (t[i]==a&&f[i]==b) dis[i]=0,q.push(i); else dis[i]=inf; while (!q.empty()){ R int x=q.front(); q.pop(); for (R int i=h[x]; i; i=ed[i].pre) { R int v=ed[i].v; if (dis[v] > dis[x]+ed[i].w) { dis[v]=dis[x]+ed[i].w; if (!vis[v]) vis[v]=1,q.push(v); } } vis[x]=0; } for (R int i=1; i<=n; ++i) if (t[i]==c&&f[i]!=b) minn(ans[i][a^c],dis[i]); }
intmain(){ in(n),in(m); for (R int i=1; i<=n; ++i) in(t[i]),ans[i][0]=ans[i][1]=inf; for (R int i=1; i<=m; ++i) in(x),in(y),in(z),add(y,x,z); for (R int p=0; p<=15; ++p) { for (R int i=1; i<=n; ++i) if ((i>>p)&1) f[i]=1; else f[i]=0; dij(0,0,0); dij(0,0,1); dij(0,1,0); dij(0,1,1); dij(1,0,0); dij(1,0,1); dij(1,1,0); dij(1,1,1); } for (R int i=1; i<=n; ++i,printf("\n")) { if (ans[i][0] == inf) printf("-1 "); elseprintf("%d ",ans[i][0]); if (ans[i][1] == inf) printf("-1"); elseprintf("%d",ans[i][1]); } return0; }
#include<bits/stdc++.h> #define LL long long #define debug usingnamespace std;
constint N = 1e5+66, INF = 214748364;
int n, m, a[N]; int vis[N], dis[N], tot[N]; int ans[N][6];
queue<int>q;
structnode {int to, val, nxt;}e[N]; int cnt, head[N];
inlinevoidjiabian(int u, int v, int c){ ++ cnt; e[cnt].to = v, e[cnt].val = c; e[cnt].nxt = head[u], head[u] = cnt; }
inlinevoidshuruyijichushihua(){ cin >> n >> m; for (int i = 1; i <= n; ++ i) { scanf ("%d", &a[i]); ans[i][0] = ans[i][1] = INF; } for (int i = 1; i <= m; ++ i) { int x, y, z; cin >> x >> y >> z; jiabian (x, y, z); } }
inlinevoidzuiduanlusuanfa(int t, int b, int s){ for (int i = 1; i <= n; ++ i) { dis[i] = INF; if (a[i] == t && tot[i] == b) { q.push(i); vis[i] = true; dis[i] = 0; } } while(q.size()) { int x = q.front(); q.pop(); vis[x] = false; for (int i = head[x]; i; i = e[i].nxt) { int y = e[i].to; if (dis[x]+e[i].val<dis[y]) { dis[y] = dis[x]+e[i].val; if (!vis[y]) { vis[y] = 1; q.push(y); } } } } for (int i = 1; i <= n; ++ i) { if (a[i] == s && tot[i] != b) { if (ans[i][t^s] > dis[i]) ans[i][t^s] = dis[i]; } } }
inlinevoidxiugai(int L, int R, int val){ if (pos[L] == pos[R]) { //do do do.. return; } for (int i = L; i <= pos[L]*len; ++ i) //do do do... for (int i = pos[L]+1; i <= pos[R]-1; ++ i)//do do do... for (int i = (pos[R]-1)*len+1; i <= R; ++ i)//do do do... } inlinevoidchaxun(int now){cout << a[now]+tag[pos[now]];} inlineintthestars(){ cin >> n; len = sqrt(n); for (int i = 1; i <= n; ++ i) { cin >> a[i]; pos[i] = (i-1)/len+1; } xiugai(l, r, c); chaxun(x); return0; }
#include<bits/stdc++.h> #define N 600000 usingnamespace std; signed n, len; int pos[N], tag[N], a[N];
inlinevoidadd(int L, int R, int val){ if (pos[L] == pos[R]) { for ( int i = L;i<=R;++i) a[i] += val; return; } for ( int i = L;i<=pos[L]*len;++i) a[i] += val; for ( int i = pos[L]+1;i<=pos[R]-1;++i) tag[i] += val; for ( int i = (pos[R]-1)*len+1;i<=R;++i) a[i] += val; }
signedmain(){ n = Read (), len = sqrt(n); for ( int i = 1;i<=n;++i) a[i] = Read (), pos[i] = (i - 1)/len + 1; for ( int i = 1;i<=n;++i) { int opt, l, r, c; opt = Read (), l = Read (), r = Read (), c = Read (); if (!opt) add (l, r, c); elsePut (a[r] + tag[pos[r]]); } return0; }
#include<bits/stdc++.h> #define LL long long #define debug usingnamespace std;
constint N = 1e5 + 66;
int n, m, len, a[N]; int pos[N], paixu[N]; int tag[N];
inlinevoidshuruyijichushihua(){ cin >> n; len = sqrt(n); for (int i = 1; i <= n; ++i) { cin >> a[i]; paixu[i] = a[i]; pos[i] = (i - 1) / len + 1; } for (int i = 1; i <= pos[n]; ++i) { int s = (i - 1) * len + 1, t = i * len; sort(paixu + s, paixu + t + 1); } int now = (pos[n] - 1) * len + 1; sort(paixu + now, paixu + n + 1); }
inlinevoidzaimougequjianjiashangyigeshu(int L, int R, int val){ int s = 0, t = 0; if (pos[L] == pos[R]) { for (int i = L; i <= R; ++i) a[i] += val; s = (pos[L] - 1) * len + 1, t = pos[L] * len; for (int i = s; i <= t; ++i) paixu[i] = a[i]; sort(paixu + s, paixu + t + 1); return; } for (int i = L; i <= pos[L] * len; ++i) a[i] += val; s = (pos[L] - 1) * len + 1, t = (pos[L] * len); for (int i = s; i <= t; ++i) paixu[i] = a[i]; sort(paixu + s, paixu + t + 1); for (int i = pos[L] + 1; i <= pos[R] - 1; ++i) tag[i] += val; for (int i = (pos[R] - 1) * len + 1; i <= R; ++i) a[i] += val; s = (pos[R] - 1) * len + 1, t = pos[R] * len; for (int i = s; i <= t; ++i) paixu[i] = a[i]; sort(paixu + s, paixu + t + 1); }
inlineintxiaoyumougeshudegeshu(int L, int R, int who){ intres(0); if (pos[L] == pos[R]) { for (int i = L; i <= R; ++i) if (a[i] + tag[pos[R]] < who) ++res; return res; } for (int i = L; i <= pos[L] * len; ++i) if (a[i] + tag[pos[L]] < who) ++res; for (int i = pos[L] + 1; i <= pos[R] - 1; ++i) { int now = (i - 1) * len + 1, to = i * len; int chazhao = lower_bound(paixu + now, paixu + to + 1, (who - tag[i])) - paixu; res += chazhao - now; } for (int i = (pos[R] - 1) * len + 1; i <= R; ++i) if (a[i] + tag[pos[R]] < who) ++res; return res; }
inlinevoidjiejuewentiyijishuchu(){ for (int i = 1; i <= n; ++i) { int opt, l, r, c; cin >> opt >> l >> r >> c; if (opt == 0) zaimougequjianjiashangyigeshu(l, r, c); else cout << xiaoyumougeshudegeshu(l, r, c * c) << '\n'; } }
#include<bits/stdc++.h> #define LL long long #define debug usingnamespace std;
constint N = 1e5 + 66;
int n, m, len, a[N]; int pos[N], paixu[N]; int tag[N];
inlinevoidshuruyijichushihua(){ cin >> n; len = sqrt(n); for (int i = 1; i <= n; ++i) { cin >> a[i]; paixu[i] = a[i]; pos[i] = (i - 1) / len + 1; } for (int i = 1; i < pos[n]; ++i) { int s = (i - 1) * len + 1, t = i * len; sort(paixu + s, paixu + t + 1); } int now = (pos[n] - 1) * len + 1; sort(paixu + now, paixu + n + 1); }
inlinevoidzaimougequjianjiashangyigeshu(int L, int R, int val){ int s = 0, t = 0; if (pos[L] == pos[R]) { for (int i = L; i <= R; ++i) a[i] += val; s = (pos[L] - 1) * len + 1, t = pos[L] * len; for (int i = s; i <= t; ++i) paixu[i] = a[i]; sort(paixu + s, paixu + t + 1); return; } for (int i = L; i <= pos[L] * len; ++i) a[i] += val; s = (pos[L] - 1) * len + 1, t = (pos[L] * len); for (int i = s; i <= t; ++i) paixu[i] = a[i]; sort(paixu + s, paixu + t + 1); for (int i = pos[L] + 1; i <= pos[R] - 1; ++i) tag[i] += val; for (int i = (pos[R] - 1) * len + 1; i <= R; ++i) a[i] += val; s = (pos[R] - 1) * len + 1, t = pos[R] * len; for (int i = s; i <= t; ++i) paixu[i] = a[i]; sort(paixu + s, paixu + t + 1); } inlineintxiaoyumougeshudegeshu(int L, int R, int who){ intres(-1), s(0), t(0); if (pos[L] == pos[R]) { for (int i = L; i <= R; ++i) if (a[i]+tag[pos[L]] < who) res = max(res, a[i]+tag[pos[L]]); return res; } s = L, t = pos[L]*len; for (int i = s; i <= t; ++i) if (a[i]+tag[pos[L]] < who) res = max(res, a[i]+tag[pos[L]]); for (int i = pos[L] + 1; i <= pos[R] - 1; ++i) { s = (i - 1) * len + 1, t = i * len; int now = lower_bound(paixu+s, paixu+t+1, (who-tag[i])) - paixu; -- now; res = (now==s-1)?res:(res>paixu[now]+tag[i])?res:paixu[now]+tag[i]; } s = (pos[R]-1)*len+1, t = R; for (int i = s; i <= t; ++i) if (a[i]+tag[pos[R]] < who) res = max(res, a[i]+tag[pos[R]]); return res; }
inlinevoidjiejuewentiyijishuchu(){ for (int i = 1; i <= n; ++i) { int opt, l, r, c; cin >> opt >> l >> r >> c; if (opt == 0) zaimougequjianjiashangyigeshu(l, r, c); else cout << xiaoyumougeshudegeshu(l, r, c) << '\n'; } }
#include<bits/stdc++.h> #define int long long // #define debug usingnamespace std;
constint N = 1e5+66;
int n, m, len, a[N]; int pos[N], sum[N], tag[N];
inlinevoidadd(int L, int R, int val){ int x = pos[L], y = pos[R]; if (x == y) { for (int i = L; i <= R; ++ i) a[i] += val; sum[x] += val*(R-L+1); return; } for (int i = L; i <= x*len; ++ i) a[i] += val; sum[x] += val*(x*len-L+1); for (int i = x+1; i <= y-1; ++ i) tag[i] += val; for (int i = (y-1)*len+1; i <= R; ++ i) a[i] += val; sum[y] += val*(R-((y-1)*len+1)+1); return; }
inlineintget_sum(int L, int R, int mod){ int x = pos[L], y = pos[R], res(0); if (x == y) { intres(0); for (int i = L; i <= R; ++ i) res += (tag[pos[i]]+a[i]); return res%mod; } for (int i = L; i <= x*len; ++ i) res += (tag[pos[i]]+a[i]); for (int i = x+1; i <= y-1; ++ i) res += sum[i] + tag[i]*len; for (int i = (y-1)*len+1; i <= R; ++ i) res += (tag[pos[i]]+a[i]); return res%mod; }
inlinevoidshuru(){ cin >> n; len = sqrt(n); for (int i = 1; i <= n; ++ i) { scanf ("%lld", &a[i]); pos[i] = (i-1)/len+1; sum[pos[i]] += a[i]; } }
inlinevoidjiejue(){ for (int i = 1; i <= n; ++ i) { int opt, l, r, c; scanf ("%lld%lld%lld%lld", &opt, &l, &r, &c); if (!opt) add(l, r, c); else cout << get_sum(l, r, c+1) << '\n'; } }
/* By TheStars Time:2020.02.02 */ #include<bits/stdc++.h> #define N 500009 usingnamespace std; //燕园情 千千结 int n, m, col[N], ans[N][3], cnt[N]; int fz, fm, block, len; structNode { int l, r; int ord; }e[N];