Submission #672072

# Submission time Handle Problem Language Result Execution time Memory
672072 2022-12-14T16:53:14 Z neki Izbori (COCI22_izbori) C++14
40 / 110
850 ms 524288 KB
#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;}
        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){
        if(ql>qr) return;
        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){
        if(ql>qr) return make_pair(0LL, 0LL);
        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){
        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);}
};

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){
        //ll tr=0;
        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;
            ans+=sg.get(cur-d-1, cur-2);
            //ll dod=sg.get(cur-d-1, cur-2);cout << "-enke:" << dod<<endl;
            //tr+=dod;//inclusive
            sg.update(cur-d, cur-1);
            cur-=d;
            ++cur;
            ans+=sg.get(cur-1, cur-1);
            //dod=sg.get(cur-1, cur-1); cout << "enka:" << dod<<endl;
            //tr+=dod;
            sg.update(cur, cur);
        }
        ll d=n+1-pos.back()-1;
        ans+=sg.get(cur-d-1, cur-2);
        //ll dod=sg.get(cur-d-1, cur-2);//cout << dod << endl;
        //tr+=dod;
        cur-=d;
        //cout << tr << endl;
        //ans+=tr;
        //cout << ans <<endl;
    }
    cout << ans << endl;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:74:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   74 |     for(auto [key, pos]: poss){
      |              ^
Main.cpp:79: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]
   79 |         for(ll i=0;i+1<pos.size();++i){
      |                    ~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 428 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 428 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 6 ms 4564 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 5 ms 1588 KB Output is correct
10 Correct 6 ms 1588 KB Output is correct
11 Correct 5 ms 1620 KB Output is correct
12 Correct 5 ms 1588 KB Output is correct
13 Correct 7 ms 1624 KB Output is correct
14 Correct 4 ms 1620 KB Output is correct
15 Correct 4 ms 1696 KB Output is correct
16 Correct 5 ms 1620 KB Output is correct
17 Correct 4 ms 568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 3800 KB Output is correct
2 Correct 201 ms 4856 KB Output is correct
3 Correct 126 ms 2992 KB Output is correct
4 Correct 223 ms 5048 KB Output is correct
5 Correct 231 ms 5184 KB Output is correct
6 Correct 243 ms 5556 KB Output is correct
7 Correct 260 ms 5540 KB Output is correct
8 Correct 237 ms 5560 KB Output is correct
9 Correct 293 ms 5508 KB Output is correct
10 Correct 255 ms 5488 KB Output is correct
11 Correct 254 ms 36620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 428 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 6 ms 4564 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 5 ms 1588 KB Output is correct
10 Correct 6 ms 1588 KB Output is correct
11 Correct 5 ms 1620 KB Output is correct
12 Correct 5 ms 1588 KB Output is correct
13 Correct 7 ms 1624 KB Output is correct
14 Correct 4 ms 1620 KB Output is correct
15 Correct 4 ms 1696 KB Output is correct
16 Correct 5 ms 1620 KB Output is correct
17 Correct 4 ms 568 KB Output is correct
18 Correct 145 ms 3800 KB Output is correct
19 Correct 201 ms 4856 KB Output is correct
20 Correct 126 ms 2992 KB Output is correct
21 Correct 223 ms 5048 KB Output is correct
22 Correct 231 ms 5184 KB Output is correct
23 Correct 243 ms 5556 KB Output is correct
24 Correct 260 ms 5540 KB Output is correct
25 Correct 237 ms 5560 KB Output is correct
26 Correct 293 ms 5508 KB Output is correct
27 Correct 255 ms 5488 KB Output is correct
28 Correct 254 ms 36620 KB Output is correct
29 Correct 323 ms 38232 KB Output is correct
30 Correct 400 ms 244384 KB Output is correct
31 Correct 778 ms 498036 KB Output is correct
32 Runtime error 850 ms 524288 KB Execution killed with signal 9
33 Halted 0 ms 0 KB -