QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104407 | #5374. 数圈 | AvavaAva | 60 | 271ms | 17476kb | C++14 | 3.2kb | 2023-05-10 17:06:09 | 2023-05-10 17:06:10 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[100005];
struct qwq
{
int op,x,y,z;
}c[1000005];
int ans;
bool cmp(qwq x,qwq y)
{
return x.x>y.x||x.x==y.x&&x.op<y.op;
}
bool cmpy(qwq x,qwq y)
{
return x.y<y.y||x.y==y.y&&x.op<y.op;
}
int cntop=0;
struct BIT
{
int tree[500005];
void init()
{
memset(tree,0,sizeof tree);
}
int lowbit(int x)
{
return x&-x;
}
void add(int x,int y)
{
for(x+=5;x<=500000;x+=lowbit(x)) tree[x]+=y;
}
int ask(int x)
{
int rtn=0;
for(x+=5;x;x^=lowbit(x)) rtn+=tree[x];
return rtn;
}
}T;
const int inf=8e18;
int Div(int x,int d)
{
return (x+inf/d*d)/d-inf/d;
}
vector<int> S;
int rk(int x)
{
return lower_bound(S.begin(),S.end(),x)-S.begin();
}
void SOLVE(int l,int r)
{
if(l>=r) return ;
int mid=(l+r)/2;
SOLVE(l,mid),SOLVE(mid+1,r);
vector<qwq> v;
for(int i=l;i<=mid;i++)
if(c[i].op==0) v.push_back(c[i]);
for(int i=mid+1;i<=r;i++)
if(c[i].op!=0) v.push_back(c[i]);
sort(v.begin(),v.end(),cmpy);
vector<int> clr;
for(auto t:v)
{
int R=rk(t.z);
if(t.op==0) T.add(R,1),clr.push_back(R);
else ans+=T.ask(R);
}
for(auto t:clr) T.add(t,-1);
}
void solve()
{
cntop=0;
int n;
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++) a[i]+=a[i-1];
if(a[n]<0)
{
cout << "-1\n";
return ;
}
if(a[n]==0)
{
int flag=0;
for(int i=1;i<=n;i++) if(a[i]) flag=1;
if(flag)
{
cout << "-1\n";
return ;
}
cout << "0\n";
return ;
}
int d=a[n];
ans=0;
int MN=0;
for(int i=1;i<=n;i++) MN=min(MN,a[i]);
for(int i=1;i<=n;i++) a[i]-=MN;
for(int i=1;i<=n;i++)
c[++cntop]={0,i,a[i]%d,a[i]};
for(int i=1;i<=n;i++) S.push_back(a[i]-1),S.push_back(a[i]),S.push_back(a[i]-2),S.push_back(a[i]+1);
sort(S.begin(),S.end());
BIT T,TT;
T.init(),TT.init();
for(int i=n;i>=1;i--)
{
//y<x
int x=a[i]+d-1;
int k=(x)%d;
if(k<0) k+=d;
int w=Div((x),d);
c[++cntop]={2,i+1,k,a[i]-1};
ans+=T.ask(rk(a[i]-1))*(w-1);
ans+=TT.ask(rk(a[i]-1));
T.add(rk(a[i]),1);
TT.add(rk(a[i]),-(a[i])/d);
}
T.init(),TT.init();
for(int i=n;i>=1;i--)
{
//y>x+d
int x=a[i];
int k=(x+1)%d;
if(k<0) k+=d;
int w=Div((x+1),d)+1;
c[++cntop]={1,i+1,k,x+1};
ans-=(T.ask(400000)-T.ask(rk(a[i])))*(w);
ans+=(TT.ask(400000)-TT.ask(rk(a[i])));
T.add(rk(a[i]),1);
TT.add(rk(a[i]),(a[i])/d);
}
int qwq=0;
sort(c+1,c+cntop+1,cmp);
int nw=n;
T.init(),TT.init();
for(int i=cntop;i>=1;i--)
{
if(c[i].op==1)
{
ans+=n-c[i].x+1;
for(int j=c[i].x;j<=n;j++)
{
if(a[j]%d<c[i].y) --ans;
}
for(int j=c[i].x;j<=n;j++)
{
if(a[j]<c[i].z) --ans;
}
c[i].op=2,c[i].y--,c[i].z--;
}
}
/* for(int i=1;i<=cntop;i++)
{
if(c[i].op==2) --c[i].x;
}*/
/* for(int i=1;i<=cntop;i++)
{
if(c[i].op==2)
{
for(int j=c[i].x;j<=n;j++)
{
if((a[j]%d<=c[i].y)&&(a[j]<=c[i].z)) ++ans;
}
}
}*/
sort(c+1,c+cntop+1,cmp);
SOLVE(1,cntop);
cout << ans << "\n";
}
signed main()
{
// freopen("circle4.in","r",stdin);
// freopen("circle.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int greg;
cin >> greg;
while(greg--) solve();
return 0;
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 5ms
memory: 17476kb
input:
10 3 2 -9 -4 3 4 6 0 3 3 -10 0 3 7 -6 3 3 -6 7 10 3 -3 9 -2 3 6 1 -2 3 5 2 -2 3 -9 -5 7 3 -4 -5 6
output:
-1 0 -1 3 1 4 2 1 -1 -1
result:
ok 10 lines
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #2:
score: 20
Accepted
time: 10ms
memory: 15308kb
input:
10 50 -5 -5 3 8 0 0 3 8 5 -7 6 -9 5 5 2 7 -9 1 -7 5 0 10 -6 -2 -6 -8 9 7 4 3 -9 9 5 9 8 1 7 0 0 -5 -1 -3 5 -7 5 -4 6 8 -1 1 50 -6 4 3 -6 -9 -8 -5 -2 -10 7 -4 1 -1 -5 2 1 -10 8 8 7 -8 -5 3 -6 10 3 2 -1 10 0 4 -6 9 3 -6 3 9 -4 4 -2 -3 4 -9 -7 7 1 5 2 -4 5 50 2 -10 5 3 -1 1 9 -1 -5 3 -2 -10 7 0 5 -5 -1...
output:
161 -1 249 -1 21873 -1 452 -1 -1 314
result:
ok 10 lines
Subtask #3:
score: 30
Accepted
Dependency #2:
100%
Accepted
Test #3:
score: 30
Accepted
time: 271ms
memory: 16468kb
input:
10 2000 -4438 -448 2902 3873 -5348 1821 -5284 2787 -1369 -4712 3298 2808 1651 -4568 4377 870 2217 -2683 1217 120 -3854 1156 -2129 -3757 -2704 3026 -1745 -5327 -1315 405 3944 340 -1510 2213 -24 -32 -5414 -2330 760 3715 -4871 2831 1917 3148 1360 -3662 -4281 -1248 788 1334 -3401 2050 4174 3163 -2456 33...
output:
90206708583 9272643195 2640993721 148400379 20504656 2904294 -1 6666669000000 61998 67150
result:
ok 10 lines
Subtask #4:
score: 0
Time Limit Exceeded
Test #4:
score: 0
Time Limit Exceeded
input:
10 30000 -3879 -556 4570 1863 2815 -4010 2471 -270 2835 3071 -3331 -1251 -2243 4221 -5249 -4134 3376 1978 858 2545 -4207 386 3875 2029 1706 1119 3065 -3097 4399 4385 -3021 2473 2506 2157 3946 -886 3929 1478 2728 -4239 4091 -151 -4762 -2136 -1424 2162 -669 267 190 -1180 2640 -757 -2078 -1409 3165 216...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
0%