답안 #211295

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

const int mx = 500005;

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

list<int> sum[mx];

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

int cnt2;

void rmv(int i, bool f){
    cnt2 -= (sum[A[i]].size() == 2);
    if(f){
        sum[A[i]].pop_front();
    }else{
        sum[A[i]].pop_back();
    }
    cnt2 += (sum[A[i]].size() == 2);
}

void ins(int i, bool f){
    cnt2 -= (sum[A[i]].size() == 2);
    if(f){
        sum[A[i]].push_front(i);
    }else{
        sum[A[i]].push_back(i);
    }
    cnt2 += (sum[A[i]].size() == 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 11 ms 12160 KB Output is correct
2 Correct 11 ms 12160 KB Output is correct
3 Correct 12 ms 12160 KB Output is correct
4 Correct 24 ms 12544 KB Output is correct
5 Correct 656 ms 20504 KB Output is correct
6 Correct 667 ms 20596 KB Output is correct
7 Correct 2183 ms 29240 KB Output is correct
8 Correct 4428 ms 37864 KB Output is correct
9 Execution timed out 5072 ms 43368 KB Time limit exceeded
10 Execution timed out 5036 ms 49920 KB Time limit exceeded