标签:sizeof int struct else define AC name -o sync
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---
const int N = 500050;
int n, m;
int a[N], ans[N], nx[N], vis[N];
bool in[N];
vector<pii> q[N];
struct Seg {
#define ls (rt<<1)
#define rs (ls|1)
static const int N = ::N*4+22;
int ma[N], ind[N];
void upd(int p, int c, int l, int r, int rt) {
if(l == r) {
ma[rt] = c;
ind[rt] = l;
return ;
}
int mid = l+r>>1;
if(p<=mid) upd(p, c, l, mid, ls);
else upd(p, c, mid+1, r, rs);
ma[rt] = max(ma[ls], ma[rs]);
ind[rt] = ma[ls]>ma[rs] ? ind[ls] : ind[rs];
}
void upd(pii &a, pii b) {
if(a < b) a = b;
}
pii qry(int L, int R, int l, int r, int rt) {
if(L<=l && r<=R) return mp(ma[rt], a[ind[rt]]);
int mid = l+r>>1;
pii ans = mp(0, 0);
if(L<=mid) upd(ans, qry(L, R, l, mid, ls));
if(R>=mid+1) upd(ans, qry(L, R, mid+1, r, rs));
return ans;
}
}seg;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
///read
cin >> n;
rep(i, 1, n+1) cin >> a[i];
cin >> m;
rep(i, 1, m+1) {
int x, y;
cin >> x >> y;
q[x].pb(mp(y, i));
}
///solve
for(int i = n; i; --i) {
nx[i] = vis[a[i]];
if(!nx[i]) nx[i] = n+1;
vis[a[i]] = i;
}
memset(vis, 0, sizeof(vis));
rep(i, 1, n+1) vis[nx[i]] = 1;
rep(i, 1, n+1) if(!vis[i]) seg.upd(i, nx[i], 1, n, 1);
rep(i, 1, n+1) {
for(auto t : q[i]) {
auto c = seg.qry(i, t.fi, 1, n, 1);
ans[t.se] = c.fi>t.fi ? c.se : 0;
}
if(nx[i]<=n) seg.upd(nx[i], nx[nx[i]], 1, n, 1);
}
rep(i, 1, m+1) cout << ans[i] << endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---
const int N = 505050;
int n, m;
int a[N], ans[N], ne[N], vis[N];
vector<pii> q[N];
struct Seg {
#define ls (rt<<1)
#define rs (ls|1)
static const int N = ::N<<2;
int pri[N], val[N], p, v;
void upd(int L, int R, int p, int v, int l, int r, int rt) {
if(L<=l && r<=R) {
if(p > pri[rt]) {
pri[rt] = p;
val[rt] = v;
}
return ;
}
int mid = l+r>>1;
if(L<=mid) upd(L, R, p, v, l, mid, ls);
if(R>=mid+1) upd(L, R, p, v, mid+1, r, rs);
}
void _qry(int pos, int l, int r, int rt) {
if(pri[rt] > p) {
p = pri[rt];
v = val[rt];
}
if(l == r) return ;
int mid = l+r>>1;
if(pos<=mid) _qry(pos, l, mid, ls);
else _qry(pos, mid+1, r, rs);
}
pii qry(int pos) {
p = v = 0;
_qry(pos, 1, n, 1);
return mp(p, v);
}
}seg;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n;
rep(i, 1, n+1) cin >> a[i];
cin >> m;
rep(i, 1, m+1) {
int x, y;
cin >> x >> y;
q[x].pb(mp(y, i));
}
for(int i = n; i; --i) {
ne[i] = vis[a[i]];
vis[a[i]] = i;
if(!ne[i]) ne[i] = n+1;
}
memset(vis, 0, sizeof(vis));
rep(i, 1, n+1) vis[ne[i]] = 1;
rep(i, 1, n+1) if(!vis[i]) seg.upd(i, ne[i] - 1, i, a[i], 1, n, 1);
rep(i, 1, n+1) {
for(auto j : q[i]) {
pii t = seg.qry(j.fi);
ans[j.se] = t.fi >= i ? t.se : 0;
}
if(ne[i]<=n) seg.upd(ne[i], ne[ne[i]] - 1, ne[i], a[i], 1, n, 1);
}
rep(i, 1, m+1) cout << ans[i] << endl;
return 0;
}
codeforces 1000F One Occurrence
标签:sizeof int struct else define AC name -o sync
原文地址:https://www.cnblogs.com/wuyuanyuan/p/9240463.html