Submission #784610

# Submission time Handle Problem Language Result Execution time Memory
784610 2023-07-16T10:58:42 Z YassineBenYounes Monkey and Apple-trees (IZhO12_apple) C++17
0 / 100
374 ms 262144 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;
    }
    unlazy(node, l, r);
    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;
    }
    unlazy(node, l, r);
    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();
    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 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 14 ms 8540 KB Output is correct
5 Correct 17 ms 10292 KB Output is correct
6 Correct 17 ms 9940 KB Output is correct
7 Correct 18 ms 10328 KB Output is correct
8 Correct 161 ms 77020 KB Output is correct
9 Correct 294 ms 130808 KB Output is correct
10 Correct 304 ms 146868 KB Output is correct
11 Correct 343 ms 159292 KB Output is correct
12 Correct 327 ms 164804 KB Output is correct
13 Correct 305 ms 204952 KB Output is correct
14 Correct 303 ms 207140 KB Output is correct
15 Runtime error 374 ms 262144 KB Execution killed with signal 9
16 Halted 0 ms 0 KB -