답안 #1031772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031772 2024-07-23T06:59:15 Z GrindMachine Segments (IZhO18_segments) C++17
16 / 100
414 ms 19692 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

/*

refs:
other solutions
https://codeforces.com/blog/entry/124766 (log decomposition technique)

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int n;
    vector<vector<int>> tr;
    int LOG = 0;

    fenwick() {

    }

    fenwick(int n_) {
        n = n_;
        tr = vector<vector<int>>(n + 1);
        while((1<<LOG) <= n) LOG++;
    }

    int lsb(int x) {
        return x & -x;
    }

    void pupd(int i, int v) {
        i++;
        for(; i <= n; i += lsb(i)){
            tr[i].pb(v);
        }
    }

    void build(){
        rep1(i,n){
            sort(all(tr[i]));
        }
    }

    void clear(){
        tr.clear();
        tr.shrink_to_fit();
    }

    int get(int i, int x, int y) {
        i++;
        int res = 0;
        for(; i; i ^= lsb(i)){
            auto &a = tr[i];
            res += upper_bound(all(a),y)-lower_bound(all(a),x);
        }
        return res;
    }

    int query(int l, int r, int x, int y) {
        if (l > r) return 0;
        int res = get(r,x,y) - get(l-1,x,y);
        return res;
    }
};

struct S{
    vector<pii> a;
    fenwick<int> fenw1,fenw2;
    bool empty(){
        return a.empty();
    }
    void clear(){
        a.clear();
        a.shrink_to_fit();
        fenw1.clear(), fenw2.clear();
    }
    void build(){        
        fenw1 = fenwick<int>(sz(a));
        fenw2 = fenwick<int>(sz(a));
        rep(i,sz(a)){
            fenw1.pupd(i,a[i].ff);
            fenw2.pupd(i,a[i].ss);
        }
        fenw1.build();
        fenw2.build();
    }
    int query(int i, int l1, int r1, int k){
        int n = sz(a);
        return fenw1.query(i,n-1,r1-k+1,2*inf1)+fenw2.query(i,n-1,0,l1+k-1);
    }
};

struct DS{
    S a[20];
    void insert(int l, int r){
        S curr;
        curr.a.pb({l,r});

        rep(i,20){
            if(!a[i].empty()){
                S nxt;
                int ptr1 = 0, ptr2 = 0;
                while(ptr1 < sz(curr.a) and ptr2 < sz(a[i].a)){
                    int len1 = curr.a[ptr1].ss-curr.a[ptr1].ff;
                    int len2 = a[i].a[ptr2].ss-a[i].a[ptr1].ff;
                    if(len1 < len2){
                        nxt.a.pb(curr.a[ptr1++]);
                    }
                    else{
                        nxt.a.pb(a[i].a[ptr2++]);
                    }
                }                
                while(ptr1 < sz(curr.a)) nxt.a.pb(curr.a[ptr1++]);
                while(ptr2 < sz(a[i].a)) nxt.a.pb(a[i].a[ptr2++]);

                curr = nxt;
                nxt.clear();
                a[i].clear();                
            }
            else{
                a[i] = curr;
                a[i].build();
                break;
            }
        }
    }

    int query(int l1, int r1, int k){
        if(r1-l1 < k) return 0;
        int ans = 0;
        rep(i,20){
            if(a[i].empty()) conts;
            auto &b = a[i].a;
            int lo = 0, hi = sz(b)-1;
            int pos = -1;

            while(lo <= hi){
                int mid = (lo+hi) >> 1;
                if(b[mid].ss-b[mid].ff >= k){
                    pos = mid;
                    hi = mid-1;
                }
                else{
                    lo = mid+1;
                }
            }

            if(pos == -1) conts;

            int bad = a[i].query(pos,l1,r1,k);
            int add = sz(b)-pos-bad;
            ans += add;
        }

        return ans;
    }
};

void solve(int test_case)
{
    int q,c; cin >> q >> c;
    
    DS ds1,ds2;
    vector<pii> a;
    a.pb({0,0});

    int last_ans = 0;

    while(q--){
        int t; cin >> t;
        if(t == 1){
            int l,r; cin >> l >> r;
            l ^= c*last_ans, r ^= c*last_ans;
            if(l > r) swap(l,r);
            ds1.insert(l,r);
            a.pb({l,r});
        }
        else if(t == 2){
            int id; cin >> id;
            auto [l,r] = a[id];
            ds2.insert(l,r);
        }
        else{
            int l,r,k; cin >> l >> r >> k;
            l ^= c*last_ans, r ^= c*last_ans;
            if(l > r) swap(l,r);
            k--;
            int ans = ds1.query(l,r,k)-ds2.query(l,r,k);
            cout << ans << endl;
            last_ans = ans;
        }
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 10 ms 860 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 282 ms 12020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 331 ms 15664 KB Output is correct
2 Correct 285 ms 15048 KB Output is correct
3 Correct 323 ms 15024 KB Output is correct
4 Correct 281 ms 15040 KB Output is correct
5 Correct 292 ms 15596 KB Output is correct
6 Correct 230 ms 13400 KB Output is correct
7 Correct 252 ms 15164 KB Output is correct
8 Correct 356 ms 19684 KB Output is correct
9 Correct 307 ms 19688 KB Output is correct
10 Correct 371 ms 17944 KB Output is correct
11 Correct 289 ms 14060 KB Output is correct
12 Correct 328 ms 18148 KB Output is correct
13 Correct 313 ms 17472 KB Output is correct
14 Correct 290 ms 15592 KB Output is correct
15 Correct 300 ms 16384 KB Output is correct
16 Correct 284 ms 14824 KB Output is correct
17 Correct 209 ms 11760 KB Output is correct
18 Correct 215 ms 11772 KB Output is correct
19 Correct 223 ms 11772 KB Output is correct
20 Correct 213 ms 11768 KB Output is correct
21 Correct 292 ms 14196 KB Output is correct
22 Correct 289 ms 15960 KB Output is correct
23 Correct 343 ms 18160 KB Output is correct
24 Correct 294 ms 16028 KB Output is correct
25 Correct 298 ms 14944 KB Output is correct
26 Correct 302 ms 14992 KB Output is correct
27 Correct 308 ms 15280 KB Output is correct
28 Correct 300 ms 14928 KB Output is correct
29 Correct 344 ms 17220 KB Output is correct
30 Correct 335 ms 17388 KB Output is correct
31 Correct 353 ms 19692 KB Output is correct
32 Correct 341 ms 17896 KB Output is correct
33 Correct 341 ms 17648 KB Output is correct
34 Correct 320 ms 15104 KB Output is correct
35 Correct 296 ms 17092 KB Output is correct
36 Correct 332 ms 18152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 414 ms 15376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 10 ms 860 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 10 ms 860 KB Output isn't correct
4 Halted 0 ms 0 KB -