Submission #784597

# Submission time Handle Problem Language Result Execution time Memory
784597 2023-07-16T10:39:22 Z YassineBenYounes Monkey and Apple-trees (IZhO12_apple) C++17
0 / 100
321 ms 9420 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("f.in", "r", stdin);
    freopen("f.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;
    }
    //cout << l << " " << r << endl;
    int md = (l+r)/2;
    if(node-> L == nullptr)node -> L = create_node();
    if(node -> R == nullptr)node -> R = create_node();
    update(node -> L, l, md, qlow, qhigh);
    update(node -> R, md+1, r, qlow, qhigh);
    node -> value = node -> L -> value + node -> R -> value;
}

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(){
    //init();
    usaco_problem();
    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("f.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("f.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);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 321 ms 9420 KB Output isn't correct
2 Halted 0 ms 0 KB -