답안 #49206

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
49206 2018-05-23T18:29:25 Z Benq 케이크 (CEOI14_cake) C++14
0 / 100
850 ms 11560 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;

int D[1<<18];

struct {
    pair<int,pi> seg[1<<19];
    pi query(int x, int ind = 1, int l = 0, int r = (1<<18)-1) {
        pi t = {seg[ind].f,seg[ind].s.f*x+seg[ind].s.s};
        if (l == r) return t;
        
        int m = (l+r)/2;
        if (x <= m) return max(t,query(x,2*ind,l,m));
        return max(t,query(x,2*ind+1,m+1,r));
    }
    void upd(int lo, int hi, pair<int,pi> v, int ind = 1, int l = 0, int r = (1<<18)-1) {
        if (r < lo || hi < l) return;
        if (lo <= l && r <= hi) {
            seg[ind] = v;
            return;
        }
        int m = (l+r)/2;
        upd(lo,hi,v,2*ind,l,m);
        upd(lo,hi,v,2*ind+1,m+1,r);
    }
} Seg;

int N, Q, a;
vpi cur, highest;
int z = 1;

bool contains(pi x, int y) {
    return min(x.f,x.s) <= y && y <= max(x.f,x.s);
}

void se(int l, int r, int& t) {
    cur.pb({l,r});
    if (l <= r) Seg.upd(l,r,{++z,{1,t-l}});
    else Seg.upd(r,l,{++z,{-1,t+l}});
    t += abs(l-r)+1;
}

void renumerate(int x, int y) {
    for (auto& a: highest) if (a.s >= y) a.s ++;
    int ind = 0; while (ind < sz(highest) && highest[ind].f < x) ind ++;
    highest.insert(highest.begin()+ind,{x,y});
    if (ind != sz(highest)-1 && highest[ind+1].f == x) {
        int t = highest[ind+1].s;
        highest.erase(highest.begin()+ind+1);
        for (auto& a: highest) if (a.s > t) a.s --;
    }
    if (sz(highest) > 10) F0R(i,sz(highest)) if (highest[i].s == 11) highest.erase(highest.begin()+i);
}

void mod(int x, int y) {
    renumerate(x,y);
    if (x == a) return;
    
    int lo = 0, hi = N-1, t = N;
    
    while (!contains(cur.back(),x)) {
        t -= abs(cur.back().f-cur.back().s)+1;
        if (cur.back().s < a) lo = cur.back().f+1;
        else hi = cur.back().f-1;
        cur.pop_back();
    }
    
    t -= abs(x-cur.back().s)+1;
    if (cur.back().s < a) {
        cur.back().s = lo = x+1;
        if (cur.back().s > cur.back().f) cur.pop_back();
    } else {
        cur.back().s = hi = x-1;
        if (cur.back().s < cur.back().f) cur.pop_back();
    }
    
    int l = sz(highest)-1, r = 0;
    while (l >= 0 && highest[l].f >= lo) l --;
    while (r < sz(highest) && highest[r].f <= hi) r ++;
    
    while (lo > 0 && hi < N-1) {
        if (l == -1) {
            se(lo-1,0,t); lo = 0;
        } else if (r == sz(highest)) {
            se(hi+1,N-1,t); hi = N-1;
        } else if (highest[l].s < highest[r].s) {
            int HI = ((r+1 == sz(highest)) ? N-1 : highest[r+1].f-1);
            se(hi+1,HI,t);
            hi = HI; r ++;
        } else {
            int LO = ((l == 0) ? 0 : highest[l-1].f+1);
            se(lo-1,LO,t);
            lo = LO; l --;
        }
    }
    
    // for (auto a: cur) cout << a.f << " " << a.s << " | ";
    // cout << "\n----\n";
    if (hi < N-1) se(hi+1,N-1,t);
    if (lo > 0) se(lo-1,0,t);

    /*cout << lo << " " << t << "\n";
    for (auto a: cur) cout << a.f << " " << a.s << " | ";
    cout << "HI " << Seg.query(1).f << " " << Seg.query(1).s << "\n";
    cout << "\n----\n";*/
}

void init() {
    int l = a, r = a;
    int co = 0;
    while (r-l != N-1) {
        if (r == N-1 || (l != 0 && D[l-1] < D[r+1])) {
            l --; cur.pb({l,l});
            Seg.upd(l,l,{1,{0,++co}});
        } else {
            r ++; cur.pb({r,r});
            Seg.upd(r,r,{1,{0,++co}});
        }
    }
    F0R(i,N) if (D[i] > N-10) highest.pb({i,N+1-D[i]});
    sort(all(highest));
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> a; a --;
    F0R(i,N) {
        // D[i] = i+1;
        cin >> D[i];
    }
    // random_shuffle(D,D+N);
    cin >> Q;
    
    init();
    F0R(i,Q) {
        char c; cin >> c;
        if (c == 'F') {
            int x; cin >> x;
            cout << Seg.query(x-1).s << "\n";
        } else {
            int x, y; cin >> x >> y;
            mod(x-1,y);
        }
    }
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Incorrect 4 ms 380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 508 ms 976 KB Output isn't correct
2 Correct 773 ms 1108 KB Output is correct
3 Incorrect 681 ms 1108 KB Output isn't correct
4 Correct 459 ms 1108 KB Output is correct
5 Incorrect 578 ms 1604 KB Output isn't correct
6 Correct 591 ms 1736 KB Output is correct
7 Correct 850 ms 1736 KB Output is correct
8 Correct 558 ms 1736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 66 ms 4308 KB Output isn't correct
2 Correct 61 ms 4308 KB Output is correct
3 Correct 80 ms 4308 KB Output is correct
4 Correct 3 ms 4308 KB Output is correct
5 Incorrect 140 ms 8084 KB Output isn't correct
6 Incorrect 127 ms 8084 KB Output isn't correct
7 Correct 103 ms 8084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 50 ms 8084 KB Output isn't correct
2 Incorrect 54 ms 8084 KB Output isn't correct
3 Incorrect 106 ms 8084 KB Output isn't correct
4 Incorrect 86 ms 8084 KB Output isn't correct
5 Incorrect 121 ms 8084 KB Output isn't correct
6 Incorrect 139 ms 8084 KB Output isn't correct
7 Incorrect 140 ms 8084 KB Output isn't correct
8 Incorrect 256 ms 8084 KB Output isn't correct
9 Incorrect 623 ms 11524 KB Output isn't correct
10 Incorrect 424 ms 11524 KB Output isn't correct
11 Incorrect 470 ms 11524 KB Output isn't correct
12 Incorrect 745 ms 11524 KB Output isn't correct
13 Incorrect 542 ms 11560 KB Output isn't correct