이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
using namespace std;
const int N = 4e5 + 5;
int a[N],p[N],last[N];
int ans[N];
vector <int> v[N],path;
vector <pii> qr[N];
bool vis[N];
void dfs(int x){
vis[x]=1;
path.pb(x);
for (auto [y,i]: qr[x]){
int l = 0,r = (int)path.size() - 1,res=-1;
while (l <= r){
int mid = (l + r)/2;
if (path[mid] <= y){
res = mid;
r = mid - 1;
}else{
l = mid + 1;
}
}
ans[i] = (int)path.size() - res - 1;
}
for (int to: v[x])
dfs(to);
path.pop_back();
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
vector <int> vec;
map <int,int> mp;
vec.pb(0);
for(int i=1;i<=n;i++){
cin>>a[i];
}
for (int i = n; i >= 1; i--){
p[i]=p[i+1]+a[i];
vec.pb(p[i]);
}
sort(vec.begin(),vec.end());
int val=0;
for (int i=0;i<vec.size();i++){
if (!i || vec[i] != vec[i - 1]) {
++val;
mp[vec[i]] = val;
}
}
for (int i = 1; i <= n+1; i++)
p[i] = mp[p[i]],last[p[i]] = 1e9;
last[mp[0]] = n + 1;
int k = 1e9;
for (int i = n; i >= 1; i--){
k = min(k,last[p[i]]);
if (k != 1e9) v[k].pb(i);
last[p[i]] = i;
}
int q;
cin>>q;
for (int i = 1; i <= q; i++){
int l,r;
cin>>l>>r;
qr[l].pb({r + 1,i});
}
for (int i = n+1; i >= 1; i--){
if (!vis[i]) dfs(i);
}
for (int i = 1; i <= q; i++)
cout<<ans[i]<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
sumzero.cpp: In function 'int main()':
sumzero.cpp:63:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i=0;i<vec.size();i++){
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |