提供兩種解法。
第一種:直接對詢問離線,然後莫隊分塊。
我們用乙個陣列vis來統計每個數出現的次數,然後維護區間內出現次數超過2的數量即可。
一開始加了個vis[i] = max(vis[i],0)防負數操作還wa了,這樣其實有點違背統計思想了。
//view codeauthor: levil
#includeusing
namespace
std;
typedef
long
long
ll;typedef pair
pii;
const
int n = 1e5+5
;const
int m = 12005
;const ll mod = 199999
;#define rg register
#define pi acos(-1)
#define inf 1e5+1
#define ct0 cin.tie(0),cout.tie(0)
#define io ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace
fastio
while(c >= '
0' && c <= '9')
return x*f;
}void print(int
x)
if(x > 9) print(x/10
); putchar(x%10+'0'
); }
}using
namespace
fastio;
void
fre()
int n,q,a[n],ans[n],vis[n],cnt = 0;//
cnt記錄出現超過一次的數字的數量
struct nodep[n];
bool
cmp(node a,node b)
void del(int
u)void add(int
u)int
main()
sort(p+1,p+q+1
,cmp);
int l = 0,r = 0
;
for(rg int i = 1;i <= q;++i)
for(rg int i = 1;i <= q;++i) printf("
%s\n
",ans[i] > 0 ? "
no" : "
yes"
);
//system("pause");
}
只要這個區間內的所有點上一次出現的位置都 < l,那麼說明滿足。
如果我們遍歷l到r去檢視每個點的上一次出現位置,顯然會t。
所以我們可以記錄最大的上一次出現位置,只要判斷這個位置是否滿足即可(因為這個位置會影響答案)
//view codeauthor: levil
#includeusing
namespace
std;
typedef
long
long
ll;typedef pair
pii;
const
int n = 1e5+5
;const
int m = 12005
;const ll mod = 199999
;#define rg register
#define pi acos(-1)
#define inf 1e5+1
#define ct0 cin.tie(0),cout.tie(0)
#define io ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace
fastio
while(c >= '
0' && c <= '9')
return x*f;
}void print(int
x)
if(x > 9) print(x/10
); putchar(x%10+'0'
); }
}using
namespace
fastio;
void
fre()
intn,q,a[n],max_pre[n],vis[n];
intmain()
while(q--)
//system("pause");
}
洛谷 P3901 數列找不同 思維
傳送門 給出n nn個數,以及q qq組詢問,對於li ri l i r i li ri 中所有數是否都是不相同的,如果是,就輸出yes yesye s,否則輸出nono no乙個很正常的思路就是對於每個區間都去判斷,但我們不妨換種思路,先預處理出對於每個位置符合條件的最右位置,然後在輸入範圍時就可...
P3901 數列找不同
題意 每次詢問乙個區間裡的數是否各不相同 顯然,當乙個區間裡的數各不相同時,它們的種數就是區間的長度 莫隊可以快速地求出區間的顏色種數,因此強制離線計算即可 include using namespace std const int n 100005 int n,q,a n block,ans 0,...
洛谷 P3901 數列找不同 莫隊
題目描述 現有數列 a1,a2,a na 1,a 2,ldots,a n a1 a2 an q qq 個詢問 li ri l i,r i li r i 詢問 ali ali 1,ari a a ldots,a ali ali 1 ar i 是否互不相同。輸入格式 第一行,兩個整數 n,q n,qn,...