Submission #672081

#TimeUsernameProblemLanguageResultExecution timeMemory
672081nekiIzbori (COCI22_izbori)C++14
110 / 110
978 ms36320 KiB
#include <bits/stdc++.h> #define ll long long #define vc vector using namespace std; struct segtr{ struct node{ node* lc=0,* rc=0; ll sum=0,sum2=0, upd=0, l, r, mid; node(ll l_, ll r_):l(l_), r(r_){mid=l+r;if(mid<0)--mid;mid/=2;} ~node(){if(lc)delete lc; if(rc)delete rc;} inline ll getlen(){return r-l+1;} inline ll getsum(){return sum+upd*getlen();} inline ll getsum2(){return sum2+upd*getlen()*(getlen()+1)/2;} void push(){ if(!lc) lc=new node(l, mid); if(!rc) rc=new node(mid+1, r); if(upd){lc->upd+=upd, rc->upd+=upd;upd=0;} } void updsums(){ sum=lc->getsum()+rc->getsum(); sum2=lc->getsum2()+lc->getsum()*rc->getlen()+rc->getsum2(); } }; node* root; ll n; void update_w(ll ql, ll qr, node* no){ assert(ql<=qr && -n<=ql && qr<=n); if(ql==no->l && qr==no->r) ++no->upd; else{ no->push(); if(qr<=no->mid) update_w(ql, qr, no->lc); else if(no->mid<ql) update_w(ql, qr, no->rc); else update_w(ql, no->mid, no->lc), update_w(no->mid+1, qr, no->rc); no->updsums(); } } void update(ll ql, ll qr){update_w(ql, qr, root);} pair<ll, ll> query(ll ql, ll qr, node* no){ assert(ql<=qr && -n<=ql && qr<=n); if(ql==no->l && qr==no->r) return make_pair(no->getsum(), no->getsum2()); else{ pair<ll, ll> ret; no->push(); if(qr<=no->mid) ret=query(ql, qr, no->lc); else if(no->mid<ql) ret=query(ql, qr, no->rc); else{ auto r1=query(ql, no->mid, no->lc); auto r2=query(no->mid+1, qr, no->rc); ret=make_pair(r1.first+r2.first, r1.second+r1.first*(qr-no->mid)+r2.second); } no->updsums(); return ret; } } ll get(ll ql, ll qr){ assert(ql<=qr); assert(-n<=ql); assert(qr<=n); return query(-n, ql-1, root).first*(qr-ql+1)+query(ql, qr, root).second; } segtr(ll n_): n(n_){root=new node(-n, n);} ~segtr(){ delete root; } }; int main() { segtr sg(10); sg.update(2, 6); //cout << sg.query(3, 6, sg.root).second<<endl; //cout << sg.query(-1, 7, sg.root).first<<endl; ll n; cin >> n; vc<ll> a(n+1);for(ll i=1;i<=n;++i) cin >> a[i]; map<ll, vc<ll>> poss; for(ll i=1;i<=n;++i) poss[a[i]].push_back(i); ll ans=0; for(auto [key, pos]: poss){ segtr sg(n+4); pos.insert(pos.begin(), 0); ll cur=0;sg.update(0, 0); for(ll i=0;i+1<pos.size();++i){ ll d=pos[i+1]-pos[i]-1; assert(d>=0); if(d>0)ans+=sg.get(cur-d-1, cur-2), sg.update(cur-d, cur-1); cur-=d; ++cur; ans+=sg.get(cur-1, cur-1); sg.update(cur, cur); } ll d=n+1-pos.back()-1; if(d>0)ans+=sg.get(cur-d-1, cur-2); cur-=d; } cout << ans << endl; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:81:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   81 |     for(auto [key, pos]: poss){
      |              ^
Main.cpp:85:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for(ll i=0;i+1<pos.size();++i){
      |                    ~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...