답안 #38717

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
38717 2018-01-06T10:28:57 Z Talant 원숭이와 사과 나무 (IZhO12_apple) C++14
100 / 100
603 ms 122328 KB
#include <bits/stdc++.h>

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)7e6 + 7e5;

int n;
int type,x,y,c;
int cnt = 1;

struct node {
        int l,r,sum,add;
        node () {
                l = r = add = sum = 0;
        }
}t[N];
void push (int v,int tl,int tr) {
        if (t[v].add != 0) {
                if (!t[v].l)
                        t[v].l = ++cnt;
                if (!t[v].r)
                        t[v].r = ++cnt;

                t[t[v].l].add = t[v].add;
                t[t[v].r].add = t[v].add;
                t[v].sum = (tr - tl + 1);
        }
        t[v].add = 0;
}
void update (int l,int r,int v = 1,int tl = 1,int tr = 1e9 + 7) {
        if (tl > r || tr < l)
                return;
        if (t[v].add != 0)
                push (v,tl,tr);

        if (l <= tl && tr <= r) {
                t[v].add = 1;
        }
        else {
              int tm = (tl + tr) >> 1;

              if (t[v].l == 0)
                        t[v].l = ++cnt;
              if (t[v].r == 0)
                        t[v].r = ++cnt;

              update (l,r,t[v].l,tl,tm);
              update (l,r,t[v].r,tm + 1,tr);

              push (v,tl,tr);
              push (t[v].l,tl,tm);
              push (t[v].r,tm + 1,tr);

              t[v].sum = t[t[v].l].sum + t[t[v].r].sum;
        }
}
int get (int l,int r,int v = 1,int tl = 1,int tr = 1e9 + 7) {
        if (tl > r || tr < l)
                return 0;
        push (v,tl,tr);

        if (l <= tl && tr <= r)
                return t[v].sum;

        int tm = (tl + tr) >> 1;

        return get (l,r,t[v].l,tl,tm) + get(l,r,t[v].r,tm + 1,tr);
}
int main () {
        scanf ("%d", &n);

        for (int i = 1; i <= n; i ++) {
                scanf ("%d%d%d", &type,&x,&y);
                x += c;
                y += c;

                if (type == 1) {
                        c = get(x,y);
                        printf("%d\n", c);
                }
                else {
                        update (x,y);
                }
        }
}

Compilation message

apple.cpp: In function 'int main()':
apple.cpp:79:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &n);
                         ^
apple.cpp:82:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                 scanf ("%d%d%d", &type,&x,&y);
                                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 122328 KB Output is correct
2 Correct 26 ms 122328 KB Output is correct
3 Correct 9 ms 122328 KB Output is correct
4 Correct 29 ms 122328 KB Output is correct
5 Correct 46 ms 122328 KB Output is correct
6 Correct 53 ms 122328 KB Output is correct
7 Correct 36 ms 122328 KB Output is correct
8 Correct 193 ms 122328 KB Output is correct
9 Correct 426 ms 122328 KB Output is correct
10 Correct 469 ms 122328 KB Output is correct
11 Correct 456 ms 122328 KB Output is correct
12 Correct 409 ms 122328 KB Output is correct
13 Correct 393 ms 122328 KB Output is correct
14 Correct 366 ms 122328 KB Output is correct
15 Correct 603 ms 122328 KB Output is correct
16 Correct 573 ms 122328 KB Output is correct
17 Correct 356 ms 122328 KB Output is correct
18 Correct 406 ms 122328 KB Output is correct
19 Correct 519 ms 122328 KB Output is correct
20 Correct 549 ms 122328 KB Output is correct