QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#218102#7079. ArrayKLPP#WA 471ms11860kbC++203.3kb2023-10-17 18:32:282023-10-17 18:32:29

Judging History

你现在查看的是最新测评结果

  • [2023-10-17 18:32:29]
  • 评测
  • 测评结果:WA
  • 用时:471ms
  • 内存:11860kb
  • [2023-10-17 18:32:28]
  • 提交

answer

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
int n;
lld arr[2000000];
int nxt[2000000];
int prv[2000000];
map<lld,int> m;
set<lld> s;
lld sm(lld x, lld y){
    lld ans=(y-x+1);
    ans*=(x+y);
    ans/=2;
    return ans;
}
vector<lld >V;
const int MX=2'000'000;
class ST{
    lld mn[MX*4];
    public:
    void build(int a, int b, int node){
        mn[node]=0;
        if(a==b)return;
        int mid=(a+b)/2;
        build(a,mid,2*node);
        build(mid+1,b,2*node+1);
    }
    void update(int a, int b, int node, int pos, lld val){
        if(pos<a || b<pos)return;
        if(a==b){
            mn[node]=val;
            return;
        }
        int mid=(a+b)/2;
        update(a,mid,2*node,pos,val);
        update(mid+1,b,2*node+1,pos,val);
        mn[node]=min(mn[2*node],mn[2*node+1]);
    }
    lld query(int a, int b, int node, int l, int r){
        if(r<a || b<l)return 1e18;
        if(l<=a && b<=r)return mn[node];
        int mid=(a+b)/2;
        return min(query(a,mid,2*node,l,r),query(mid+1,b,2*node+1,l,r));
    }
};
ST S1,S2;
void solve(){
    V.clear();
    m.clear();
    s.clear();
    cin>>n;
    rep(i,0,n)cin>>arr[i];
    rep(i,0,n)V.push_back(arr[i]);
    sort(V.begin(),V.end());
    V.resize(unique(V.begin(),V.end())-V.begin());
    int M=V.size();
    S1.build(0,M-1,1);
    S2.build(0,M-1,1);
    for(int i=n-1;i>-1;i--){
        if(m.find(arr[i])==m.end())nxt[i]=n;
        else nxt[i]=m[arr[i]];
        m[arr[i]]=i;
    }
    m.clear();
    rep(i,0,n){
        if(m.find(arr[i])==m.end())prv[i]=-1;
        else prv[i]=m[arr[i]];
        m[arr[i]]=i;
    }
    trav(a,m){
        lld X=a.second;
        S1.update(0,M-1,1,lower_bound(V.begin(),V.end(),a.first)-V.begin(),(X*(X+1))/2-a.first);
        S2.update(0,M-1,1,lower_bound(V.begin(),V.end(),a.first)-V.begin(),(X*(X+1))/2+a.first);
    }
    lld best=0;
    rep(i,0,n){
        S1.update(0,M-1,1,lower_bound(V.begin(),V.end(),arr[i])-V.begin(),1e18);
        S2.update(0,M-1,1,lower_bound(V.begin(),V.end(),arr[i])-V.begin(),1e18);
        if(prv[i]==-1){
            if((int)s.size()>0){
                set<lld>::iterator it=s.lower_bound(arr[i]);
                if(it!=s.end()){
                    best=min(best,*it-arr[i]-sm(i+1,nxt[i]));
                }
                if(it!=s.begin()){
                    best=min(best,arr[i]-*prev(it)-sm(i+1,nxt[i]));
                }
            }
            lld I=nxt[i];
            best=min(best,S1.query(0,M-1,1,0,lower_bound(V.begin(),V.end(),arr[i])-V.begin())+arr[i]-(I*(I+1))/2);
            best=min(best,S2.query(0,M-1,1,lower_bound(V.begin(),V.end(),arr[i])-V.begin(),M-1)-arr[i]-(I*(I+1))/2);
        }
        s.insert(arr[i]);
    }
    s.clear();
    lld ans=0;
    rep(i,0,n){
        s.insert(arr[i]);
        lld can=i+1;
        can*=s.size();
        ans+=can;
    }
    cout<<ans+best<<"\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	cin>>tt;
	while(tt--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11860kb

input:

1
4
1 2 3 4

output:

22

result:

ok "22"

Test #2:

score: 0
Accepted
time: 206ms
memory: 11860kb

input:

100000
10
873324878 873324878 873324878 873324878 891656676 891656676 615245360 873324878 873324878 873324878
10
560723194 560723194 797429144 797429144 560723194 797429144 819647695 560723194 797429144 560723194
10
750627649 746781323 756277046 756277046 750627649 750627649 756277046 750627649 9142...

output:

134
141
180
168
109
95
181
185
144
149
202
158
161
107
210
210
255
104
210
109
82
158
149
201
154
109
206
158
161
161
158
134
206
198
161
109
143
152
183
156
171
201
149
104
210
161
181
152
152
250
185
243
206
158
152
128
147
203
225
143
203
198
206
201
158
109
114
100
183
161
162
183
154
222
181
14...

result:

ok 100000 tokens

Test #3:

score: 0
Accepted
time: 352ms
memory: 11784kb

input:

39978
23
512481863 596944631 624383245 441725511 441725511 594496544 441725511 624383245 698754670 596944631 182448912 636350614 596944631 391310300 624383245 391310300 698754670 596944631 441725511 182448912 826649520 351713941 596944631
24
789886131 679943285 874352131 191233114 214841280 21484128...

output:

2322
2416
4226
1974
3362
1631
2373
2521
3618
4540
2589
2104
2692
1407
2757
2394
3938
2106
2769
3209
3418
3828
2339
3954
5404
3191
1734
4260
2178
1354
2829
2552
2820
2385
3102
3442
3459
3027
3631
2399
1682
4564
4494
4217
3756
4176
3290
4109
1702
3740
4220
4117
2118
1840
4223
3499
3834
2472
5465
1636
...

result:

ok 39978 tokens

Test #4:

score: -100
Wrong Answer
time: 471ms
memory: 11816kb

input:

10000
100
70652501 126219335 870011044 503878453 331807482 42366188 570696778 892481058 11179909 898060545 596710776 892481058 892481058 126219335 938507063 540380652 869222706 898060545 380041360 643567581 977928808 655190742 75768776 126219335 386687451 513015608 898060545 540380652 798597796 7794...

output:

156439
177539
142728
105896
98255
145550
158303
139241
142500
123767
164463
173494
162837
133032
161874
108825
159157
176582
143686
146818
161864
166700
93293
121994
166017
96652
176532
106752
141748
145107
147984
139577
153293
146263
112899
130505
178781
186526
162804
138862
106517
170352
136899
13...

result:

wrong answer 959th words differ - expected: '140167', found: '140748'