Submission #221815

# Submission time Handle Problem Language Result Execution time Memory
221815 2020-04-11T08:03:12 Z patrikpavic2 Street Lamps (APIO19_street_lamps) C++17
40 / 100
3692 ms 524292 KB
/**
* user:  ppavic
* fname: Patrik
* lname: Pavić
* task:  street-light
* score: 100.0
* date:  2019-07-06 10:35:14.642573
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>

#define X first
#define Y second
#define PB push_back

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair < int, int > pii;
typedef vector < int > vi;
typedef vector < pii > vp;


const int N = 3e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int LOG = 19;
const int OFF = (1 << LOG);

int n, m, POS, K, K2, I, FL;

struct node{
	node *L, *R;
	int sm = 0, sm2 = 0;
};


node *loga1[N], *loga2[N];

void update(node* cur, int a,int b){
	cur -> sm += K, cur -> sm2 += K2;
	//printf("%d %d += %d\n", a, b, k);
	if(a == b) return;
	if(I <= (a + b) / 2) {
		if(cur -> L == NULL) cur -> L = new node();
		update(cur -> L, a, (a + b) / 2);
		return;
	}
	if(cur -> R == NULL) cur -> R = new node();
	update(cur -> R, (a + b) / 2 + 1, b);
}

int get(node* cur, int a,int b){
	if(cur == NULL || a > I) return 0;
	if(b <= I) {
		if(!FL) return cur -> sm;
		else return POS * cur -> sm2 - cur -> sm;
	}
	return get(cur -> L, a, (a + b) / 2) + get(cur -> R, (a + b) / 2 + 1, b);
}

inline void dodaj(int x,int y,int k){
	y = n - y + 10; x += 10;
	for(; x < N; x += x & -x){
		if(loga1[x] == NULL) loga1[x] = new node();
		I = y, K = k, K2 = 0;
		update(loga1[x], 0, OFF - 1);
	}
}

inline int nadi(int x,int y){
	y = n - y + 10; x += 10;
	int ret = 0;
	for(; x ; x -= x & -x){
		I = y, FL = 0;
		ret += get(loga1[x], 0, OFF - 1);
	}
	//printf("ret = %d\n", ret);
	return ret;
}

inline void dodaj2(int x,int y,int k,int k2){
	y = n - y + 10; x += 10;
	for(; x < N; x += x & -x){
		if(loga2[x] == NULL) loga2[x] = new node();
		I = y, K = k, K2 = k2;
		update(loga2[x], 0, OFF - 1);
	}
}

inline int nadi2(int x,int y,int k){
	y = n - y + 10; x += 10; POS = k;
	ll ret = 0;
	for(; x ; x -= x & -x){
		I = y, FL = 1;
		ret += get(loga2[x], 0, OFF - 1);
	}
	//printf("ret = %d\n", ret);
	return ret;
}



set < pii > gr;
int A[N], L[M], R[M], tren[M], c = 0;

int nadi_interval(int x){
	if(x < 0 || x >= n || !A[x]) return -1;
	return (gr.lower_bound({x, 0})) -> Y;
}

void makni_interval(int x, int i){
	//printf("Nestaje interval [%d %d], ind = %d tren %d sad %d\n", L[x], R[x], x, tren[x], i);
	dodaj2(L[x], R[x], -tren[x], -1);
	dodaj(L[x], R[x], i - tren[x]);
	gr.erase({R[x], x});
}

void dodaj_interval(){
	//printf("Dolazi interval [%d %d], ind = %d tren %d\n", L[c], R[c], c, tren[c]);
	dodaj2(L[c], R[c], tren[c],1);
	gr.insert({R[c], c++});
}

int main(){
	scanf("%d%d", &n, &m);
	for(int i = 0;i < n;i++){
		char c; scanf(" %c", &c);
		A[i] = c - '0';
	}
	for(int i = 0;i < n;){
		if(!A[i]){
			i++; continue;
		}
		int j = i;
		for(; j < n && A[j]; j++);
		L[c] = i, R[c] = j - 1; tren[c] = 0;
		dodaj_interval();
		i = j;
	}
	for(int i = 1;i <= m;i++){
		char str[10]; scanf("%s", str);
		if(str[0] == 'q'){
			int l, r; scanf("%d%d", &l, &r); l--, r--, r--;
			//printf("%d %d %d\n", pas.nadi(l, r), cnt_akt.nadi(l, r), akt.nadi(l, r));
			printf("%d\n", nadi(l, r) + nadi2(l, r, i));
		}
		else{
			int x; scanf("%d", &x); x--;
			if(!A[x]){
				int lft = nadi_interval(x - 1);
				int rig = nadi_interval(x + 1);
				int nL = x, nR = x;
				if(lft != -1){
					nL = L[lft];
					makni_interval(lft, i);
				}
				if(rig != -1) {
					nR = R[rig];
					makni_interval(rig, i);
				}
				L[c] = nL, R[c] = nR; tren[c] = i; 
				dodaj_interval();
			}
			else{
				int moj = nadi_interval(x);
				makni_interval(moj, i);
				if(L[moj] != x){
					L[c] = L[moj], R[c] = x - 1, tren[c] = i;
					dodaj_interval();
				}
				if(R[moj] != x){
					L[c] = x + 1, R[c] = R[moj], tren[c] = i;
					dodaj_interval();
				}		
				
			}
			A[x] = !A[x];
		}
	}
	return 0;
}







Compilation message

street_lamps.cpp: In function 'void dodaj_interval()':
street_lamps.cpp:132:20: warning: operation on 'c' may be undefined [-Wsequence-point]
  gr.insert({R[c], c++});
                   ~^~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:136:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:138:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   char c; scanf(" %c", &c);
           ~~~~~^~~~~~~~~~~
street_lamps.cpp:152:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   char str[10]; scanf("%s", str);
                 ~~~~~^~~~~~~~~~~
street_lamps.cpp:154:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int l, r; scanf("%d%d", &l, &r); l--, r--, r--;
              ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:159:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int x; scanf("%d", &x); x--;
           ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 809 ms 6792 KB Output is correct
2 Correct 897 ms 8020 KB Output is correct
3 Correct 1622 ms 24620 KB Output is correct
4 Correct 3037 ms 437752 KB Output is correct
5 Correct 3274 ms 443768 KB Output is correct
6 Correct 3249 ms 465136 KB Output is correct
7 Correct 206 ms 8056 KB Output is correct
8 Correct 214 ms 9492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2688 KB Output is correct
2 Correct 12 ms 2560 KB Output is correct
3 Correct 13 ms 2176 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Runtime error 3352 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1152 KB Output is correct
2 Correct 9 ms 1920 KB Output is correct
3 Correct 13 ms 2304 KB Output is correct
4 Correct 12 ms 2688 KB Output is correct
5 Correct 778 ms 189304 KB Output is correct
6 Correct 1883 ms 368200 KB Output is correct
7 Correct 2816 ms 464632 KB Output is correct
8 Runtime error 3692 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 809 ms 6792 KB Output is correct
9 Correct 897 ms 8020 KB Output is correct
10 Correct 1622 ms 24620 KB Output is correct
11 Correct 3037 ms 437752 KB Output is correct
12 Correct 3274 ms 443768 KB Output is correct
13 Correct 3249 ms 465136 KB Output is correct
14 Correct 206 ms 8056 KB Output is correct
15 Correct 214 ms 9492 KB Output is correct
16 Correct 12 ms 2688 KB Output is correct
17 Correct 12 ms 2560 KB Output is correct
18 Correct 13 ms 2176 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Runtime error 3352 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
21 Halted 0 ms 0 KB -