답안 #211296

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211296 2020-03-20T01:59:16 Z TAMREF Poklon (COCI17_poklon) C++11
140 / 140
1518 ms 19432 KB
#include <bits/stdc++.h>
using namespace std;

const int mx = 500005;

int A[mx];
vector<int> cmpa;

int sz[mx];

struct Query{
    int s, e, i;
    int64_t o;
} Que[500005];

int cnt2;

void rmv(int i, bool f){
    cnt2 -= (sz[A[i]] == 2);
    --sz[A[i]];
    cnt2 += (sz[A[i]] == 2);
}

void ins(int i, bool f){
    cnt2 -= (sz[A[i]] == 2);
    ++sz[A[i]];
    cnt2 += (sz[A[i]] == 2);
}

int n, q, b;
int ans[500005];

const int rotateDelta[4] = {3, 0, 0, 1};
inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
	if (pow == 0) {
		return 0;
	}
	int hpow = 1 << (pow-1);
	int seg = (x < hpow) ? (
		(y < hpow) ? 0 : 3
	) : (
		(y < hpow) ? 1 : 2
	);
	seg = (seg + rotate) & 3;
	int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
	int nrot = (rotate + rotateDelta[seg]) & 3;
	int64_t subSquareSize = int64_t(1) << (2*pow - 2);
	int64_t ans = seg * subSquareSize;
	int64_t add = hilbertOrder(nx, ny, pow-1, nrot);
	ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
	return ans;
}

void input(){
    cin >> n >> q;
    for(int i=1;i<=n;i++){
        cin >> A[i];
        cmpa.push_back(A[i]);
    }
    sort(cmpa.begin(),cmpa.end());
    cmpa.erase(unique(cmpa.begin(),cmpa.end()),cmpa.end());
    for(int i = 1; i <= n; i++){
        A[i] = lower_bound(cmpa.begin(),cmpa.end(),A[i]) - cmpa.begin();
    }
    for(int i=0;i<q;i++){
        cin >> Que[i].s >> Que[i].e;
        if(Que[i].s > Que[i].e) swap(Que[i].s, Que[i].e);
        Que[i].i = i;
        Que[i].o = hilbertOrder(Que[i].s, Que[i].e, 19, 0);
    }

    sort(Que,Que+q,[](Query &u, Query &v){
            return u.o < v.o;
         });
}

void query(){
    for(int i=0, lo=0, hi=-1;i<q;i++){
        int nlo = Que[i].s, nhi = Que[i].e;
        while(hi < nhi){
            ++hi;
            ins(hi, false);
        }
        while(lo > nlo){
            --lo;
            ins(lo, true);
        }
        while(hi > nhi){
            rmv(hi, false);
            --hi;
        }
        while(lo < nlo){
            rmv(lo, true);
            ++lo;
        }
        lo = nlo;
        hi = nhi;
        ans[Que[i].i] = cnt2;
    }
}

void output(){
    for(int i=0;i<q;i++) cout << ans[i] << '\n';
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    input();
    query();
    output();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 9 ms 640 KB Output is correct
5 Correct 178 ms 4212 KB Output is correct
6 Correct 180 ms 4212 KB Output is correct
7 Correct 450 ms 7920 KB Output is correct
8 Correct 771 ms 11884 KB Output is correct
9 Correct 1131 ms 15464 KB Output is correct
10 Correct 1518 ms 19432 KB Output is correct