답안 #477494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477494 2021-10-02T09:53:57 Z LptN21 Poklon (COCI17_poklon) C++14
140 / 140
1148 ms 49044 KB
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define PI acos(-1.0)
#define FF first
#define SS second
#define eps 1e-9
// vector
#define pb push_back
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define lb lower_bound
#define ub upper_bound
// bit
#define CNTBIT(x) __builtin_popcount(x)
#define ODDBIT(x) __builtin_parity(x)
#define MASK(i) (1ll<<(i))
#define BIT(x, i) (((x)>>(i))&1)
#define SUBSET(big, small) (((big)&(small))==(small))
#define MINBIT(x) (x)&(-x)
// typedef
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, int> ii;
typedef pair<int, ii> i3;
/* CODE BELOW */
const int N=500000+7, M=447; // M=SQRT(N) ~ 447 + const
const int MOD=1e9+7;
const ll oo=1e18+9;

int n, m, k, t;

int st[N<<2];

void update(int idx, int l0, int r0, int p, int v) {
    if(l0>p||r0<p) return;
    if(l0==p&&r0==p) {st[idx]+=v; return;}
    int mid=(l0+r0)/2;
    update(idx*2, l0, mid, p, v);
    update(idx*2+1, mid+1, r0, p, v);
    st[idx]=st[idx*2]+st[idx*2+1];
}
int query(int idx, int l0, int r0, int l, int r) {
    if(l0>r||r0<l) return 0;
    if(l<=l0&&r0<=r) return st[idx];
    int mid=(l0+r0)/2;
    int ansl=query(idx*2, l0, mid, l, r);
    int ansr=query(idx*2+1, mid+1, r0, l, r);
    return ansl+ansr;
}
void upd(int p, int v) {
    if(p) update(1, 1, n, p, v);
}
int qry(int l, int r) {
    return query(1, 1, n, l, r);
}

int ans[N], a[N];
map<int, vector<int> > lst;
map<int, int> cnt;
vector<ii> q[N];

signed main() {
    //freopen("BTD.INP", "r", stdin);
    //freopen("BTD.OUT", "w", stdout);
    //fastIO;
    scanf("%d%d", &n, &m); int l, r;
    memset(st, 0, sizeof st);
    for(int i=1;i<=n;i++) scanf("%d", &a[i]);

    const int SUB1=3000;

    if(max(n, m)<=SUB1) {
        for(int i=1;i<=m;i++) {
            scanf("%d%d", &l, &r);
            map<int, int> _cnt;
            int _ans=0;
            for(int j=l;j<=r;j++) _cnt[a[j]]++;
            for(auto it=_cnt.begin();it!=_cnt.end();it++)
                if(it->SS==2) _ans++;
            printf("%d\n", _ans);
        }
        return 0;
    }

    for(int i=1;i<=m;i++) {
        scanf("%d%d", &l, &r);
        q[r].pb(ii(l, i));
    }

    for(int i=1;i<=n;i++) {
        cnt[a[i]]++;
        if(cnt[a[i]]>1) {
            int lsz=sz(lst[a[i]]);
            upd(lst[a[i]][lsz-1], 1);
            upd(lst[a[i]][lsz-2], -2);
            if(lsz>2) upd(lst[a[i]][lsz-3], 1);
        } else lst[a[i]].pb(0);

        for(ii qr:q[i]) {
            tie(l, r)=qr;
            ans[r]=qry(l, i);
        }
        lst[a[i]].pb(i);
    }

    for(int i=1;i<=m;i++) printf("%d\n", ans[i]);
    return 0;
}
/* stuff you should look for
    - int overflow, array bounds
    - special/edge cases (n=1?)
    - do smth instead of nothing and stay organized
    - WRITE STUFF DOWN
    - DONT JUST STICK ON ONE APPROACH
*/

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |     scanf("%d%d", &n, &m); int l, r;
      |     ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:70:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     for(int i=1;i<=n;i++) scanf("%d", &a[i]);
      |                           ~~~~~^~~~~~~~~~~~~
poklon.cpp:76:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |             scanf("%d%d", &l, &r);
      |             ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         scanf("%d%d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19856 KB Output is correct
2 Correct 17 ms 19848 KB Output is correct
3 Correct 34 ms 19888 KB Output is correct
4 Correct 18 ms 20180 KB Output is correct
5 Correct 164 ms 25416 KB Output is correct
6 Correct 194 ms 25468 KB Output is correct
7 Correct 477 ms 31608 KB Output is correct
8 Correct 691 ms 38072 KB Output is correct
9 Correct 943 ms 43872 KB Output is correct
10 Correct 1148 ms 49044 KB Output is correct