結果
| 問題 |
No.2758 RDQ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-17 21:44:15 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,878 bytes |
| コンパイル時間 | 4,697 ms |
| コンパイル使用メモリ | 262,200 KB |
| 最終ジャッジ日時 | 2025-02-21 14:39:44 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | TLE * 1 -- * 20 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, a, b) for(ll i=a; i<b; i++)
#define rrep(i, a, b) for(ll i=a; i>=b; i--)
#define all(a) (a).begin(), (a).end()
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }
inline int popcount(int n) { return __builtin_popcount(n); } // 2進数で表した場合に立ってるビット数がいくつか
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int ctz(int n) { return n != 0 ? __builtin_ctz(n) : -1; } // 2進数で表した場合に 1 の位からいくつ 0 が連なっているか
inline int ctz(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int clz(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } // 2進数で表した場合に左側にいくつ 0 を埋める必要があるか
inline int clz(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
const double PI = 3.141592653589793;
const vector<int> DX = { 1, 0, -1, 0 };
const vector<int> DY = { 0, 1, 0, -1 };
const long long INF = 4004004003104004004LL; // (int)INF = 1010931620;
ll coordinate(ll h, ll w, ll W){ return h*W + w; } // 二次元座標を一次元座標に変換
#define endl "\n" // インタラクティブの時はコメントアウトする
int main()
{
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// cout << fixed << setprecision(18);
/*
Mo's アルゴリズム
verify: https://atcoder.jp/contests/abc174/tasks/abc174_f
参考: https://ei1333.hateblo.jp/entry/2017/09/11/211011
1. 配列の要素が不変
2. クエリを先読みできる
3. 区間 [l,r) の結果から区間[l+1,r), [l-1,r), [l,r-1), [l,r+1) の結果を計算できる
*/
int n,q;
cin >> n >> q;
vector<int> a(n);
rep(i,0,n) cin >> a[i];
int mx = *max_element(all(a));
int window = max(1, int(n/sqrt(q))); // 区間を区切る領域サイズ
int nl = 0, nr = 0; // [nl, nr): 現在の計算済み区間
vector<ll> res(q); // 各クエリの結果を格納する配列
// 問題に応じて書き換える
map<int,int> mp;
// index: 各クエリのインデックスを管理する配列
vector<int> l(q), r(q), k(q), index(q);
rep(i,0,q){
cin >> l[i] >> r[i] >> k[i];
l[i]--;
}
for (int i=0; i<q; i++) index[i] = i;
// ソート
sort(all(index), [&](int a, int b){
if (l[a]/window != l[b]/window) return l[a] < l[b];
if (l[a]/window % 2) return r[a] > r[b];
else return r[a] < r[b];
});
// 問題に応じて書き換える
auto add = [&](int idx){
int x = a[idx];
mp[x]++;
};
// 問題に応じて書き換える
auto del = [&](int idx){
int x = a[idx];
mp[x]--;
};
for (auto nex: index){
// クエリの区間に一致するまで、現在の区間を拡張 or 縮小
while (nl > l[nex]) add(--nl);
while (nr < r[nex]) add(nr++);
while (nl < l[nex]) del(nl++);
while (nr > r[nex]) del(--nr);
// 問題に応じて書き換える
// クエリの計算結果
int now = 1;
int cnt = 0;
while(now*k[nex]<=mx+10){
if (mp.find(now*k[nex])!=mp.end()) cnt += mp[now*k[nex]];
now++;
}
res[nex] = cnt;
}
rep(i,0,q) cout << res[i] << endl;
return 0;
}