답안 #125471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
125471 2019-07-05T11:55:51 Z arnold518 가로등 (APIO19_street_lamps) C++14
100 / 100
4255 ms 484964 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;
const int MAXVAL = 2e7;

int N, Q, A[MAXN+10];

set<int> S, E;

void operator += (pll &p, pll q) { p.first+=q.first, p.second+=q.second; }

pll range(int pos)
{
    pll ret;
    auto it=S.upper_bound(pos); it--; ret.first=*it;
    auto jt=E.lower_bound(pos); ret.second=*jt;
    return ret;
}

struct Node
{
    ll val; int one, pos;
    int lc, rc;
    Node() : lc(-1), rc(-1), val(0), one(0), pos(-1) {}
};

int cnt;
Node tree[MAXVAL];

void update1(int node, int tl, int tr, int x, ll val, int one)
{
    //printf("?:%d %d %d %d %lld %d\n", node, tl, tr, x, val, one);
    if(tl==tr)
    {
        tree[node].val+=val;
        tree[node].one+=one;
        return;
    }
    int mid=tl+tr>>1;

    if(tree[node].pos!=-1)
    {
        if(tree[node].pos<=mid)
        {
            tree[node].lc=cnt++;
            tree[tree[node].lc].pos=tree[node].pos;
            tree[tree[node].lc].val+=tree[node].val;
            tree[tree[node].lc].one+=tree[node].one;
        }
        else
        {
            tree[node].rc=cnt++;
            tree[tree[node].rc].pos=tree[node].pos;
            tree[tree[node].rc].val+=tree[node].val;
            tree[tree[node].rc].one+=tree[node].one;
        }
        tree[node].pos=-1;
    }

    if(x<=mid)
    {
        if(tree[node].lc==-1)
        {
            tree[node].lc=cnt++;
            tree[tree[node].lc].pos=x;
            tree[tree[node].lc].val+=val;
            tree[tree[node].lc].one+=one;
        }
        else
        {
            update1(tree[node].lc, tl, mid, x, val, one);
        }
    }
    else
    {
        if(tree[node].rc==-1)
        {
            tree[node].rc=cnt++;
            tree[tree[node].rc].pos=x;
            tree[tree[node].rc].val+=val;
            tree[tree[node].rc].one+=one;
        }
        else
        {
            update1(tree[node].rc, mid+1, tr, x, val, one);
        }
    }
    tree[node].val+=val;
    tree[node].one+=one;
}

pll query1(int node, int tl, int tr, int l, int r)
{
    //printf("%d %d %d %d %d %d %lld %d\n", node, tl, tr, l, r, tree[node].pos, tree[node].val, tree[node].one);
    if(r<tl || tr<l) return {0, 0};
    if(l<=tl && tr<=r) return {tree[node].val, tree[node].one};
    int mid=tl+tr>>1;
    if(tree[node].pos!=-1)
    {
        if(l<=tree[node].pos && tree[node].pos<=r) return {tree[node].val, tree[node].one};
        else return {0, 0};
    }
    pll ret;
    if(tree[node].lc!=-1) ret+=query1(tree[node].lc, tl, mid, l, r);
    if(tree[node].rc!=-1) ret+=query1(tree[node].rc, mid+1, tr, l, r);
    return ret;
}

void update2(int y, int x, ll val, int one)
{
    for(; y<=N; y+=(y&-y)) update1(y, 1, N, x, val, one);
}

pll query2(int y, int x)
{
    pll ret;
    for(; y>0; y-=(y&-y)) ret+=query1(y, 1, N, x, N);
    return ret;
}

