Submission #784627

# Submission time Handle Problem Language Result Execution time Memory
784627 2023-07-16T11:17:19 Z YassineBenYounes Monkey and Apple-trees (IZhO12_apple) C++17
100 / 100
386 ms 207740 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;
    int md = (l+r)/2;
    if(node-> L == nullptr)node -> L = create_node();
    node -> L -> value = md-l+1;
    if(node -> R == nullptr)node -> R = create_node();
    node -> R -> value = r-md;
    node -> L -> lazy = 1;
    node -> R -> lazy = 1;
    node -> lazy = 0;
}
 
void update(Node* node, int l, int r, int qlow, int qhigh){
    
    if(qhigh < l || qlow > r){
        return;
    }
    if(l >= qlow && r <= qhigh){
        node -> value = r-l+1;
        node -> lazy = 1;
        return;
    }
    unlazy(node, l, r);
    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){
    if(qhigh < l || qlow > r){
        return 0;
    }
    if(l >= qlow && r <= qhigh){
        return node -> value;
    }
    unlazy(node, l, r);
    int md = (l+r)/2;
    ll x = 0, y = 0;
    if(node-> L != nullptr)x = query(node -> L, l, md, qlow, qhigh);
    if(node -> R != nullptr)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("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 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 14 ms 4884 KB Output is correct
5 Correct 15 ms 5868 KB Output is correct
6 Correct 12 ms 5588 KB Output is correct
7 Correct 12 ms 5844 KB Output is correct
8 Correct 94 ms 43636 KB Output is correct
9 Correct 204 ms 75544 KB Output is correct
10 Correct 237 ms 83640 KB Output is correct
11 Correct 268 ms 89788 KB Output is correct
12 Correct 246 ms 92728 KB Output is correct
13 Correct 194 ms 107856 KB Output is correct
14 Correct 213 ms 108900 KB Output is correct
15 Correct 361 ms 201720 KB Output is correct
16 Correct 330 ms 203108 KB Output is correct
17 Correct 223 ms 114780 KB Output is correct
18 Correct 219 ms 114888 KB Output is correct
19 Correct 354 ms 207708 KB Output is correct
20 Correct 386 ms 207740 KB Output is correct