@KirinBill
2017-11-02T15:53:12.000000Z
字数 3947
阅读 1218
题解
套题
目录
long long
不溢出就好了。
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <algorithm>
using std::max;
const int MAXN=17;
int n;
long long ans;
int a[MAXN],b[MAXN],c[MAXN],d[MAXN];
void DFS(int cur,int oi,int whk){
if(cur==n+1){
ans=max(ans,(long long)oi*whk);
return;
}
DFS(cur+1,max(0,oi-b[cur]),whk+a[cur]);
DFS(cur+1,oi+c[cur],max(0,whk-d[cur]));
}
int main(){
#ifdef DEBUG
setIO("a");
#endif
read(n);
for(int i=1;i<=n;++i)
read(a[i]),read(b[i]),read(c[i]),read(d[i]);
DFS(1,0,0);
write(ans);
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
const int MAXN=2005;
int n,m,x1,y1,x2,y2;
int blk[MAXN][MAXN],all[MAXN][MAXN],le[MAXN][MAXN],up[MAXN][MAXN];
char G[MAXN][MAXN];
inline void prepare(){
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j)
blk[i][j]=blk[i-1][j]+blk[i][j-1]-blk[i-1][j-1]+(G[i][j]^'0');
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(G[i][j]=='1' && G[i][j-1]=='1')
++all[i][j],++le[i][j];
if(G[i][j]=='1' && G[i-1][j]=='1')
++all[i][j],++up[i][j];
le[i][j]+=le[i-1][j];
all[i][j]=all[i-1][j]+all[i][j-1]-all[i-1][j-1]+all[i][j];
}
}
for(int i=1;i<=m;++i){
for(int j=1;j<=n;++j)
up[j][i]+=up[j][i-1];
}
}
inline int solve(){
int ret=blk[x2][y2]-blk[x1-1][y2]-blk[x2][y1-1]+blk[x1-1][y1-1];
ret-=all[x2][y2]-all[x1-1][y2]-all[x2][y1-1]+all[x1-1][y1-1];
ret+=le[x2][y1]-le[x1-1][y1];
ret+=up[x1][y2]-up[x1][y1-1];
return ret;
}
int main(){
#ifdef DEBUG
setIO("b");
#endif
int q;
read(n),read(m),read(q);
for(int i=1;i<=n;++i)
scanf("%s",G[i]+1);
prepare();
for(int i=1;i<=q;++i){
read(x1),read(y1),read(x2),read(y2);
write(solve(),'\n');
}
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
const int MAXA=1e5+5;
int n,x1,a,mod;
class BIT{
private:
int c[MAXA];
int lowbit(int x){return x&-x;}
public:
void add(int l,int x){
for(;l<=a;l+=lowbit(l))
c[l]+=x;
}
int qry(int r){
int ret=0;
for(;r;r-=lowbit(r))
ret+=c[r];
return ret;
}
}ta;
inline long long solve(){
int i=1,x=x1;
while(x<mod) ++i,x+=a;
long long ret=0;
for(int tot=1,cnt=0,sum,tmp;i<=n;++i){
if(x>=mod){
x-=mod;
tot=1;
sum=ta.qry(x+1);
ta.add(x+1,1);
++cnt;
}
if(x>x1) tmp=(x-x1)/a+1;
else tmp=0;
tmp+=sum+(tot-1)*cnt;
ret+=i-tmp-1;
++tot,x+=a;
}
return ret;
}
int main(){
#ifdef DEBUG
setIO("c");
#endif
read(n),read(x1),read(a),read(mod);
write(solve());
return 0;
}