답안 #784600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784600 2023-07-16T10:44:43 Z YassineBenYounes 원숭이와 사과 나무 (IZhO12_apple) C++17
0 / 100
0 ms 320 KB
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<ll, ll>, null_type, less<pair<ll,ll>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////

void usaco_problem(){
    freopen("milkvisits.in", "r", stdin);
    freopen("milkvisits.out", "w", stdout);
}

void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}

const int mx = 2e5+5;
const int LOG = 25;
const ll inf = 1e18;
const ll mod = 1e8;

struct Node {
    ll value, lazy;
    Node *L, *R;
};

struct Node* create_node()
{
    struct Node* temp = new struct Node;
    temp->value = 0;
    temp->lazy = 0;
    temp->L = nullptr;
    temp->R = nullptr;
    return temp;
}

void unlazy(Node* node, int l, int r){
    if(node -> lazy == 0)return;
    node -> value = r-l+1;
    node -> lazy = 0;
    if(l == r)return;
    if(node-> L == nullptr)node -> L = create_node();
    if(node -> R == nullptr)node -> R = create_node();
    node -> L -> lazy = 1;
    node -> R -> lazy = 1;

}

void update(Node* node, int l, int r, int qlow, int qhigh){
    unlazy(node, l, r);
    if(qhigh < l || qlow > r){
        return;
    }
    if(l >= qlow && r <= qhigh){
        node -> lazy = 1;
        unlazy(node, l, r);
        return;
    }
    int md = (l+r)/2;
    ll sum1 = 0;
    if(max(l, qlow) <= min(md, qhigh)){
        if(node -> L == nullptr)node -> L = create_node();
        update(node -> L, l, md, qlow, qhigh);
        sum1 = node -> L -> value;
    }
    ll sum2 = 0;
    if(max(md+1, qlow) <= min(r, qhigh)){
        if(node -> R == nullptr)node -> R = create_node();
        update(node -> R, md+1, r, qlow, qhigh);
        sum2 = node -> R -> value;
    }
    node -> value = sum1 + sum2;
}

ll query(Node* node, int l, int r, int qlow, int qhigh){
    unlazy(node, l, r);
    if(qhigh < l || qlow > r){
        return 0;
    }
    if(l >= qlow && r <= qhigh){
        return node -> value;
    }
    int md = (l+r)/2;
    if(node-> L == nullptr)node -> L = create_node();
    ll x = query(node -> L, l, md, qlow, qhigh);
    if(node -> R == nullptr)node -> R = create_node();
    ll y = query(node -> R, md+1, r, qlow, qhigh);
    return (x+y);
}

void run_case(){
    int q;cin >> q;
    Node *root = create_node();
    int c = 0;
    while(q--){
        int type;cin >> type;
        if(type == 2){
            int l, r;cin >> l >> r;
            l += c;
            r += c;
            update(root, 1, 1e9, l, r);
        }
        else{
            int l, r;cin >> l >> r;
            //cout << l << " " << r << endl;
            l += c;r += c;
            c = query(root, 1, 1e9, l, r);
            cout << c << endl;
        }
    }
}

int main(){
    speed;
    int t;
    //cin >> t;
    t = 1;
    while(t--){
        run_case();
    }
}

/*
    NEVER GIVE UP!
    DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
    Your Guide when stuck:
    - Continue keyword only after reading the whole input
    - Don't use memset with testcases
    - Check for corner cases(n=1, n=0)
    - Check where you declare n(Be careful of declaring it globally and in main)
*/

Compilation message

apple.cpp: In function 'void usaco_problem()':
apple.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen("milkvisits.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("milkvisits.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp: In function 'void init()':
apple.cpp:39:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:41:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 320 KB Output isn't correct
4 Halted 0 ms 0 KB -