Submission #1303276

#TimeUsernameProblemLanguageResultExecution timeMemory
1303276hainam2k9Fish 3 (JOI24_fish3)C++20
100 / 100
450 ms103976 KiB
#include <bits/stdc++.h> #define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0) #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout) #define ll long long #define ull unsigned long long #define i128 __int128 #define db long double #define sz(a) ((int)(a).size()) #define pb emplace_back #define pf emplace_front #define pob pop_back #define pof pop_front #define lb lower_bound #define ub upper_bound #define fi first #define se second #define ins emplace #define mp make_pair using namespace std; const int MOD = 1e9+7, MAXN = 3e5+5; const string NAME = ""; int n,q; ll d,a[MAXN],b[MAXN],pre[MAXN],st[20][MAXN],sum[MAXN],rs[MAXN]; vector<pair<int,int>> v[MAXN]; void build(){ for(int i = 1; i<=n; ++i) st[0][i]=pre[i]-a[i]; for(int i = 1; i<=__lg(n); ++i) for(int j = 1; j+(1<<i)-1<=n; ++j) st[i][j]=max(st[i-1][j],st[i-1][j+(1<<(i-1))]); } ll rmq(int l, int r){ int k=__lg(r-l+1); return max(st[k][l],st[k][r-(1<<k)+1]); } struct node{ ll MIN=1e18,MAX=1e18,sum=0,lazy=1e18; }sgt[MAXN<<2]; void down(int id, int l, int r){ ll& val=sgt[id].lazy; if(val==1e18) return; int mid=(l+r)>>1; sgt[id<<1].MIN=sgt[id<<1].MAX=sgt[id<<1].lazy=val, sgt[id<<1].sum=val*(mid-l+1); sgt[id<<1|1].MIN=sgt[id<<1|1].MAX=sgt[id<<1|1].lazy=val, sgt[id<<1|1].sum=val*(r-mid); val=1e18; } void update(int id, int l, int r, int u, int v, ll val){ if(v<l||r<u||sgt[id].MAX<=val) return; if(u<=l&&r<=v&&sgt[id].MIN>val){ sgt[id].MIN=sgt[id].MAX=sgt[id].lazy=val, sgt[id].sum=val*(r-l+1); return; } down(id,l,r); int mid=(l+r)>>1; update(id<<1,l,mid,u,v,val); update(id<<1|1,mid+1,r,u,v,val); sgt[id].MIN=min(sgt[id<<1].MIN,sgt[id<<1|1].MIN); sgt[id].MAX=max(sgt[id<<1].MAX,sgt[id<<1|1].MAX); sgt[id].sum=sgt[id<<1].sum+sgt[id<<1|1].sum; } ll getsum(int id, int l, int r, int u, int v){ if(v<l||r<u) return 0; if(u<=l&&r<=v) return sgt[id].sum; down(id,l,r); int mid=(l+r)>>1; return getsum(id<<1,l,mid,u,v)+getsum(id<<1|1,mid+1,r,u,v); } int main() { tt; if(fopen((NAME + ".INP").c_str(), "r")) fo; cin >> n >> d; for(int i = 1; i<=n; ++i) cin >> a[i]; for(int i = 1; i<=n; ++i){ b[i]=a[i]%d; b[i]-=a[i-1]%d; if(b[i]<0) b[i]+=d; pre[i]=pre[i-1]+b[i]; } build(); for(int i = 1; i<=n; ++i) a[i]-=pre[i], a[i]/=d, sum[i]=sum[i-1]+a[i]; cin >> q; for(int i = 1; i<=q; ++i){ int l,r; cin >> l >> r; if(l==r) rs[i]=0; else if(rmq(l+1,r)>pre[l]) rs[i]=-1; else v[r].pb(l,i), rs[i]=sum[r]-sum[l-1]; } for(int i = 1; i<=n; ++i){ update(1,1,n,1,i,a[i]); for(pair<int,int>& p : v[i]) rs[p.se]-=getsum(1,1,n,p.fi,i); } for(int i = 1; i<=q; ++i) cout << rs[i] << "\n"; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:45: note: in expansion of macro 'fo'
   71 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
Main.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:45: note: in expansion of macro 'fo'
   71 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...