답안 #473775

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
473775 2021-09-16T08:48:56 Z ljuba Growing Trees (BOI11_grow) C++17
100 / 100
126 ms 3344 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef vector<int> vi;
typedef vector<ll> vll;
 
typedef vector<vi> vvi;
typedef vector<vll> vvll;
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
typedef vector<pii> vpii;
typedef vector<pll> vpll;
 
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
 
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
 
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
 
namespace debug {
    void __print(int x) {cerr << x;}
    void __print(long long x) {cerr << x;}
    void __print(double x) {cerr << x;}
    void __print(long double x) {cerr << x;}
    void __print(char x) {cerr << '\'' << x << '\'';}
    void __print(const string &x) {cerr << '\"' << x << '\"';}
    void __print(const char *x) {cerr << '\"' << x << '\"';}
    void __print(bool x) {cerr << (x ? "true" : "false");}
 
    template<typename T, typename V>
    void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
    template<typename T>
    void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
    void _print() {cerr << "]\n";}
    template <typename T, typename... V>
    void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifdef ljuba
#define dbg(x...) cerr << "\e[91m" << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
}
 
using namespace debug;
 
const char nl = '\n';
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
/*
погледај сампле тестове, пре него што имплементираш (можда имаш погрешну идеју) 
уместо да разматраш неке глупе случајеве на папиру, размишљај заправо о задатку
*/

const int mxN = 1e5 + 12;

struct ft {
    int a[mxN];

    void update(int i, int x) {
        for(; i < mxN; i += i&-i) {
            a[i] += x;
        }
    }

    int query(int i) {
        int r = 0;
        for(; i; i -= i&-i) {
            r += a[i];
        }
        return r;
    }
}ft;

void print(int n) {
    for(int i = 1; i <= n; ++i) {
        cout << ft.query(i) << " ";
    }
    cout << endl;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vi v(n);
    for(auto &z : v)
        cin >> z;

    sort(all(v));

    for(int i = 1; i <= n; ++i) {
        ft.update(i, v[i-1]);
        ft.update(i+1, -v[i-1]);
    }

    //spaghetti code

    while(m--) {
        //print(n);
        char c;
        int x, y;
        cin >> c >> x >> y;
        if(c == 'F') {
            int l = 1, r = n;
            int gde = -1;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(ft.query(mid) >= y) {
                    gde = mid;
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }

            if(gde == -1)
                continue;

            int prvi = gde;

            int drugi = gde + x - 1;
            if(drugi >= n) {
                ft.update(prvi, 1);
                ft.update(n+1, -1);
                continue;
            }

            int maksi = ft.query(drugi);

            l = 1, r = n;
            gde = -1;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(ft.query(mid) <= maksi) {
                    gde = mid;
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }

            //gde sada pokazuje na mesto gde je poslednje maksi
            l = prvi, r = drugi;
            int gde2 = -1;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(ft.query(mid) >= maksi) {
                    gde2 = mid;
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }

            int ima = drugi - gde2 + 1; //koliko ima u segmentu najvecih
            int t2 = gde;
            int t1 = gde-ima+1;
            ft.update(t1, 1);
            ft.update(t2+1, -1);

            ft.update(prvi, 1);
            ft.update(gde2, -1);
        } else {
            int l = 1, r = n;
            int levo = -1;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(ft.query(mid) >= x) {
                    levo = mid;
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
            l = 1, r = n;
            int desno = -1;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(ft.query(mid) <= y) {
                    desno = mid;
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }

            if(min(levo, desno) == -1 || levo > desno) {
                cout << 0 << nl;
            } else {
                cout << desno-levo+1 << nl;
            }
        }
    }
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int testCases = 1;
    //cin >> testCases;
    while(testCases--)
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 2316 KB Output is correct
2 Correct 114 ms 2756 KB Output is correct
3 Correct 59 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 67 ms 1356 KB Output is correct
6 Correct 69 ms 1608 KB Output is correct
7 Correct 7 ms 460 KB Output is correct
8 Correct 43 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 1644 KB Output is correct
2 Correct 72 ms 1732 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 48 ms 1248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 1868 KB Output is correct
2 Correct 76 ms 1644 KB Output is correct
3 Correct 7 ms 460 KB Output is correct
4 Correct 82 ms 1744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 1984 KB Output is correct
2 Correct 104 ms 2296 KB Output is correct
3 Correct 20 ms 860 KB Output is correct
4 Correct 41 ms 2244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 1936 KB Output is correct
2 Correct 108 ms 2340 KB Output is correct
3 Correct 47 ms 2512 KB Output is correct
4 Correct 20 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 2100 KB Output is correct
2 Correct 95 ms 2364 KB Output is correct
3 Correct 52 ms 2540 KB Output is correct
4 Correct 19 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 2756 KB Output is correct
2 Correct 104 ms 2284 KB Output is correct
3 Correct 31 ms 1732 KB Output is correct
4 Correct 92 ms 2172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 2528 KB Output is correct
2 Correct 126 ms 2688 KB Output is correct
3 Correct 117 ms 2864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 3344 KB Output is correct