답안 #518772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
518772 2022-01-24T14:59:03 Z aloo123 Poklon (COCI17_poklon) C++14
0 / 140
634 ms 524292 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
using vl = vector<ll>;
#define mp make_pair
#define f first
#define s second

// vectors
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define ins insert
#define pf push_front
#define pb push_back

// loops
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const int MOD = 1e9 + 7; // 998244353;
const ll INF = 1e18;     // not too close to LLONG_MAX

#define int ll
const int xd[8] = {1, 0, -1, 0, -1,-1,1,1}, yd[8] = {0, 1, 0, -1,-1,1,-1,1}; // for every grid problem!!
const int N = 5e5+5;
const int LOGN = 25;
const int BLOCKSZ = 350;

using namespace std;
int a[N];
struct query{
    int l,r,idx;
};
query Q[N];

bool cmp(query x, query y){
    return mp(x.l/BLOCKSZ, x.r) < mp(y.l/BLOCKSZ, y.r);
}
int freq[N];
int ans = 0;
int fans[N];
int add(int x){
    freq[a[x]]++;
    if(freq[a[x]] == 2) ans++;
    if(freq[a[x]] == 3) ans--;
}
int rem(int x){
    freq[a[x]]--;
    if(freq[a[x]] == 2) ans++;
    if(freq[a[x]] == 1) ans--;
}

void solve(){
    int n, q;
    cin >>n >> q;
    for(int i =0;i<n;i++)cin>>a[i];
   
   ////
    set<int> s;
    for(int i =0;i<n;i++)s.ins(a[i]);
    vl v;
    for(int i:s) v.pb(i);
    map<int,int>m;
    for(int i = 0;i<sz(v);i++){
        m[v[i]] = i + 1;
    }
    for(int i =0;i<n;i++){
        a[i] = m[a[i]];
    }
///////

    for(int i =1;i<=q;i++){
        cin >> Q[i].l>> Q[i].r;Q[i].idx=i;
        Q[i].l--;
        Q[i].r--;
    }
    sort(Q+1,Q+q+1,cmp);

    int cur_l= 0, cur_r = -1;
    for(int i = 1;i <=q;i++){
        while(cur_l>Q[i].l){
            cur_l--;
            add(cur_l);
        }
        while(cur_r < Q[i].r){
            cur_r++;
            add(cur_r);
        }
        while(cur_l<Q[i].l){
            rem(cur_l);
            cur_l++;
        }
        while(cur_r>Q[i].r){
            rem(cur_r);
            cur_r--;
        }
        fans[Q[i].idx] = ans;
    }
    for(int i= 1;i<=q;i++){
        cout<<fans[i]<<"\n";
    }








}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin>>t;
    while (t--){
        solve();
    }
}

Compilation message

poklon.cpp: In function 'll add(ll)':
poklon.cpp:79:1: warning: no return statement in function returning non-void [-Wreturn-type]
   79 | }
      | ^
poklon.cpp: In function 'll rem(ll)':
poklon.cpp:84:1: warning: no return statement in function returning non-void [-Wreturn-type]
   84 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 405 ms 524292 KB Execution killed with signal 9
2 Runtime error 390 ms 524292 KB Execution killed with signal 9
3 Runtime error 368 ms 524292 KB Execution killed with signal 9
4 Runtime error 368 ms 524292 KB Execution killed with signal 9
5 Runtime error 396 ms 524292 KB Execution killed with signal 9
6 Runtime error 434 ms 524292 KB Execution killed with signal 9
7 Runtime error 469 ms 524292 KB Execution killed with signal 9
8 Runtime error 526 ms 524292 KB Execution killed with signal 9
9 Runtime error 634 ms 524292 KB Execution killed with signal 9
10 Runtime error 634 ms 524292 KB Execution killed with signal 9