| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1330655 | herominhsteve | Sum Zero (RMI20_sumzero) | C++20 | 299 ms | 22524 KiB |
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "SUMZERO"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9 + 7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(FNAME".inp","r")){
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
const int MAXN = 400000 + 5;
const int K = 5;
int n, q;
int goArr[MAXN];
int up[MAXN][K];
pair<int,int> queries[MAXN];
void init(){
cin>>n;
for (int i = 0; i <= n; i++) {
goArr[i] = -1;
for (int k = 0; k < K; k++) up[i][k] = -1;
}
unordered_map<long long,int> last;
last.reserve((size_t)n * 2 + 10);
last.max_load_factor(0.7f);
long long sum = 0;
last[0] = 0;
for (int i = 1; i <= n; i++) {
int x; cin>>x;
sum += x;
unordered_map<long long,int>::iterator it = last.find(sum);
if (it != last.end()){
goArr[it->second] = i;
}
last[sum] = i;
}
cin>>q;
int mn = n + 1;
for (int pos = n; pos >= 0; pos--) {
if (goArr[pos] != -1) mn = min(mn, goArr[pos]);
if (mn <= n) up[pos][0] = mn;
else up[pos][0] = -1;
}
for (int k = 1; k < K; k++) {
for (int pos = 0; pos <= n; pos++) {
int x = pos;
for (int t = 0; t < 16; t++) {
if (x == -1) break;
x = up[x][k - 1];
}
up[pos][k] = x;
}
}
}
void sol(){
while (q--) {
int L,R; cin>>L>>R;
int now = L - 1;
int res = 0;
for (int k = K - 1; k >= 0; ) {
int nx = up[now][k];
if (~nx and nx <= R) {
now = nx;
res += (1<<(4 * k));
} else {
k--;
}
}
cout<<res<<el;
}
}
int main(){
setup();
init();
sol();
return 0;
}컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