int main()
{
    int i, j, k;

    scanf("%d%d", &N, &Q);
    for(i=1; i<=N; i++) scanf("%1d", &A[i]);

    if(A[1]==1) S.insert(1);
    if(A[N]==1) E.insert(N);
    for(i=2; i<=N; i++) if(A[i-1]==0 && A[i]==1) S.insert(i);
    for(i=1; i<=N-1; i++) if(A[i]==1 && A[i+1]==0) E.insert(i);

    cnt=N+1;

    for(auto it=S.begin(), jt=E.begin(); it!=S.end() && jt!=E.end(); it++, jt++) update2(*it, *jt, Q+1, 1);
/*
    for(auto it : S) printf("%d ", it); printf("\n");
    for(auto it : E) printf("%d ", it); printf("\n");
*/
    for(k=Q; k>=1; k--)
    {
        char s[10];
        scanf("%s", s);
        if(s[0]=='t')
        {
            int pos;
            scanf("%d", &pos);
            if(A[pos]==0)
            {
                S.insert(pos); E.insert(pos);
                if(pos!=1 && A[pos-1]==1)
                {
                    pll a=range(pos), b=range(pos-1);

                    update2(b.first, b.second, -k, -1);
                    E.erase(b.second);
                    S.erase(a.first);
                }

                if(pos!=N && A[pos+1]==1)
                {
                    pll a=range(pos), b=range(pos+1);

                    update2(b.first, b.second, -k, -1);
                    E.erase(a.second);
                    S.erase(b.first);
                }
                pll b=range(pos);
                update2(b.first, b.second, k, 1);

                A[pos]=1;
            }
            else
            {
                pll now=range(pos);

                if(now.first==now.second)
                {
                    update2(pos, pos, -k, -1);
                    S.erase(pos); E.erase(pos);
                }
                else if(pos==now.first)
                {
                    update2(now.first, now.second, -k, -1);
                    update2(pos+1, now.second, k, 1);
                    S.erase(pos);
                    S.insert(pos+1);
                }
                else if(pos==now.second)
                {
                    update2(now.first, now.second, -k, -1);
                    update2(now.first, pos-1, k, 1);
                    E.erase(pos);
                    E.insert(pos-1);
                }
                else
                {
                    update2(now.first, now.second, -k, -1);
                    update2(now.first, pos-1, k, 1);
                    update2(pos+1, now.second, k, 1);
                    S.insert(pos+1);
                    E.insert(pos-1);
                }
                A[pos]=0;
            }
        }
        else
        {
            int a, b;
            scanf("%d%d", &a, &b); b--;
            pll val=query2(a, b);
            //printf("%lld %lld\n", val.first, val.second);
            printf("%lld\n", val.first-val.second*k);
        }

        /*
        for(auto it : S) printf("%d ", it); printf("\n");
        for(auto it : E) printf("%d ", it); printf("\n");
        */
    }
}

Compilation message

street_lamps.cpp: In constructor 'Node::Node()':
street_lamps.cpp:28:13: warning: 'Node::rc' will be initialized after [-Wreorder]
     int lc, rc;
             ^~
street_lamps.cpp:27:8: warning:   'll Node::val' [-Wreorder]
     ll val; int one, pos;
        ^~~
street_lamps.cpp:29:5: warning:   when initialized here [-Wreorder]
     Node() : lc(-1), rc(-1), val(0), one(0), pos(-1) {}
     ^~~~
street_lamps.cpp: In function 'void update1(int, int, int, int, ll, int)':
street_lamps.cpp:44:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=tl+tr>>1;
             ~~^~~
street_lamps.cpp: In function 'pll query1(int, int, int, int, int)':
street_lamps.cpp:102:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=tl+tr>>1;
             ~~^~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:128:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j, k;
            ^
street_lamps.cpp:130:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &Q);
     ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:131:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=N; i++) scanf("%1d", &A[i]);
                         ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:148:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s", s);
         ~~~~~^~~~~~~~~
street_lamps.cpp:152:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &pos);
             ~~~~~^~~~~~~~~~~~
street_lamps.cpp:215:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d", &a, &b); b--;
             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 472 ms 469976 KB Output is correct
2 Correct 399 ms 470108 KB Output is correct
3 Correct 394 ms 469920 KB Output is correct
4 Correct 396 ms 469936 KB Output is correct
5 Correct 390 ms 469952 KB Output is correct
6 Correct 391 ms 469912 KB Output is correct
7 Correct 390 ms 470032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 673 ms 474092 KB Output is correct
2 Correct 749 ms 474448 KB Output is correct
3 Correct 1119 ms 475120 KB Output is correct
4 Correct 2602 ms 483964 KB Output is correct
5 Correct 2807 ms 484288 KB Output is correct
6 Correct 2860 ms 483964 KB Output is correct
7 Correct 615 ms 477856 KB Output is correct
8 Correct 621 ms 479224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 393 ms 470128 KB Output is correct
2 Correct 404 ms 470012 KB Output is correct
3 Correct 393 ms 470008 KB Output is correct
4 Correct 390 ms 470008 KB Output is correct
5 Correct 4255 ms 482560 KB Output is correct
6 Correct 3306 ms 483256 KB Output is correct
7 Correct 2293 ms 483736 KB Output is correct
8 Correct 625 ms 479120 KB Output is correct
9 Correct 498 ms 473720 KB Output is correct
10 Correct 510 ms 473976 KB Output is correct
11 Correct 509 ms 474232 KB Output is correct
12 Correct 631 ms 477756 KB Output is correct
13 Correct 687 ms 479224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 391 ms 470036 KB Output is correct
2 Correct 414 ms 470076 KB Output is correct
3 Correct 393 ms 470008 KB Output is correct
4 Correct 393 ms 470188 KB Output is correct
5 Correct 853 ms 484964 KB Output is correct
6 Correct 1638 ms 484104 KB Output is correct
7 Correct 2516 ms 483364 KB Output is correct
8 Correct 3657 ms 482700 KB Output is correct
9 Correct 780 ms 473564 KB Output is correct
10 Correct 733 ms 473080 KB Output is correct
11 Correct 795 ms 473524 KB Output is correct
12 Correct 759 ms 473080 KB Output is correct
13 Correct 797 ms 473592 KB Output is correct
14 Correct 779 ms 472888 KB Output is correct
15 Correct 608 ms 477812 KB Output is correct
16 Correct 666 ms 479104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 472 ms 469976 KB Output is correct
2 Correct 399 ms 470108 KB Output is correct
3 Correct 394 ms 469920 KB Output is correct
4 Correct 396 ms 469936 KB Output is correct
5 Correct 390 ms 469952 KB Output is correct
6 Correct 391 ms 469912 KB Output is correct
7 Correct 390 ms 470032 KB Output is correct
8 Correct 673 ms 474092 KB Output is correct
9 Correct 749 ms 474448 KB Output is correct
10 Correct 1119 ms 475120 KB Output is correct
11 Correct 2602 ms 483964 KB Output is correct
12 Correct 2807 ms 484288 KB Output is correct
13 Correct 2860 ms 483964 KB Output is correct
14 Correct 615 ms 477856 KB Output is correct
15 Correct 621 ms 479224 KB Output is correct
16 Correct 393 ms 470128 KB Output is correct
17 Correct 404 ms 470012 KB Output is correct
18 Correct 393 ms 470008 KB Output is correct
19 Correct 390 ms 470008 KB Output is correct
20 Correct 4255 ms 482560 KB Output is correct
21 Correct 3306 ms 483256 KB Output is correct
22 Correct 2293 ms 483736 KB Output is correct
23 Correct 625 ms 479120 KB Output is correct
24 Correct 498 ms 473720 KB Output is correct
25 Correct 510 ms 473976 KB Output is correct
26 Correct 509 ms 474232 KB Output is correct
27 Correct 631 ms 477756 KB Output is correct
28 Correct 687 ms 479224 KB Output is correct
29 Correct 391 ms 470036 KB Output is correct
30 Correct 414 ms 470076 KB Output is correct
31 Correct 393 ms 470008 KB Output is correct
32 Correct 393 ms 470188 KB Output is correct
33 Correct 853 ms 484964 KB Output is correct
34 Correct 1638 ms 484104 KB Output is correct
35 Correct 2516 ms 483364 KB Output is correct
36 Correct 3657 ms 482700 KB Output is correct
37 Correct 780 ms 473564 KB Output is correct
38 Correct 733 ms 473080 KB Output is correct
39 Correct 795 ms 473524 KB Output is correct
40 Correct 759 ms 473080 KB Output is correct
41 Correct 797 ms 473592 KB Output is correct
42 Correct 779 ms 472888 KB Output is correct
43 Correct 608 ms 477812 KB Output is correct
44 Correct 666 ms 479104 KB Output is correct
45 Correct 504 ms 472492 KB Output is correct
46 Correct 565 ms 472596 KB Output is correct
47 Correct 1365 ms 476180 KB Output is correct
48 Correct 2224 ms 483832 KB Output is correct
49 Correct 517 ms 474076 KB Output is correct
50 Correct 514 ms 474180 KB Output is correct
51 Correct 516 ms 474260 KB Output is correct
52 Correct 521 ms 474532 KB Output is correct
53 Correct 532 ms 474688 KB Output is correct
54 Correct 529 ms 474728 KB Output is correct