Submission #525171

# Submission time Handle Problem Language Result Execution time Memory
525171 2022-02-11T02:02:08 Z Killer2501 Poklon (COCI17_poklon) C++14
140 / 140
418 ms 56692 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, int>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 5e5+5;
const int M = 14;
const ll inf = 1e9;
const int base = 350;
const int mod = 998244353;
int n, t, m, k, q, c[N], a[N], b[N];
ll ans, tong, d[N];
vector<int> vi, adj[N], gr[N];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
string s;
void add(int& x, int y)
{
    x += y;
    if(x >= mod)x -= mod;
    if(x < 0)x += mod;
}
struct BIT
{
    int n;
    vector<int> fe;
    BIT(int _n)
    {
        n = _n;
        fe.resize(n+1, 0);
    }
    void add(int id, int x)
    {
        for(; id <= n; id += id & -id)fe[id] += x;
    }
    void add(int l, int r, int x)
    {
        add(l, x);
        add(r+1, -x);
    }
    int get(int id)
    {
        int res = 0;
        for(; id; id -= id & -id)res += fe[id];
        return res;
    }
};
void sol()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        vi.pb(a[i]);
    }
    sort(vi.begin(), vi.end());
    vi.erase(unique(vi.begin(), vi.end()), vi.end());
    for(int i = 1; i <= n; i ++)
        a[i] = lower_bound(vi.begin(), vi.end(), a[i]) - vi.begin();
    for(int i = 1; i <= m; i ++)
    {
        cin >> b[i] >> k;
        adj[k].pb(i);
    }
    BIT bit(n);
    for(int i = 1; i <= n; i ++)
    {
        gr[a[i]].pb(i);
        if(gr[a[i]].size() >= 2)
        {
            int l = gr[a[i]].size() == 2 ? 1 : gr[a[i]][gr[a[i]].size()-3]+1;
            int r = gr[a[i]][gr[a[i]].size()-2];
            bit.add(l, r, 1);
        }
        if(gr[a[i]].size() >= 3)
        {
            int l = gr[a[i]].size() == 3 ? 1 : gr[a[i]][gr[a[i]].size()-4]+1;
            int r = gr[a[i]][gr[a[i]].size()-3];
            bit.add(l, r, -1);
        }
        for(int j: adj[i])d[j] = bit.get(b[j]);
    }
    for(int i = 1; i <= m; i ++)cout << d[i] <<'\n';
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
    }
    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
    return 0;
}
/*
    2 2
  4 2 2 3 5
6 4 2 1 3 5 7
*/

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:97:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
poklon.cpp:98:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23804 KB Output is correct
4 Correct 14 ms 24056 KB Output is correct
5 Correct 66 ms 30036 KB Output is correct
6 Correct 72 ms 30192 KB Output is correct
7 Correct 148 ms 36824 KB Output is correct
8 Correct 249 ms 44168 KB Output is correct
9 Correct 352 ms 50588 KB Output is correct
10 Correct 418 ms 56692 KB Output is correct