# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
211296 | TAMREF | Poklon (COCI17_poklon) | C++11 | 1518 ms | 19432 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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();
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |