Submission #146436

# Submission time Handle Problem Language Result Execution time Memory
146436 2019-08-24T03:42:34 Z 12tqian Cake (CEOI14_cake) C++14
0 / 100
1223 ms 15464 KB
#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#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;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

const double PI = 4 * atan(1);

#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>

#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)

template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
  cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}

void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
void io(string taskname){
    string fin = taskname + ".in";
    string fout = taskname + ".out";
    const char* FIN = fin.c_str();
    const char* FOUT = fout.c_str();
    freopen(FIN, "r", stdin);
    freopen(FOUT, "w", stdout);
    fast_io();
}
struct node{
    int nxt, bef, val;
};
const int MAX = 250005;
const int BIG = 10;
int n, a;

node ord[MAX];
int link[MAX];
int beg[MAX];
int head, tail;
void del(int x){
    if(ord[x].val == tail){
        ord[(ord[x].bef)].nxt = head;
        tail = ord[x].bef;
        ord[head].bef = tail;
    }
    else if(ord[x].val == head){
        ord[(ord[x].nxt)].bef = tail;
        head = ord[x].nxt;
        ord[tail].bef = head;
    }
    else{
        ord[(ord[x].bef)].nxt = ord[x].nxt;
        ord[(ord[x].nxt)].bef = ord[x].bef;
    }
}
vi check;
vi num;
void redo(int i, int e){
    if(i == a) return;
    int mn = e;
    f0r(j, sz(check)){
        if(i<a){
            if(check[j]>a) continue;
            if(i<check[j]){
                mn = min(mn, num[j]);
            }
        }
        else{
            if(check[j]<a) continue;
            if(i>check[j]){
                mn = min(mn, num[j]);
            }
        }
    }
   // if(i == 3){cout << mn <<" min" << endl;}
    if(i<a) link[i] = n;
    else link[i] = -1;
    f0r(j, sz(check)){
        if(i<a){
            if(check[j]<a) continue;
            if(link[i]>check[j] && num[j]<mn) link[i] = check[j];

        }
        else{
            if(check[j]>a) continue;
            if(link[i]<check[j] && num[j]<mn) link[i] = check[j];
        }
    }

}
void ins(int x, int r){
    del(x);
    int cur = head;
    if(r == 0){
        int rep = head;
        ord[x].nxt = rep;
        ord[x].bef = tail;
        ord[rep].bef = ord[x].val;
        head = ord[x].val;
        ord[tail].nxt = head;
    }
    else if(r == n-1){
        int rep = tail;
        ord[x].bef = rep;
        ord[x].nxt = head;
        ord[rep].nxt = ord[x].val;
        tail = ord[x].val;
        ord[head].bef = tail;
    }
    else{
        int rep = head;

        f0r(i, r){
            rep = ord[rep].nxt;
        }
        ord[x].nxt = rep;
        ord[x].bef = ord[rep].bef;
        ord[ord[rep].bef].nxt = ord[x].val;
        ord[rep].bef = ord[x].val;

    }
}
int main(){
    fast_io();
    cin >> n >> a;
    a--;
    vpi d;
    f0r(i, n){
        int di;
        cin >> di;
        d.eb(mp(di, i));
        beg[i] = di;
    }
    sort(all(d));
    reverse(all(d));
    f0r(i, n){
        ord[i].val = i;
        ord[i].nxt = -1;
        ord[i].bef = -1;
    }
    f0r(i, sz(d)){
        int j = (i+1)%n;
        ord[d[i].s].nxt = ord[d[j].s].val;
        ord[d[j].s].bef = ord[d[i].s].val;
        if(i == 0) head = ord[d[i].s].val;
        if(i == sz(d)-1) tail = ord[d[i].s].val;
    }
    int cnt = head;
    while(true){
        if(ord[cnt].nxt == head) break;
        cnt = ord[cnt].nxt;
    }
    int mx = 0;
    int idx = a+1;
    for(int i = a-1; i>= 0; i--){
        mx = max(mx, beg[i]);
        while(idx<n){
            if(mx>beg[idx]) idx++;
            else break;
        }
        link[i] = idx;
    }
    mx = 0;
    idx = a-1;
    for(int i = a+1; i<n; i++){
        mx = max(mx, beg[i]);
        while(idx>0){
            if(mx>beg[idx]) idx--;
            else break;
        }
        link[i] = idx;
    }
    link[a] = a;
    int q;
    cin >> q;
    while(q--){
        char c;
        cin >> c;
        if(c == 'E'){
            int i, e;
            cin >> i >> e;
            i--; e--;
            if(i == a) continue;
            ins(i, e);
            check.clear();
            num.clear();
            int cnt = 0;
            int st = head;
            while(cnt<BIG){
                if(st != a){
                    check.eb(st);
                    num.eb(cnt);
                }
                cnt++;
                st = ord[st].nxt;
                if(st == head) break;
            }
         //  cout << check << " " << num << endl;
            f0r(j, sz(check)) redo(check[j], num[j]);
            int cur = head;
          /*  f0r(i, n){
                cout << cur << " " << ord[cur].bef << " " << ord[cur].nxt << endl;
                cur = ord[cur].nxt;
            }
            cout << endl;
            f0r(i, n){
                cout << i << " " << link[i] << endl;
            }*/
        }
        else{
            int b;
            cin >> b;
            b--;
            if(b == a) cout << 1 - 1<< endl;
            else cout << abs(b - a)+1 + abs(link[b] - a) - 1-1 << endl;
        }
    }
    return 0;
}

Compilation message

cake.cpp:1:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/stack:200000000")
 
cake.cpp: In function 'void ins(int, int)':
cake.cpp:120:9: warning: unused variable 'cur' [-Wunused-variable]
     int cur = head;
         ^~~
cake.cpp: In function 'int main()':
cake.cpp:227:17: warning: unused variable 'cur' [-Wunused-variable]
             int cur = head;
                 ^~~
cake.cpp: In function 'void io(std::__cxx11::string)':
cake.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FIN, "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~
cake.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FOUT, "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 3 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 573 ms 5112 KB Output isn't correct
2 Correct 313 ms 5180 KB Output is correct
3 Incorrect 571 ms 5244 KB Output isn't correct
4 Incorrect 316 ms 5240 KB Output isn't correct
5 Incorrect 618 ms 5848 KB Output isn't correct
6 Incorrect 498 ms 6264 KB Output isn't correct
7 Incorrect 585 ms 5988 KB Output isn't correct
8 Incorrect 330 ms 6212 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 315 ms 5132 KB Output isn't correct
2 Incorrect 321 ms 5020 KB Output isn't correct
3 Incorrect 314 ms 5124 KB Output isn't correct
4 Correct 2 ms 376 KB Output is correct
5 Incorrect 365 ms 10496 KB Output isn't correct
6 Incorrect 362 ms 10344 KB Output isn't correct
7 Incorrect 351 ms 10344 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 110 ms 1008 KB Output isn't correct
2 Incorrect 112 ms 1128 KB Output isn't correct
3 Incorrect 184 ms 3312 KB Output isn't correct
4 Incorrect 180 ms 3032 KB Output isn't correct
5 Incorrect 320 ms 1912 KB Output isn't correct
6 Incorrect 345 ms 4376 KB Output isn't correct
7 Incorrect 433 ms 2908 KB Output isn't correct
8 Incorrect 297 ms 5740 KB Output isn't correct
9 Incorrect 1223 ms 15364 KB Output isn't correct
10 Incorrect 1068 ms 5348 KB Output isn't correct
11 Incorrect 1069 ms 6772 KB Output isn't correct
12 Incorrect 1202 ms 13676 KB Output isn't correct
13 Incorrect 1175 ms 15464 KB Output isn't correct